Use data from R cell in Python cell of databricks notebook

Viewed 56

Is this possible and if so how? As a simple example I might have an R cell like the following

%r
x <- rbinom(10, 100, .5)

How can I access x from a Python cell?

1 Answers

A small excerpt from the Databricks website - mix-languages

When you invoke a language magic command, the command is dispatched to the REPL in the execution context for the notebook. Variables defined in one language (and hence in the REPL for that language) are not available in the REPL of another language. REPLs can share state only through external resources such as files in DBFS or objects in object storage.

Some workarounds that I have seen on stackoverflow

  1. Save in file in DBFS and read it back in python cell.

  2. Other not tested/not recommended options.

    2.1. Set environmental variable in one language and then read the same in another language. 2.2. Set value as a spark - conf and then read back in another language cell.

The file option works well. The other options I have not been able to pass along reliably. Maybe it is just for me.

Please validate and see if it works for you...

Related