About R at IU

On this page:


Overview

R is a language and environment for statistical computing and graphics. R provides a wide variety of statistical (for example, linear and nonlinear modeling, classical statistical tests, time-series analysis, classification, and clustering) and graphical techniques, and is highly extensible. For more, see the R Project for Statistical Computing home page.

Use R at IU

At Indiana University, R is available for use on personal computers via IUanyWare.

RStudio is available via the menu bar in Research Desktop (RED). For more about RED, including how to get access, see About Research Desktop (RED) at IU.

R is also available on the research supercomputers. To use R on an IU research supercomputer, you first must add the R module to your user environment; on the command line, enter:

module load r

RStudio graphic issues

If you encounter errors when generating graphics in RStudio on the Research Desktop (RED) environment, modify your RStudio preferences to switch from the default graphics device to Cairo.

To do this in RStudio:

  1. From the Tools menu, select Global Options.
  2. In the "Options" window, on the left, select General, and then, on the right, select Graphics.
  3. Under "Graphics Device", use the "Backend" drop-down to select Cairo, and then select OK.

Alternatively, to do this in RED, open a Terminal window, and then use your preferred editor to add the following line to your ~/.config/rstudio/rstudio-prefs.json file:

"graphics_backend": "cairo"

For example:

  • The default contents of the rstudio-prefs.json file should look similar to this:
    {
        "initial_working_directory": "~",
        "pdf_previewer": "none",
        "posix_terminal_shell": "bash"sr
    }
  • After adding the line above, the contents of the rstudio-prefs.json file should look similar to this:
    {
        "initial_working_directory": "~",
        "pdf_previewer": "none",
        "posix_terminal_shell": "bash",
        "graphics_backend": "cairo"
    }

Good practices in R programming

Following are guidelines and code examples that illustrate good practices in R programming:

  • Avoid unnecessary operators: R is an interpreted language; every operator in your R scripts requires a name lookup every time you use it.

    The following two code examples are functionally equivalent. However, the first code example takes about twice as much processing time due to the multiple parentheses.

    Example1 Example2
    system.time({ 
        I = 0
        while (I<100000) {
            ((((((((((10))))))))))
            I = I + 1
        }
    })
    user    system    elapse
    0.125   0.000     0.125
    system.time({ 
        I = 0
        while (I<100000) {
            10
            I = I + 1
        }
    })
    user    system    elapse
    0.055   0.000     0.055
  • Avoid growing objects inside loops: Always pre-allocate objects to be used inside loops. Executing loops in R is slow, and growing objects inside loops will make your R program particularly slow. You should always try to pre-allocate vectors, lists, and data frames accessed inside any loops.

    Consider the following two code examples. The first accesses and grows a vector inside the for loop while the second pre-allocates the vector and accesses the vector inside the for loop without growing its size.

    Example1 Example2
    square_loop_noinit <- function (n) {
        x <- c() 
        for (i in 1:n) {
            x <- c(x, i^2)
    }
    system.time({
        square_loop_noinit(200)
    })
    user    system    elapse
    0.257   0.000     0.257
    square_loop_noinit <- function (n) {
        x <- integer(n)
        for (i in 1:n) {
            x[i] <- i^2
    }
    system.time({
        square_loop_noinit(200)
    })
    user    system    elapse
    0.099   0.000     0.099
  • Use vectorization if possible: In R, everything is a vector. In your R script, you should always write vectorized code or use pre-existing compiled kernels (which are already vectorized and optimized) to avoid interpreter overhead.

    Consider the following two code examples. The second example achieves a 38-fold speedup by using vectorized code provided by compiled kernels.

    Example1 Example2
    Ply <- function(x) lapply (rep(1, 1000), rnorm)
    system.time({
        Ply()
    })
    user    system    elapse
    0.348   0.000     0.348
    vec <- function(x) rnorm(1000)
    system.time({
        vec()
    })
    user    system    elapse
    0.009   0.000     0.009

Get help

If you have questions about using R at IU or need help, contact the UITS Research Applications and Deep Learning team.

This is document avxb in the Knowledge Base.
Last modified on 2024-03-20 15:55:00.