Do Notebooks support other languages?

Follow

 

Notebooks and R Markdown support other languages. Full details of Notebook support are available in the article on Knitr Language Engines, as well as the knitr documentation. To create a code chunk with a different language, you can select an option from insert chunk:



 

Execution Model:

Execution of other languages is by done by: starting the relevant interpreter, submitting the code inside the chunk, and exiting. For instance, a python code chunk like:

a = dict()
a["test"] = [1,2,3]
print(a)

will roughly be translated into:

$ python -c 'a=dict(); a["test"]=[1,2,3]; print(a)'

R Markdown listens for output or errors from this process. In the example above, the Notebook would catch the output of print(a) and display the result.

The engine.path chunk option controls which python install to use. For example:

```{python engine.path = "/usr/bin/python3"}
import sys
print(sys.version)
```

 This is how you would make use of an Anaconda distribution. At this time, there is not support for virtualenv.

Note that RStudio does not ship with any other language engines. This feature requires the relevant engine to be already installed and configured on the machine.

Limitations:

  • Each code chunk is submitted independently. For example, an object created in one python code chunk is not accessible in a later python code chunk.
  • Each code chunk is submitted all at once. You cannot run individual lines of a code chunk interactively.
  • Only printed outputs or errors are captured and placed into the notebook. For example, if you define a python object and want to use it later, you need to save it to disk.  If you create a plot in python (i.e., with matplotlib) you must save it to disk and then load it into the notebook with R.
  • Objects from other languages are not available in the environment pane.
  • Not all knitr languages are currently supported with interactive chunk execution in the Notebook. 

Supported Features:

  • Syntax highlighting
  • Code snippets
  • Chunk options. For example, chunks with eval=FALSE will not be executed.  

Special Exceptions:

C++, SQL, Javascript, CSS, and stan code chunks are handled uniquely, as outlined in the article on Notebook support for knitr Language Engine. For example, the results of a SQL query can be assigned to a variable in R for use in later chunks. Likewise, C++ code is compiled via Rcpp, and the C++ functions are available for subsequent use in later R chunks.

Comments