Jupyter 笔记本上的 R 脚本

R script on Jupyter notebook

我有以下 R 代码,我想将它复制到用内核编写的 Jupyter 笔记本上 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)

如果我直接复制,会出现下面的信息

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


有办法吗?其他部分用Python语言

编写

通常,Jupyter notebooks 一次只使用一种语言:内核语言(Python 在你的例子中)。

您可以使用 Script of Scripts (SoS) to make Jupyter polyglot to handle both python and R code in one Notebook. There is also reticulate and rpy2 混合使用 python 和 R 代码。

要在 Jupyter notebook 中显示 R 结果,我建议以与语言无关的文件格式导出 R 结果,例如csv 或 json。之后可以在 python 中轻松导入这些文件。