Five R Markdown tricks that you may not know about | by Keith McNulty | Towards Data Science

Many of you who regularly read my articles will know how much of a fan of R Markdown I am. It is hands down the best data science publishing software, and as well as being completely free it is also constantly being developed to be better and more flexible.
As I go along, I keep picking up new tricks to make my life easier or to get the most out of R Markdown. I’m sharing the latest five with you here. Hope you find them useful.
Many of you likely know about chunk options in R Markdown. When you write a code chunk you can add numerous options to instruct on what you want to do with that chunk. For example:
```{r, echo = FALSE}# some R code ```
This will display the results of the code chunk but will not display the code itself. Or:
```{python, eval = FALSE}# some python code```
This will display the code but will not run it. In fact there are a very wide array of chunk options available, and it can sometimes get untidy and difficult to follow if you have to put a lot of them in curlys {}.
If you install knitr version 1.35+ you can now type your chunk options line by line inside the chunk using a new chunk option special comment #| , for example:
```{r}#| echo = FALSE, out.width = "80%"#| fig.cap = "My caption"my_plot```
Did you know that you can put conditions in your chunk options? This can be super useful and can allow you to run a chunk only if certain conditions are satisfied. For example, this chunk will only be evaluated if the object my_data has more than three rows:
```{r}#| eval = nrow(my_data) > 3my_data```
You can also use conditional chunks to show different things depending on whether you are rendering to PDF or HTML — meaning you only have to keep one Rmd file irrelevant of your intended output. For example:
```{r}#| eval = knitr::is_html_output()# code for html output (eg interactive graphic)``````{r}#| eval = knitr::is_latex_output()# code for pdf output (eg static graphic)```
You can also use these knitr output functions in if statements, so another way of doing the above is:
```{r}if (knitr::is_html_output()) {  # code for html output} else {  # code for latex output}```
Some of you may write your Shiny apps in using two separate ui.R and server.R files, but you can also write your full app in a single R Markdown file. One of the advantages of doing this is that you can customize your app using Javascript by simply typing the Javascript directly in a js chunk.
Let’s look at a simple example. In Shiny, there is currently no direct option to limit the number of characters in a textarea input box. So if I use this standard Shiny UI function:
```{r}shiny::textAreaInput(  "comment",  "Enter your comment:")```
This will create a textarea box with the ID of comment. However, if I want to keep all comments limited to, say, 1,000 characters, I have no easy way of doing this in the textAreaInput() function.
Never fear though — if you know a little Javascript/jQuery, you can add a js chunk to add a maxlength attribute to the textarea box, like this:
```{js}$('[id="comment"]').attr('maxlength', 1000)```
If your document or application is getting very long and has a lot of code inside the chunks, that can get annoying to follow and difficult to maintain. Instead, you can take the code from inside the chunks and save them in R files, and then use the code chunk option to read that code from the R or Python file. For example:
```{python}#| code = readLines("my_setup_code.py")# python code for setup```
If you want to customize the style of your document or application, you can use CSS code chunks to do this really easily. You may have to render the document and then inspect it in Google Chrome to identify the class or ID of the specific element you want to customize, but as a simple example, if you wanted to change the background color of the main body of your document to a nice light blue, you can simply insert this chunk:
```{css}body { background-color: #d0e3f1;}```
Hope you found these neat little tricks useful. If you have any others that you’d like to suggest please do so in the comments.
Originally I was a Pure Mathematician, then I became a Psychometrician and a Data Scientist. I am passionate about applying the rigor of all those disciplines to complex people questions. I’m also a coding geek and a massive fan of Japanese RPGs. Find me on LinkedIn or on Twitter. Also check out my blog on drkeithmcnulty.com.