First thing you need to do is create an R-Markdown document and insert an R chunk: insert (top right of source > R). Lets look at an example from the reticulate documentation. Otherwise it will only work when you knit the document it doesn’t happen if you are running chunk by chunk. Warning: The communication between R and Python chunks (the pieces of code in an R-Markdown document) is only supported since RStudio v1.2 preview release. Whith this option you can comminicate between Python and R while generating great documents with all your data anlysis pipeline in it! Objects you create within Python are available to your R session (and vice versa). Python interactive session: The repl_python() function creates an interactive Python console within R. Sourcing Python scripts: The source_python() function enables you to source a Python script the same way you would source() an R script (Python functions and objects defined within the script become directly available to the R session).Ĥ. Importing Python modules: The import() function enables you to import any Python module and call its functions directly from R.ģ. Python in R Markdown: Supports communication between R and Python (R chunks can access Python objects and vice-versa).Ģ. We are now all set to start running some python code! There are 4 ways to interact with python using this package:ġ. You can even manage your conda environment directly from R: py_install("pandas") # If you are using an environment with already installed packages you can skip this step or install it directly on your environement. Lets try installing a usefull package (this will be installed to the “r-reticulate” environment). You can easily install packages directly through reticulate. # /Users/jfranco1/anaconda/envs/general/bin/python # /Users/jfranco1/anaconda/envs/r-reticulate/bin/python # numpy: /Users/jfranco1/anaconda/envs/r-reticulate/lib/python3.6/site-packages/numpy # pythonhome: /Users/jfranco1/anaconda/envs/r-reticulate:/Users/jfranco1/anaconda/envs/r-reticulate # libpython: /Users/jfranco1/anaconda/envs/r-reticulate/lib/libpython3.6m.dylib It should be 3.6… py_config() # python: /Users/jfranco1/anaconda/envs/r-reticulate/bin/python We can have a final check of the Python version being used. If changing the conda environment doesn’t work, try changing the path direclty using: use_python("Path.", required=TRUE) The same is true whenever you type py_config() instead of py_discover_config(). This means that you need to restart R if you want to change the Python version. Once you run use_condaenv("relevantenv") Python is loaded. If the Python version is not the one from the relevant anaconda environment change the environment by typing use_condaenv("relevantenv") It might be the case that you had an older Python version installed and that reticulate is pointing to this older version (remember you need a recent version of Python - e.g Python 3.6.5-). Once installed we need to call in the package: library(reticulate) Now you are ready to install the reticulate package: install.packages("reticulate") Install RStudio v1.2 preview release - The new version of RStudio will fix some bugs. In order for the current package of reticulate to work out of the box I recommend to follow these instructions: Reticulate is quite recent and many bugs with RStudio have been solved in the last month, so I recommend installing the latest versions of everything we need.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |