--- title: "6. Learn R: Rmarkdown code block options" date: 18.8.2023 author: Matti Pirinen output: html_document --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ``` This time our Learn R session is about R markdown format. As you have likely noticed, the section titles in Rmd are made by inserting \# symbols in sequence 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 with two asterisks (\*\*) and *italize* text by surrounding it with one asterisk (\*). There is a whole Tex / Latex language for writing mathematical documents, and most common parts of Tex 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 certain formulas / symbols. For example, in Tex we write that $y_i$ is the mean of values of the square roots of $\alpha_{ik}$'s like this $$y_i = \frac{1}{K} \sum_{k=1}^K \sqrt{\alpha_{ik}}.$$ ## R code blocks In Rmd file, when we make a code block with \```{r} notation, we can tune a couple of things: * \```{r, eval = FALSE} makes R markdown to skip the code block completely. 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, but not the results, from appearing in the finished file. 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 finished file. R Markdown still runs this code block, and the results 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 finished document. * \```{r, warning = FALSE} prevents warnings that are generated by the code from appearing in the finished document. For example, compare ```{r} x = 2 + 3 x ``` with the same block 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 block in which `x = 3 + 3`, but with `include = FALSE` so 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 block was still run and the value of x should now be 6: ```{r} x ``` ## Size of the plot in R markdown. We can also specify the dimensions of the figures in 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 this setting where we set `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) ```