--- title: "Learn R 6: Rmarkdown code block options" date: 20.8.2024 author: Matti Pirinen output: html_document --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ``` This time our Learn R session is about the R markdown format. As you have likely noticed, the section titles in Rmd are created by inserting a sequence of \# symbols of length between 1 and 4, where 1 corresponds to the top level title and 4 to the lowest level title. # This is the highest level title made by a single \# #### This is the lowest level title made by four symbols \#\#\#\# Additionally, one can **bold** text by surrounding it within two asterisks (\*\*) and *italize* text by surrounding it with one asterisk (\*). Tex / LaTex language for writing mathematical documents can be directly used within an Rmd file. We don't study Tex on this course, but you can find help from the web by googling how to use it to write formulas / symbols. For example, in Tex we write that $y_i$ is the mean of values of the square roots of $\alpha_{ik}^3$'s like this $$y_i = \frac{1}{K} \sum_{k=1}^K \sqrt{\alpha_{ik}^3}.$$ ## R code blocks In an Rmd file, when we start a code block by writing \```{r}, we can simultaneously tune a couple of things: * \```{r, eval = FALSE} makes R markdown to skip the code block completely, but the code still shows up in the knitted document. This may be useful if some code block takes a lot of time to run, and you want to work on the other parts of the document without every time running through the heavy code block. * \```{r, echo = FALSE} prevents the code from appearing in the knitted document but the code is still run and its results will be shown in the knitted document. This is a useful way to embed plots without showing a possibly long code that produced the figure. * \```{r, include = FALSE} prevents code and results from appearing in the knitted documnet. R Markdown still runs this code block, and the results (e.g. variables that are assigned values in this code block) can be used by other code blocks that appear later in the document. * \```{r, message = FALSE} prevents messages that are generated by the code from appearing in the knitted document. * \```{r, warning = FALSE} prevents warnings that are generated by the code from appearing in the knitted document. For example, compare ```{r} x = 2 + 3 x ``` with the same code but with option `echo = FALSE`: ```{r, echo = FALSE} x = 2 + 3 #These are still run but not printed x ``` and the same block but with `eval = FALSE`: ```{r, eval = FALSE} x = 2 + 3 #These are not run since eval = FALSE but they are still printed since echo is not FALSE x ``` and the slightly changed code `x = 3 + 3`, using `include = FALSE` when we don't see anything in the compiled document. ```{r, include = FALSE} x = 3 + 3 #These are run but not printed and their results are not printed either since include = FALSE x ``` Even though we can't see the `x = 3 + 3` in the final document, the code was still run and the value of x should now be 6 rather than 5: ```{r} x ``` ## Size of the plot in R markdown. We can also specify the dimensions of the figures in the R markdown output. Our default figure looks like this ```{r} plot(pressure) ``` We can modify the size of the figure by `fig.width =` and `fig.height =` in the code block specification. Thus, by \```{r, fig.width = 8, fig.height = 6} we get a bigger output figure: ```{r, fig.width = 8, fig.height = 6} plot(pressure) ``` In particular, it may be useful to increase the default width of the plot when we plot many panels into the same figure. The following setting looks quite bad with the default options ```{r} par(mfrow = c(1,3)) plot(pressure) plot(pressure) plot(pressure) ``` compared to setting `fig.width = 11, fig.height = 3.5` ```{r, fig.width = 11, fig.height = 3.5} par(mfrow = c(1,3)) plot(pressure) plot(pressure) plot(pressure) ```