R
Online R Compiler
Your script runs with Rscript on R 4.5, with base R and its built-in datasets. It can read input while it runs: open file("stdin") and readLines() waits for you to type in the terminal.
Try it aboveHow R runs here
- Versions
- R 4.5
- Run
Rscript main.R- Limits per run
- 256 MB memory, 15 s of CPU time on half a core, 2 MB of output
- Time
- Up to 10 minutes a run (30 on a paid plan); waits 2 minutes for input (5 on a paid plan)
- Sandbox
- Each run gets its own Linux sandbox with no network access. Text files your program writes next to your code open as new tabs.
An R script that reads numbers and summarizes them
This main.R reads one line of scores, converts it to a numeric vector and prints a few statistics. Everything after the input line works on the whole vector at once:
con <- file("stdin", "r") cat("Enter scores separated by spaces: ") scores <- as.numeric(strsplit(trimws(readLines(con, n = 1)), " +")[[1]]) close(con) cat(sprintf("Count: %d\n", length(scores))) cat(sprintf("Mean: %.2f, median: %.1f, sd: %.2f\n", mean(scores), median(scores), sd(scores))) above <- scores[scores > mean(scores)] cat(sprintf("Above the mean: %s\n", paste(above, collapse = " "))) print(sort(scores))
Five scores typed on one line:
$ Rscript main.R Enter scores separated by spaces: 72 88 55 91 64 Count: 5 Mean: 74.00, median: 72.0, sd: 15.41 Above the mean: 88 91 [1] 55 64 72 88 91
Reading input in R
Rscript isn’t an interactive R session, and that changes how input works:
- readline("Name: ") doesn’t wait. Outside an interactive session it returns "" at once, as if you had pressed Enter. Use readLines() on file("stdin") instead.
- Open the connection once and keep reading from it, as in the example below, rather than calling file("stdin") again for every line.
- Always pass n. readLines(con) with no n reads until the end of input, and the terminal never sends one (there’s no Ctrl+D), so the script waits until the input time runs out.
- Input arrives as text. Convert it with as.numeric() or as.integer(), and split a line of several values with strsplit() first.
con <- file("stdin", "r") # open once, read many times cat("Your name: ") name <- readLines(con, n = 1) cat("Your age: ") age <- as.integer(readLines(con, n = 1)) cat(sprintf("%s will be %d next year\n", name, age + 1L))
Scripts in several files
Rscript runs one file: the open tab, or the Entry file set in Run settings. Other files sit in the same working folder, so source() them by name:
# helpers.R zscore <- function(x) (x - mean(x)) / sd(x) # main.R source("helpers.R") print(round(zscore(c(2, 4, 6)), 2)) # [1] -1 0 1
Data files work the same way. Upload a CSV next to main.R and read it with read.csv("data.csv"). Text files your script writes with write.csv() or writeLines() open as new tabs after the run.
What isn’t available here
- CRAN packages. ggplot2, dplyr, tidyr, readr, data.table and the rest of the tidyverse aren’t installed, and install.packages() can’t download anything because runs have no network.
- The recommended packages too. MASS, lattice, Matrix, survival and nlme aren’t there. You get R’s base packages: base, stats, utils, methods, datasets, graphics, grDevices, grid, splines, stats4, parallel, tools and compiler.
- Charts. The playground shows text only. plot() and hist() draw to a file, Rplots.pdf, and the playground doesn’t display it. Use summary(), table(), quantile() and printed values instead.
- An interactive console. There’s no > prompt between lines: the whole script runs from top to bottom.
Errors you’ll see
- “there is no package called ‘ggplot2’” from library(ggplot2). The package isn’t installed. Rewrite the step with base R, for example aggregate() instead of dplyr’s group_by() and summarise().
- “could not find function "mutate"” A function from a package that isn’t loaded, or a typo in the name.
- “object 'totl' not found” A variable that was never assigned, often a typo or a different capital letter. R is case-sensitive.
- “non-numeric argument to binary operator” Arithmetic on text, typically a value from readLines() that wasn’t converted with as.numeric().
- Warning “NAs introduced by coercion” as.numeric() met something that isn’t a number, such as "12,5" or "n/a". The value becomes NA; check with is.na().
When a tidyverse snippet fails here, Fix with AI under the failed run can rewrite it in base R as a diff you review.
R 4.5 in practice
- The datasets package is loaded, so mtcars, iris, airquality and ToothGrowth are there to practice on: try aggregate(mpg ~ cyl, data = mtcars, FUN = mean).
- Top-level values print themselves, but not inside a loop or function. Wrap them in print(), or format with cat(sprintf(...)) for exact output.
- The native pipe |> and the \(x) lambda shorthand work, so mtcars |> subset(cyl == 4) |> nrow() is plain base R.
- set.seed(1) before sample() or rnorm() makes random results repeat from run to run.
- The %||% operator (a default for NULL) is part of base R since 4.4.
Questions
Which version of R does this use?
R 4.5. Your file runs with Rscript main.R, as a script rather than an interactive session.
Can I install packages like ggplot2 or dplyr?
No. Only R’s base packages are installed, and runs have no network, so install.packages() fails.
Why doesn’t readline() wait for my input?
Under Rscript, readline() returns an empty string straight away. Read a line with readLines(file("stdin"), n = 1) instead.
How do I give several inputs in R online?
Open con <- file("stdin", "r") once, then call readLines(con, n = 1) for each line and type the answers in the terminal.
Can I see plots?
No. Output is text: plot() writes Rplots.pdf, which the playground doesn’t display.
Are datasets like mtcars and iris available?
Yes. The datasets package comes with base R, so you can use them without loading anything.