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
If you subscribe to Colab Pro, terminal is now available. Just click the 'Terminal' icon on the left pane.
The Google Colaboratory (“Colab”) is a notebook (like a Jupyter Notebook) where you can run Python code in your Google Drive. You can write text, write code, run that code, and see the output – all in line in the same notebook.
To simply have all the cells run automatically, click Runtime > Run all in the colab toolbar.
Although Colab is primarily used for coding in Python, apparently we can also use it for R ( #Rstats ). This post wil l tell you how to run R in Google Colab and how to mount Google Drive or access BigQuery in R notebook. The first way is to use the rpy2 package in the Python runtime. This method allows you to execute R and Python syntax together.
In each Colab cell you usually type python commands but you can tell python to run a Linux command by adding a ! at the beginning of the command. For example: Then, click on the “Play” button on the left, or press Ctrl + Enter.
Google Colab is a free online Python notebook but you can use it like a regular server following these simple steps. Go to Google Colab and select “New notebook” (you need a Google account to use Colab). Now, click on top-right “Connect” button. By default, a CPU-only instance will be created.
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:
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")
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With