Skip to content

R in Basthon ​

Open the console or the notebook in R (URL parameter kernel=r). Examples are in the gallery.

Results are shown as in the R console, and charts (plot, hist, barplot…) appear directly below the cell (or in the graphical output of the console). Missing packages are downloaded automatically at the first library():

r
library(dplyr)
mtcars |> group_by(cyl) |> summarise(mpg = mean(mpg))
# A tibble: 3 × 2
    cyl   mpg
  <dbl> <dbl>
1     4  26.7
2     6  19.7
3     8  15.1

R's help is still available with ?mean or help("mean"), and readline() asks the user for an input.

What you can do ​

Statistics on a data frame ​

A data.frame, its statistics and its charts, as in R on a computer (try it, post):

r
class <- data.frame(
  name = c("Ada", "Alan", "Grace", "Linus"),
  maths = c(17.5, 15.25, 18, 12.75),
  cs = c(19, 16.5, 17.5, 14)
)
class$mean <- (class$maths + class$cs) / 2
summary(class$mean)
barplot(class$mean, names.arg = class$name, col = "steelblue")
plot(class$maths, class$cs, pch = 19)
abline(lm(cs ~ maths, data = class), lty = 2)

A chart can be completed in a later cell (abline, legend…): it is then shown again.

Datasets and packages ​

R's classic datasets (iris, mtcars…) are available, and packages such as dplyr or ggplot2 are loaded with library() (see the example above). The introduction notebook tours the kernel: computations, vectors, functions, data frames, charts, packages and keyboard input (try it).

Differences with R on a computer ​

Basthon adds no function to R, which runs here thanks to webR:

  • packages are downloaded automatically at the first library(), from webR's repository (most of CRAN's packages, but not all);
  • charts are shown below the cell (or in the graphical output of the console), as images;
  • readline(), scan() and menu() ask the user for an input;
  • a file cannot be downloaded yet.

Completion ​

The Tab key uses R's completer: objects, arguments of functions (sum(1, na.r gives na.rm=), elements of a list (car$m), functions of a package (utils::he), file names in a string.

Files and modules ​

Extra files are available in the filesystem (read.csv…). The open button (or the module URL parameter) installs *.R files, to load with source("my_module.R").

Downloading a file ​

The R kernel cannot download a file for now.