Skip to content
Kun Ren edited this page Apr 30, 2022 · 12 revisions

R Markdown document creation is supported with the following features:

  • Chunk highlighting
  • Chunk navigation commands (Select current chunk, go to previous/next chunks, etc.)
  • Chunk execution commands (Run current chunk, run above/below chunks, etc.)
  • Rmd render commands (Knit, Knit to PDF/HTML/All)
  • Live preview rmd (as html)
  • Create new draft from a template

File association

By default, Rmd files are open as Markdown documents. To enable R Markdown features, you need to associate *.Rmd files with rmd language. Add an entry Item *.Rmd and Value rmd in the VS Code settings editor:

image

or directly update settings.json with the following:

{
   "files.associations": {
     "*.Rmd": "rmd"
  }
}

Installing pandoc

pandoc is required to render R Markdown documents. Visit Installing pandoc for instructions.

You may need to restart VS Code for rmarkdown to be able to detect pandoc.

Editing R Markdown documents

Kapture 2020-10-28 at 22 25 08

Knitting R Markdown documents

R Markdown documents can be knit in a VSCode-R managed background process when using the knit command. Beyond the use of a background process, this has the following features:

  • Set knitr document directory
  • Knit progress tracking
  • Smart knitting

Set knitr document directory

By default, knitr evaluates documents as though they are the root folder. For instance, calling source in a document will use the document's folder as the working directory, not the document's workspace. This can be modified in VSCode-R, so as to tell knitr if we want to evaluate from the workspace root or the document directory.

setDirectory

Progress tracking

Knit progress is tracked via a progress bar, and can be viewed in the R Markdown output stream. Progress is determined by the percentages reported in the R Markdown output stream and may not be 100% accurate.

image

Smart knitting

The knit button is "smart" in that it checks the document and its workspace for context when knitting. For instance, if a document has a knit function defined in the YAML header, the knit button will call the custom function when knitting. This also works for R Markdown sites, where the knit button will call rmarkdown::render_site instead of rmarkdown::render.

Live rendering R Markdown documents

You can live preview R Markdown documents by pressing the "Open Preview to Side" button when focused on an R Markdown document.

preview

You can also preview a document by right-clicking it in the explorer and selecting preview.

You can also run the following code and a browser viewer will open with live rendering of the R Markdown document.

rmarkdown::run("your-rmd-document.Rmd")

Live previewing rendered R Markdown documents

When an R Markdown document is knit and rendered to an HTML file, Live Preview can be used to live preview the HTML document. It supports auto-refresh when the HTML file is updated.

Templates

You can run "R Markdown: New Draft" from the command palettes to create a new draft from an R Markdown template.

The following is a (incomplete) list of packages that provide R Markdown templates: