Difference between revisions of "Rstudio"

From 太極
Jump to navigation Jump to search
Line 4: Line 4:
== Cloud ==
== Cloud ==
https://rstudio.cloud/. You can sign up using gmail or github account.
https://rstudio.cloud/. You can sign up using gmail or github account.
Upload a file: Click 'Files' under the bottom right pane (Files-Plots-Packages-Help-Viewer). There is an "upload" button next to the "New folder" button over there.
== Desktop ==
== Desktop ==

Revision as of 11:22, 13 January 2020

Different tastes



https://rstudio.cloud/. You can sign up using gmail or github account.

Upload a file: Click 'Files' under the bottom right pane (Files-Plots-Packages-Help-Viewer). There is an "upload" button next to the "New folder" button over there.


Build from source See https://github.com/rstudio/rstudio/blob/master/INSTALL for installation instruction.

To build Rstudio from source in Ubuntu:

  • Install R.
  • Install Qt 4.
  • Download a release version tarball of source code. Do not grab from github master branch. Sometimes the preview version works but the release version cannot be compiled. For example, the release version 0.98.507 would give a build error from /opt/QRstudio-QtSDK/Desktop/4.8.0/gcc/include/QtCore/gstring.h, but the preview version 0.98.836 (May 11th, 2014) works fine.
  • Make sure git is installed/available.
  • Install dependencies (note that installing boost will take a few minutes. Make sure there are 2GB memory or will get a memory related error when running make install)
cd rstudio-rstudio-dbf531d  # the number at the end depending on the source
cd dependencies/linux
  • Create build directory
mkdir build
cd build
  • Configure by running cmake

If something is wrong related to boost library, go back to step 1. Then type (installing boost library takes a while)

cd dependencies/linux 
run ./install-boost

And for some reason, when I try to run cmake (OS is 32-bit LXLE running on VM), I get the following error

CMake Error at /usr/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:108 (message):
  QT_QTDBUS_INCLUDE DIR QT_QTDBUS_LIBRARY) (found suitable version "4.8.6", minimum required
  is "4.8.0")
  • Build and Install
sudo make install

For RStudio Desktop on Linux, make install automatically creates an entry in the Applications -> Programming menu for RStudio. The rstudio can be launched by /usr/local/lib/rstudio/bin/rstudio.



RStudio Theme

Background color (v1.1.383, see an animation on this post)

There are 3 choices of RStudio theme (Using RStudio Themes):

  • Classic + selected Editor theme: Code and Console/Terminal are dark, Environment/history/connections and Files/Plots/Packages/Help/Viewer are white (good when doing finding). But the 'R Markdown' tab does not show a working rotation symbol.
  • Modern: 4 panels are all dark (my choice).
  • Sky: 4 panels are all dark

One good-looking option is also change the layout like this one. So source code take half and console and plot each takes a quarter of the screen.

Background color of plot pane becomes black. It turns out the reason is the user is using a transparent background in his plotting routine.

Custom Theme support was added in RStudio v1.2.

Editor theme

I pick "Vibrant Ink".


View -> Hide Toolbar

Change repository

R mirrors

  • chooseCRANmirror(): this actually changes the CRAN mirror. It is done mainly interactively.
  • Non-interactive method.
      r <- getOption("repos")
      r["CRAN"] <- "https://mirror.las.iastate.edu/CRAN"
      options(repos = r)
  • setRepositories(ind=1:2) (both non-interactive or interactive): this allows to install packages from Bioconductor. After the change, we can use RStudio or the command install.packages() to install R package from Bioconductor. See setRepositories documentation.
1: + CRAN
2: + BioC software
3:   BioC annotation
4:   BioC experiment
5:   CRAN (extras)
6:   Omegahat
7:   R-Forge
8:   rforge.net

In my case, stringr package source code in cloud.r-project.org (default in RStudio or 0-Cloud from chooseCRANmirror()) is 1 week old. That version of source code is now archived and the URL shown from the output of install.packages() cannot be found. When I switch to USA-IA mirror, it has the latest version.

Use options("repos") to see its effect.

                                        CRAN                                     BioCsoft 
       "https://mirror.las.iastate.edu/CRAN" "https://bioconductor.org/packages/3.7/bioc"

Change R version



On Linux, the default R can be found by which R in the command line. If you want to override which version of R is used then you can set the RSTUDIO_WHICH_R environment variable to the R executable that you want to run against. For example:

export RSTUDIO_WHICH_R=/opt/R/3.5.0/bin/R

Not that in order for RStudio to see this environment variable when launched from the Ubuntu desktop Applications menu (as opposed to from a terminal) it must be defined in the ~/.profile file.


