我尝试在RStudio中使用Python。通过R使用Python模块效果很好。
```{r}
library(reticulate)
py_install("pandas")
pd = import("pandas", convert = FALSE)
r_mtcars = pd$DataFrame(mtcars)
r_mtcars$describe()
```
mpg cyl disp ... am gear carb
count 32.000000 32.000000 32.000000 ... 32.000000 32.000000 32.0000
mean 20.090625 6.187500 230.721875 ... 0.406250 3.687500 2.8125
std 6.026948 1.785922 123.938694 ... 0.498991 0.737804 1.6152
min 10.400000 4.000000 71.100000 ... 0.000000 3.000000 1.0000
25% 15.425000 4.000000 120.825000 ... 0.000000 3.000000 2.0000
50% 19.200000 6.000000 196.300000 ... 0.000000 4.000000 2.0000
75% 22.800000 8.000000 326.000000 ... 1.000000 4.000000 4.0000
max 33.900000 8.000000 472.000000 ... 1.000000 5.000000 8.0000
[8 rows x 11 columns]
但是,如果我尝试通过Python使用Python模块,则会出现错误。
```{python}
import pandas
py_mtcars = r.mtcars
```
Traceback (most recent call last):
File "...Temp\RtmpUzhmLU\chunk-code2a0865ca6eb6.txt", line 4, in <module>
py_mtcars = r.mtcars
NameError: name 'r' is not defined
r.
通常用于告诉Python mtcars
是R源。
另一种方式也是不可能的,即通过R访问Python数据。
```{python}
import pandas
mtcars = pandas.read_csv("../PyR/mtcars.csv")
```
```{r}
py$mtcars
```
Error in py_get_attr_impl(x, name, silent) : AttributeError: module '__main__' has no attribute 'mtcars'
有关Python版本的信息:
py_config()
python: ~\Anaconda3\envs\r-reticulate\python.exe
libpython: ~/Anaconda3/envs/r-reticulate/python36.dll
pythonhome: ~\ANACON~1\envs\R-RETI~1
version: 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)]
Architecture: 64bit
numpy: ~\ANACON~1\envs\R-RETI~1\lib\site-packages\numpy
numpy_version: 1.14.3
pandas: ~\ANACON~1\envs\R-RETI~1\lib\site-packages\pandas\__init__.p
编辑:
library(reticulate); devtools::session_info()
- Session info --------------------------------------------------------------------------------------------
setting value
version R version 3.5.2 (2018-12-20)
os Windows >= 8 x64
system x86_64, mingw32
ui RStudio
language (EN)
collate German_Germany.1252
ctype German_Germany.1252
tz Europe/Berlin
date 2019-04-19
- Packages ------------------------------------------------------------------------------------------------
package * version date lib source
assertthat 0.2.0 2017-04-11 [1] CRAN (R 3.5.0)
backports 1.1.3 2018-12-14 [1] CRAN (R 3.5.2)
callr 3.1.1 2018-12-21 [1] CRAN (R 3.5.2)
cli 1.0.1 2018-09-25 [1] CRAN (R 3.5.2)
crayon 1.3.4 2017-09-16 [1] CRAN (R 3.5.0)
desc 1.2.0 2018-05-01 [1] CRAN (R 3.5.2)
devtools 2.0.1 2018-10-26 [1] CRAN (R 3.5.2)
digest 0.6.18 2018-10-10 [1] CRAN (R 3.5.2)
fs 1.2.6 2018-08-23 [1] CRAN (R 3.5.2)
glue 1.3.0.9000 2019-01-28 [1] Github (tidyverse/glue@8188cea)
jsonlite 1.6 2018-12-07 [1] CRAN (R 3.5.2)
lattice 0.20-38 2018-11-04 [2] CRAN (R 3.5.2)
magrittr 1.5 2014-11-22 [1] CRAN (R 3.5.0)
Matrix 1.2-15 2018-11-01 [2] CRAN (R 3.5.2)
memoise 1.1.0 2017-04-21 [1] CRAN (R 3.5.0)
pkgbuild 1.0.2 2018-10-16 [1] CRAN (R 3.5.2)
pkgload 1.0.2 2018-10-29 [1] CRAN (R 3.5.2)
prettyunits 1.0.2 2015-07-13 [1] CRAN (R 3.5.1)
processx 3.2.1 2018-12-05 [1] CRAN (R 3.5.2)
ps 1.3.0 2018-12-21 [1] CRAN (R 3.5.2)
R6 2.3.0 2018-10-04 [1] CRAN (R 3.5.2)
Rcpp 1.0.0 2018-11-07 [1] CRAN (R 3.5.2)
remotes 2.0.2 2018-10-30 [1] CRAN (R 3.5.2)
reticulate * 1.10.0.9003 2019-01-28 [1] Github (rstudio/reticulate@6a60dad)
rlang 0.3.1 2019-01-08 [1] CRAN (R 3.5.2)
rprojroot 1.3-2 2018-01-03 [1] CRAN (R 3.5.0)
rstudioapi 0.9.0 2019-01-09 [1] CRAN (R 3.5.2)
sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.5.2)
testthat 2.0.1 2018-10-13 [1] CRAN (R 3.5.2)
usethis 1.4.0 2018-08-14 [1] CRAN (R 3.5.2)
withr 2.1.2 2018-03-15 [1] CRAN (R 3.5.0)
yaml 2.2.0 2018-07-25 [1] CRAN (R 3.5.1)
[1] ~/Documents/R/win-library/3.5
[2] C:/Program Files/R/R-3.5.2/library
StackOverflow说我的帖子中有很多代码。因此,东方好人,感谢您的帮助。我希望我们能完成它。
答案 0 :(得分:0)
我认为您只能访问r
魔术变量 R降价后的python块内。
以下.Rmd
可以正常编译,并在R数据帧pandas.DataFrame.describe
上运行Python函数r.iris
。
---
output: html_document
---
```{r}
library(reticulate)
```
```{python}
import pandas as pd
pd.DataFrame.describe(r.iris)
```
您似乎希望独立的Python脚本应该能够访问名为r
的魔术变量。
我怀疑不是reticulate's features之一,而是{引用文档):
最后一点提到“块”使我相信这是仅Rmarkdown功能。
如果在R markdown之外使用来自Python的R对您很重要,我建议rpy2。