Each of the following examples runs almost identical code, but presents it in a different way, depending on the needs of the coder and the reader.
In this example, all code is shown, and all outputs are printed. Good for sharing with others for troubleshooting or for sharing specific code. Each code chunk is introduced with a brief explanation, taking the place of #comments.
Question: What is the solar irradiance at the orbital distance of Mars? How about for the other planets?
Set variables for solar irradiance at Earth’s orbital radius, in W * m-2
insol_earth <- 1360
Read planet orbital radii from .csv, and drop Pluto, because really. Then calc insolation at each planet using inverse square law.
planet_df <- read.csv('data/planets.csv')
r_earth <- planet_df$radius[3]
planet_df <- planet_df %>%
filter(planet != 'Pluto') %>%
mutate(au = radius/r_earth,
insol = insol_earth/(au^2))
Print output table to see results for all planets.
print(planet_df)
## planet radius au insol
## 1 Mercury 59 0.3933333 8790.577420
## 2 Venus 108 0.7200000 2623.456790
## 3 Earth 150 1.0000000 1360.000000
## 4 Mars 228 1.5200000 588.642659
## 5 Jupiter 778 5.1866667 50.554781
## 6 Saturn 1426 9.5066667 15.048124
## 7 Uranus 2870 19.1333333 3.714990
## 8 Neptune 4497 29.9800000 1.513128
cat(sprintf('Insolation on Mars is %.3f W * m^-2, about %.1f%% that on Earth.\n',
planet_df$insol[4], planet_df$insol[4]/insol_earth*100))
## Insolation on Mars is 588.643 W * m^-2, about 43.3% that on Earth.
In this example, the coder doesn’t need to present every line of code, but rather needs to present the overall process of loading, crunching, and reporting the data, so another scientist can understand the whole process, and if necessary, replicate it. References, links, and provenance of data files are more important here, so the reader can understand where the data sets are coming from.
Each code chunk is still introduced with a brief comment, as before, though only the important outputs are displayed. The code is still all here, so an interested reader can look up specific code if needed.
Note the code chunk options: echo = FALSE
to hide code, but cat()
or print()
still allow for outputs to be displayed if desired
Question: What is the solar irradiance at the orbital distance of Mars? How about for the other planets?
Solar irradiance at Earth’s orbital radius = 1360 W * m-2 [Wikipedia, 2015]
Read planet orbital radii from .csv, and drop Pluto, because really. Then calc insolation at each planet using inverse square law.
## Reading orbits data from file: data/planets.csv
## File `data/planets.csv`: most recent commit info: commit [4cd1fb71e26959923424008eb0c555a109ad63f1](https://github.com/eco-data-science/rmarkdown_R/commit/4cd1fb71e26959923424008eb0c555a109ad63f1) Author: Casey O'Hara <ohara@nceas.ucsb.edu> Date: Thu Oct 8 11:31:34 2015 -0700
planet | radius | au | insol |
---|---|---|---|
Mercury | 59 | 0.3933333 | 8829.359379 |
Venus | 108 | 0.7200000 | 2635.030864 |
Earth | 150 | 1.0000000 | 1366.000000 |
Mars | 228 | 1.5200000 | 591.239612 |
Jupiter | 778 | 5.1866667 | 50.777817 |
Saturn | 1426 | 9.5066667 | 15.114513 |
Uranus | 2870 | 19.1333333 | 3.731380 |
Neptune | 4497 | 29.9800000 | 1.519804 |
According to the analysis, insolation on Mars is 591.24 W m2, about 43% that on Earth.
insol_plot <- ggplot(data = planet_df, aes(x = radius, y = insol, color = planet)) +
### set text style, title size and position, and legend position:
theme(text = element_text(family = 'Helvetica', color = 'gray30', size = 12),
plot.title = element_text(size = rel(1.5), hjust = 0, face = 'bold'),
legend.position = 'right') +
scale_x_log10() + scale_y_log10() +
geom_point(size = 3) +
labs(title = 'Insolation on other planets',
x = 'radius (x 10^6 km) (log scale)',
y = 'insolation (W * m^-2) (log scale)')
print(insol_plot)
In this example, the actual work is all invisible. The coder simply wishes to report the final findings in an easy-to-read format. In-line R code allows the writer to present the results in formatted text, rather than in blocky code outputs; LaTeX generates nice-looking equations, and the plots are generated behind the scenes and displayed.
Note for all code chunks, echo = FALSE
, and most of the results reported in inline R code rather than printing from within the code chunks)
Question: What is the solar irradiance at the orbital distance of Mars? How about for the other planets?
Solar irradiance at Earth’s orbital radius = 1360 W * m-2 [Wikipedia, 2015].
Insolation can be determined from the inverse-square law, using the equation:
\[ S_r = S_{Earth} * \left(\frac{r_{Earth}}{r_x}\right) ^2 \]
According to the analysis, insolation on Mars, at 1.52 times the Earth’s orbital radius, is 591.24 W m2, about 43% that on Earth. For other planets, examine figure 1.