⇦ Back

Setup

macOS

  • Create an R Build System by creating a new Sublime-Build file:
    • Sublime-Build files tell Sublime Text how you want to run a script when you hit Ctrl+B
    • Tools > Build System > New Build System…
    • In the “untitled.sublime-build” that appears, replace everything with
    {
        "cmd"       : ["Rscript", "$file"],
        "selector"  : "source.shell",
    }
  • Save as “R.sublime-build” in the default location (which, on macOS, is /Users/<username>/Library/Application Support/Sublime Text 3/Packages/User)
  • Run R files with Ctrl+B

Ubuntu

  • Once R has been installed (sudo apt-get install r-base) it works in Sublime Text with Ctrl+B
  • If the R-IDE package is installed (it is needed if you want to work with R Markdown) this behaviour changes (the default build system becomes R-IDE and its ‘Exec’ function). To fix this, go to Tools > Build With… and select “R” to re-teach Sublime Text what build system should be used as the automatic one.

Linting

  • ‘Linting’ is the process of standardising the format of how your code looks and this can be achieved by using a linter. To read more about this and why it’s a good idea to have a linter, take a look here
  • You can download a linter for Sublime Text that will work with R code - along with many other languages - although I haven’t been able to get it working on Ubuntu 18.04 or 20.04 (it works fine on macOS Catalina though):
  1. Install the SublimeLinter package from Package Control (read here if you don’t have Package Control) in Sublime Text:
    • Using the mouse: Preferences > Package Control > Install Package > search for “SublimeLinter” > enter
    • Using the keyboard: Ctrl+Shift+P (Cmd+Shift+P on macOS) to open the Command Palette > search for “Package Control: Install Package” > enter > search for “SublimeLinter” > enter
  2. Because SublimeLinter is a linter framework and not a linter itself, you now need to install an R linter separately. And you need to install this linter both in R and in Sublime Text:
    • In R (eg in RStudio), install the lintr package with install.packages("lintr")
    • In Sublime Text, install the SublimeLinter-contrib-lintr package
      • Note that this is not always available in Package Control, so you can download it manually from here. Unzip it and move the folder into your packages folder (find where this is by opening Preferences > Browse Packages…).
  3. You now need to tell Sublime Text where your lintr R package is installed:
    • Open R and run .libPaths() to get the path to your R packages, including to lintr
    • Open the SublimeLinter settings (Preferences > Package Settings > SublimeLinter > Settings) and paste the following into the user settings (on the right-hand-side) between the curly brackets that will already be there:
    "paths": {
        "linux": [],
        "osx": [],
        "windows": []
    }
    • Paste the path to your R packages in as a string (ie with quotation marks around it) next to the relevant OS name
  4. To see the results of the linting every time you press save:
    • Open Preferences > Package Settings > SublimeLinter > Settings
    • Add to the user settings (on the right-hand-side): "show_panel_on_save": "view"

⇦ Back