-1

I have following R code and I want to copy it on Jupyter notebook written in Kernel Python

data <- list('0.47', '-0.36', '-0.5', '0.2', '0.35', '1.82', 
             '-0.78', '-0.91', '0.36', '-1.74', '0.24', '0.76', 
             '0.57', '2.32', '1.55', '-1.31', '-0.09', '-0.02', 
             '-0.07', '-0.19', '-0.25', 
             '-1.09', '0.64', '1.22', '-0.56', '1.76', '0.13', 
             '1.33', '-0.74', '-1.15', '1.63', '1.04', '-0.26', 
             '0.02', '-1.2', '0.37', '0.43', '0.04', '1.34', 
             '0.57', '0.76', '-1.25', '-0.05', '0.12', '0.8', 
             '-0.99', '-0.11', '-0.54', '-0.08', '-0.04', '-0.76', 
             '-0.8', '0.35', '1.54', '-0.99', '-0.35', '-0.28', '0.45', 
             '-0.04', '-0.06', '0.02', '0.58', '-0.32', '-0.1', '0.28', 
             '0.3', '-0.36', '0.81', '0.79', '0.21', '1.81', '0.19', '0.84', 
             '0.2', '-0.06', '-0.11', '-1.4', '-2.08', '0.88', '-0.14',
             '-0.96', '1.3', '0.06', '-0.37', '1.49', '-0.91', 
             '1.14', '-1.05', '1.49', '-0.79', '2.02', '0.38', '2.4', '1.25', 
             '0.5', '1.11', '-0.54', '-0.1', '0.63', '1.01')
num <- as.numeric(unlist(data))
shapiro.test(num)

If I directly copy, the following message comes up

 File "<ipython-input-3-3bed828b881b>", line 17
    num <- as.numeric(unlist(data))
           ^
SyntaxError: invalid syntax


Is there a way to do so? Other parts are written in Python language

Mathilda Fang
  • 353
  • 1
  • 13
  • What are you trying to do here? – The Singularity Oct 04 '21 at 09:18
  • show the result computed by R in ipynb file – Mathilda Fang Oct 04 '21 at 09:19
  • Jupyter is not very versatile when it comes to using R and Python interchangeably. If you're looking for a versatile notebook that supports interchangeability between languages like Python, R, SQL and Scala I'd recommend you try the [Databricks Community Edition](https://community.cloud.databricks.com/login.html) – The Singularity Oct 04 '21 at 09:33

1 Answers1

2

Usually, Jupyter notebooks are using only one language at once: The one by the kernel (Python in your case).

You can use Script of Scripts (SoS) to make Jupyter polyglot to handle both python and R code in one Notebook. There is also reticulate and rpy2 to mix python and R code.

To show R results in Jupyter notebook, I recommend to export the R results in a language agnostic file format e.g. csv or json. These files can be easily imported in python afterwards.

danlooo
  • 10,067
  • 2
  • 8
  • 22
  • thank your for your reply @danlooo. No other ways? I thought there must be a way to mix R code and python code lmao. – Mathilda Fang Oct 04 '21 at 09:26
  • 1
    Rmarkdown is more versatile in this case compared to Jupyter notebooks. – danlooo Oct 04 '21 at 09:31
  • How about this link? Does this link tell us we can mix python and R. https://stackoverflow.com/questions/39008069/r-and-python-in-one-jupyter-notebook – Mathilda Fang Oct 04 '21 at 09:31
  • 1
    @MathildaFang I revised my answer a couple of minutes ago. Yes, rpy2 is another way. This is more of translation than just mixing. Crossing language boundaries makes the code slower and more difficult to debug. There is a price to pay when using multiple languages. – danlooo Oct 04 '21 at 09:33
  • Thank you very much. Another stupid question: I use macbook pro, when I type ```pip install rpy2``` or ```conda install -c r rpy2 ``` in terminal, it doesn't work. I mean, when I type in, there is no action of the code. why is it? can u help – Mathilda Fang Oct 04 '21 at 09:39
  • The python package rpy2 must be installed within the same python environment the kernel uses. Execute a cell in jupyter with this command: `! pip install rpy2` – danlooo Oct 04 '21 at 09:42
  • it doesn't work... I tried. I will ask another question with screenshot and you are welcome to answer it. big thanks @danlooo – Mathilda Fang Oct 04 '21 at 09:43
  • 1
    Don't do screenshots. We can copy and search text much easier. – danlooo Oct 04 '21 at 09:46
  • I asked another question. Big thanks. I have been struggling. – Mathilda Fang Oct 04 '21 at 09:47