Using multiple versions of R with RStudio Server Pro


$ pandoc --version
pandoc 2.7.3

$ /Applications/RStudio.app/Contents/MacOS/pandoc/pandoc -v
pandoc 2.3.1

To use the system's pandoc, create/modify project's .Renviron file and include a line to something like


Get help on firefox/chrome browser

(The help panel is kind of small)

No, it can't. But alternatively, if we click "Show in a new window" icon, it will bring the help on an independent window.

> getOption("browser")
function (url) 
    .Call("rs_browseURL", url)
<environment: 0x1121bda60>


R Studio Shortcuts and Tips


Click a small icon/button at the top-right corner of the code panel.

  • For an Rmd file, it gives an online (all headers) of the file.
  • For an R file, it list all functions in the document.

Pop out the source pane to have its own window

Drag the tab outside the RStudio window to where you want your new code window to appear, or Click the "Show in new window" button on the editor toolbar (it's next to forward/back)

However, it seems we cannot move the popped out pane back to RStudio. If we just want to maximize one pane, we can use the keyboard shortcut "Ctrl+Shift+1" (source pane) or "Ctrl+Shift+2" (console pane); see Zoom in one pane.

Column selection

Click Alt.

Menu -> Code

  • For R code files *.R, it has "Insert Section" (a recommended way to insert a new section)
  • For R markdown files *.Rmd, it has "Insert Chunk" (just a short cut)

Restart R

Three ways

  • Session -> Restart R
  • .rs.restart()
  • command/ctrl + shift + F10

Note Restarting R in RStudio won't delete/clear workspace/objects but it unloads R packages.

We can use Session -> Clear Workspace or the command rm(list=ls()) to delete all objects.


  • https://support.rstudio.com/hc/en-us/articles/204463668-Code-Snippets?mobile_site=true
  • If you type 'ts' into the console, it should pop up a list. However, the default completion delay is 3 characters, meaning it will not work unless your completion delay is set to 2 characters which you can change in your global options (Code > Completion). This works on R console or an R/Rmd file.
    • If you don't want to change your default delay you can just type ts and hit tab.


On my Mac, the crash report is saved at ~/Library/Logs/DiagnosticReports/rsession_XXXX.crash. See also RStudio Crash Report

Install on Crouton

Test on my Asus (intel CPU) Chromebox/Crouton with Ubuntu 14.04.

(trusty)[email protected]:~$ sudo dpkg -i '/tmp/mozilla_brb0/rstudio-1.0.44-amd64.deb' 
Selecting previously unselected package rstudio.
(Reading database ... 133157 files and directories currently installed.)
Preparing to unpack .../rstudio-1.0.44-amd64.deb ...
Unpacking rstudio (1.0.44) ...
dpkg: dependency problems prevent configuration of rstudio:
 rstudio depends on libjpeg62; however:
  Package libjpeg62 is not installed.

dpkg: error processing package rstudio (--install):
 dependency problems - leaving unconfigured
Processing triggers for shared-mime-info (1.2-0ubuntu3) ...
Processing triggers for hicolor-icon-theme (0.13-1) ...
Processing triggers for gnome-menus (3.10.1-0ubuntu2) ...
Processing triggers for desktop-file-utils (0.22-1ubuntu1) ...
Processing triggers for bamfdaemon (0.5.1+14.04.20140409-0ubuntu1) ...
Rebuilding /usr/share/applications/bamf-2.index...
Processing triggers for mime-support (3.54ubuntu1.1) ...
Errors were encountered while processing:

After I run sudo apt-get install libjpeg62, RStudio can be installed successfully.

Switch R version for RStudio desktop


In Linux desktop, we need to run the following (or include the line in ~/.profile file for a permanent solution) before calling 'rstudio' in a terminal.

export RSTUDIO_WHICH_R=/opt/R/3.5.0/bin/R

Stop a command

  • Use the stop sign
  • Use "Interrupt R" under Session menu.

See Kill a process in R with RStudio


Scroll different tabs using the mouse wheel after you click any one of tabs.

Keyboard shortcut

  • https://support.rstudio.com/hc/en-us/articles/200711853-Keyboard-Shortcuts (Help -> Keyboard Shortcut Quick Reference)
  • Customize the shortcuts.
  • https://www.dropbox.com/s/0moch6xocili6v9/RStudio%20Shortcuts.pdf?dl=0
  • Knit an rmarkdown file
    • Mac: Shift + Cmd + k
    • Linux: Shift + Ctrl + k
  • (Aug 16, 2018) for some reason, run current line (CMD+Enter) will send random lines. It does not help if I assign a different key-bind. Update RStudio does not help either.
    • One way it still works is by selecting line(s) first and hitting the key-bind.
    • Another way is to go to Tools > Global options > Code. Change to Current line from the dropdown list next to CMD + Enter executes. The default is "Multiple-line R statements" which should be better since an R statement can spread several lines. The 3rd option "Multiple consecutive R lines" will send a block of R lines automatically which does not look ideal in most cases.

Move focus to source/console

  • Ctrl + 1: source
  • Ctrl + 2: console

These are the default.

Zoom in/out on one pane/window

  • Ctrl + Shift + 1: source
  • Ctrl + Shift + 2: console
  • Ctrl + Shift + 3: file viewer
  • Ctrl + Shift + 4: environment
  • Ctrl + Shift + 0: show all panes

Show previous/next Plot

By default, they are "Ctrl + Alt + F11" and "Ctrl + Alt + F12". But they have a conflict with other shortcuts in RStudio IDE itself or with the (Linux/Mint) system. In my case, Ctrl+Alt+F11 will escape from Linux Desktop:( Also the Quick Reference shows these bindings had a conflict: Ctrl + Alt + F11 represents Previous Terminal or Previous Plot.

I change the shortcuts to Alt+Left and Alt+Right (directly use keyboard to change it). It works but there is a delay if the plot is more complicated (6 seconds on a 10000 genes heatmap). These new assignment works on Mac and Linux.

Navigate previous commands

  • Cmd + up/down arrow keys (Mac)
  • Ctrl + up/down arrow keys (Linux)

Fix code spacing, reindent lines

CMD + i

History panel

If we want to rerun lines from the history panel, just highlight the lines and then either click the "To Console" button or press the "Enter" key.

See Using Command History from RStudio.

RStudio server


Multiple Rstudio Server instances using a single R/LaTeX install with KVM

RStudio Server Installation on Ubuntu


RStudio Server on Amazon EC2

RStudio and Git

Plots window

Plot does not show up in RStudio's plot window

options(device = "RStudioGD") # or dev.off()

Overlap problem



Using Projects

Working with Multiple Projects at Once

Script for creating a new project in Rstudio

https://stackoverflow.com/a/26127716. Below is a modified with my fix. It will generate a new file XXX.Rproj under the current working directory.

myProject <- function(proj, ...) {

    #create.project(proj, ...)

    x <- c("Version: 1.0", "", "RestoreWorkspace: Default", "SaveWorkspace: Default", 
        "AlwaysSaveHistory: Default", "", "EnableCodeIndexing: Yes", 
        "UseSpacesForTab: Yes", "NumSpacesForTab: 4", "Encoding: UTF-8", 
        "", "RnwWeave: knitr", "LaTeX: pdfLaTeX")

    #cat(paste(x, collapse="\n"), file=file.path(proj, paste0(basename(proj), ".Rproj")))
    cat(paste(x, collapse="\n"), file=paste0(basename(proj), ".Rproj"))
    message(paste0(basename(proj), ".Rproj has been created"))

myProject("MyNewProject") # It will generate the file <MyNewProject.Rproj>.

Running A Data Project In R


  • Use R projects. Always
  • Describe the purpose of your code / project.
  • Load all necessary packages in the beginning of your script & consider calling functions in a package via ::
  • Name your code sections and use them for quick navigation
  • Make your life easier and mark your code when you need to
  • Write your code as if someone was going to use it without communicating with you. From. Day. One
  • Name your files like a Pro: Machine readable, human readable & play well with ordering
    • Put something numeric in your file name first.
    • use the YYYY-MM-DD format for dates
    • regular expression and globbing friendly
    • easy to compute on using delimiters
    • left pad other numbers with zeroes
  • With big, complex data projects use project pipeline
    • load.R
    • clean.R
    • func.R
    • do.R
  • Never save your workspace
  • Before publishing/sharing your code, run it in the fresh workspace

Comment, navigation and code folding

The best practice to include a comment for a block of code is

  • Code > Insert Section (this will create something like # Section One --------------------------------- )
  • At least one number sign (pound sign) before the title PLUS at least 4 of number signs/dash signs/equal signs at the end

Good examples

#### My section ####

# My section ####

# My section ----

Bad example 1 (this will show 2 lines of (Untitled) in RStudio nagivation)

# Section 1

Bad example 2 (this will not show anything in the navigation)

# Section 2

# Section 2 #

# Section 2 ##

# Section 2 ###


R and Python: How to Integrate the Best of Both into Your Data Science Workflow



Non-invasive pretty printing of R code https://github.com/r-lib/styler


facilitate writing in markdown with RStudio




GGplot2 > esquisse




rstudio → run-diagnostics

Package Manager