⇦ Back

To render R Markdown documents that are open in Sublime Text.

Pre-Requisites

  • You need to have R installed on your computer

    • On Ubuntu you can do this from the terminal with:

      sudo apt install r-base-core
    • On macOS, download RStudio from its website.

    Check that it’s worked from the terminal with:

    R --version
  • You also need to have the R package rmarkdown installed. This can be done by opening R (eg by running R in the terminal) and executing:

    install.packages("rmarkdown")
  • If you will be running Python chunks in your R Markdown files you will need to have the reticulate package as well:

    install.packages("reticulate")

    It will be useful to have libpython, see the “Additional Setup” note below.

  • Pandoc must also be installed

    • On Ubuntu you can get this from the terminal:

      sudo apt-get install pandoc
    • On macOS it can be downloaded from its website.

    Check that you have it from the terminal with:

    pandoc --version
  • The R-IDE and LSP packages need to be installed in Sublime Text:

    • Command Palette > Package Control: Install Package > R-IDE
    • Command Palette > Package Control: Install Package > LSP

Additional Setup for Python Users:
If you run Python code in R Markdown it will use the python3 command in the background (ie it will use the same version of Python as when you run python3 in a terminal). This may be exactly what you want, in which case you can skip ahead. If you have multiple versions of Python installed, however, you might want to use a different version to that associated with python3, for example you might want to use Python 3.11 which is associated with the command python3.11. That can be achieved by adding the following chunk in your R Markdown file:

```{r, echo = FALSE}
library(knitr)
opts_chunk$set(engine.path = "/usr/bin/python3.11")
```

However, this might result in the following error:

Error: '/usr/bin/python3.11' was not built with a shared library.
reticulate can only bind to copies of Python built with '--enable-shared'.

As mentioned in the error message, your Python does not have a shared library. If you install Python from source (eg if you are on Ubuntu and you download it from the Python website) you should do so with the following step as part of the process in order to create a shared library:

./configure --enable-shared

On Ubuntu, if you install Python a different way (eg by using apt-get) then you can download the shared library libpython as an Ubuntu package, eg libpython3.11 for Python 3.11:

sudo apt-get install libpython3.11

Usage

  • To start with - just to check that it works - use R-IDE to render an R Markdown file:
    • Open an R Markdown file (one with .Rmd as the extension) in Sublime Text
    • From the menu bar, click R-IDE > Render R Markdown
    • This should cause a file to be created. The file type of this output will depend on what option was set with the output parameter in the R Markdown file’s YAML configuration (eg output: html_document will give you an HTMl file)
  • Now check to see if you can do this rendering by invoking a Sublime Text “Build System”:
    • From the menu bar, click Tools > Build. The keyboard shortcut for this is Ctrl+B (Ubuntu/Windows) or Cmd+B (macOS).
    • If this doesn’t work, you will need to create a Build System manually:

Creating an R Markdown Build System Manually

From the menu bar, go to Tools > Build System > New Build System. In the untitled.sublime-build file that appears, replace everything with:

{
    "cmd"       : ["Rscript", "-e", "rmarkdown::render('$file')"],
    "selector"  : "source.shell, source.Rmd",
}

Save as Rmarkdown.sublime-build in the default location. For the record, these default locations are:

  • Ubuntu: /home/<username>/.config/sublime-text/Packages/User
  • macOS: /Users/<username>/Library/Application Support/Sublime Text 4/Packages/User

To render with the Build System:

  • Open an R Markdown file with the .Rmd extension
  • From the menu bar, go to Tools > Build With…
  • From then on you can just build with Ctrl+B/Cmd+B as the default build system will have been set when you used “Build With…”

⇦ Back