0

I try to use Python in RStudio. Using Python modules by R works fine.

```{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]

However, if I try to use Python modules by Python I get an error.

```{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. is normally used to tell Python that mtcars is a R source.

The other way is also not possible i.e. access Python data by R.

```{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'

Information on Python version:

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

EDIT:

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 says I have to much code in my post. So, nice Eastern and thanks for your help. I hope we get it done.

Banjo
  • 1,191
  • 1
  • 11
  • 28
  • 1
    as the error says `r.` is not an object. You may have to use `rpy2` or `pyper` – akrun Apr 18 '19 at 14:08
  • if you're referring to the use of the `r` object mentioned here https://rstudio.github.io/reticulate/ then you should know that this object only exist if you're running python code with reticulate in rstudio ( e.g. a python chunk in an rmd file, or python code in a `repl_python()` shell) – DS_UNI Apr 18 '19 at 14:24
  • If I look at this Rpubs post for example there is no need to convert R data. https://rpubs.com/mattBrown88/407460. So something is still missing. – Banjo Apr 19 '19 at 11:11
  • I suggest you drop the first code chunk and remove all mention of "dataframe" from the question. A minimal example of your problem is simply a Python chunk saying `r`, which will generate the NameError. Also, make sure you load only `reticulate` before running `session_info()`, so you don't get noise from unrelated packages. You could also try debugging in a clean env where you just install Python, RStudio and reticulate. – Jon Olav Vik Apr 19 '19 at 12:39
  • Also, which version of RStudio are you using? I have 1.1.463 (and don't experience your problem), but this post says some reticulate features require Rstudio 1.2: https://stackoverflow.com/a/53462977/521493 – Jon Olav Vik Apr 19 '19 at 14:46
  • `RStudio.Version() $version [1] ‘1.1.463’` – Banjo Apr 19 '19 at 16:14

1 Answers1

1

I think you can only access the r magic variable inside python chunks in your R markdown. The following .Rmd compiles fine, running the Python function pandas.DataFrame.describe on the R data frame r.iris.

---
output: html_document
---

```{r}
library(reticulate)
```

```{python}
import pandas as pd
pd.DataFrame.describe(r.iris)
```

You seem to expect that your standalone Python scripts should be able to access the magic variable called r. I suspect that is not one of reticulate's features, which are (quoting the docs):

  • Run Python chunks in a single Python session embedded within your R session (shared variables/state between Python chunks)
  • Printing of Python output, including graphical output from matplotlib.
  • Access to objects created within Python chunks from R using the py object (e.g. py$x would access an x variable created within Python from R).
  • Access to objects created within R chunks from Python using the r object (e.g. r.x would access to x variable created within R from Python).

The mention of "chunks" in the last point makes me believe this is an Rmarkdown-only feature.

If using R from Python outside R markdown is important to you, I recommend rpy2.

Jon Olav Vik
  • 1,421
  • 2
  • 12
  • 20
  • I use Rstudio and run the code in a Markdown. I get the same error if I run the code you posted above. – Banjo Apr 19 '19 at 11:45
  • Ah, sorry. I did not understand that your "Python" heading referred to a chunk in R markdown. I'll read your error messages more closely and compare to my setup. – Jon Olav Vik Apr 19 '19 at 11:51
  • Could you post the result of `library(reticulate); devtools::session_info()`, both when compiled in R markdown and when run from the R console? Do you get the same error if you run `library(reticulate); repl_python()` and enter `r` at the Python prompt within RStudio? – Jon Olav Vik Apr 19 '19 at 12:01