How to access the shell in google Colab when running the R kernel

Viewed 384

When I use Python with colab, you can access the underlying operating system using things like ! and %%shell, e.g., !ls to list the files.

When I use colab with the R kernel, the ! and %%shell tricks don't work. Is there a way to call the shell from colab in this case?

https://colab.research.google.com/#create=true&language=r

2 Answers

There is the base R function system which allows you to call the shell behind the Notebook. Since colab suppresses the stdout, one needs to set the option intern = TRUE to see the result as an R character vector. To properly display line breaks, I defined a function called shell_call which is analog to ! in ipython:

shell_call <- function(command, ...) {
  result <- system(command, intern = TRUE, ...)
  cat(paste0(result, collapse = "\n"))
}

shell_call("ls -lah / | head")

enter image description here

The implementation of magics is kernel specific as explained here:

To Jupyter users: Magics are specific to and provided by the IPython kernel. Whether Magics are available on a kernel is a decision that is made by the kernel developer on a per-kernel basis. To work properly, Magics must use a syntax element which is not valid in the underlying language. For example, the IPython kernel uses the % syntax element for Magics as % is not a valid unary operator in Python. However, % might have meaning in other languages.

R does not offer good conditions to implement magics this way as for example %% is used as the modulo operator and ! as the logical NOT operator.

This is why the developers of the IRkernel have chosen not to support %%cell magic - see this or this.

As already outlined in the comments when working with the IRkernel you'll need to find solutions in the R language itself e.g. using built-in functions to access the shell system2("ls", stdout = TRUE) or search for files list.files() (you might also want to wrap them in a custom function as suggested by @danlooo).

If you still want to use magics another approach would be to use R magics (extension rmagic) along with the IPython kernel as shown here or here:

result

Related