RStudio and R Markdown Tips

Page content

Here are some miscellaneous tips for working on RStudio and writing R Markdown file. This blog will be updated from time to time.

See also:

1. RStudio

1.1 Re-view generated html file in RStudio

If need to view closed html result file, simply run:

rstudioapi::viewer("myfile.html")

2. R Markdown

2.1 Show code but not run

Add eval=FALSE in chunk option.

2.2 Output data frames as tables

Use kable(data, caption):

data <- faithful[1:4,]

knitr::kable(data,
             caption = "Table with kable")

For a nice-looking table, the package htmlTable is what you need.

library(htmlTalble)

htmlTable(data, caption = "Table with htmlTable")

2.3 Output text as raw Markdown content

By default, text output from code chunks will be written out verbatim with two leading hashes. If you would like to output text as Markdown content, simply insert results = 'asis' into the chunk option. This option tells knitr not to wrap your text output in verbatim code blocks, but treat it “as is.” This can be particularly useful when you want to generate content dynamically from R code.

E.g.,

```{r, results='asis'}
cat('# This is a header')
```

will output as:

This is a header

Rather than:

## # This is a header

A. Reference

  1. View markdown generated html in RStudio viewer
  2. Include code that does not run in Rpresentation Markdown
  3. R Markdown Cookbook