How can I import python packages using knit r? - r

I am looking to integrate python code into a r notebook (or even a rmarkdown document). I am able to run Python using a python code chunk:
```{python}
```
However I am unable to import a package (e.g. pandas). I have anaconda installed which has pandas installed, however I am unable to import it. Can anyone give me direction as to how I can import packages into r via knitr, preferably using anaconda?

{python, engine.path="/path/to/your/anaconda3/bin/python"}

Related

Unable to import pandas profiling in kaggle noteook

I am working in kaggle notebook and wanted to use pandas profiling for EDA but it is giving error. What can be done to use this library in kaggle notebooks?
Even though Kaggle comes with pandas-profiling as default, I would recommend running this command in a cell to use the latest version of the package: %pip install -Uqq pandas-profiling[notebook]

Error installing Numpy with Reticulate from Rstudio

I am trying to use Python from Rstudio but even if pandas and other packages can get installed normally , numpy don't. Is it something wrong with my code ?
PS : I am learning python so I might do basic error
I have tried to install again numpy from conda and no problem on there

BokehJS library missing error and no plot

I am running Anaconda 10.0 Jupyter Notebook Python 3.8, bokeh 2.3.0 on Chrome. On my other PC (should be identical setup, except this is a slightly later Anaconda version) images are fine, but on this there is no image and the Javascript console tells me:
Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing
I could not find a simple, working solution to this problem here or elsewhere on the net and I am at a total loss now.
The abbreviated code I'm running is:
import regex as rg
import pandas as pd
import numpy as np
import datashader as ds, colorcet
import holoviews as hv
from holoviews.operation.datashader import datashade
from holoviews import opts
hv.extension("bokeh")
datashade(hv.Points(df_re), cmap=colorcet.fire).relabel('Reflectances').opts(height=700, width=800)
Where is the problem and how should I fix it?
Unfortunately that didn't work for me, still the same error.
What I found interesting was that prior to installing ver. 1.0.3 of the extension, Anaconda prompt said:
(Datashader-Holoview) C:\Windows\system32>jupyter labextension list
JupyterLab v2.2.6
No installed extensions
It is as if no version of that extension did exist in the environment.
There might be a bug with the most recent pyviz jupyter labextension (1.0.4). See if it works for you after you do
jupyter labextension install #pyviz/jupyterlab_pyviz#1.0.3
This worked for me.

ggplot function not found in jupyter notebook with rpy2

I am working on a computer cluster and testing out how to use a Jupyter notebook on it. I encountered this problem while trying to use ggplot2. I've tried installing the package in the notebook, with R in the command line, and with conda install, and r-ggplot2 (version 2.2.1) shows up in conda packages. Any ideas?
Judged by your screenshot, you are running a python-based notebook. It looks as if you are trying to use ggplot as part of the python package rpy2.
Based on this source, ggplot is present in rpy2.robjects.lib.ggplot2, so you might want to try something like
from rpy2.robjects.lib.ggplot2 import ggplot
...
ggplot(data=df)

Unable to import google.cloud.vision via the reticulate package in R

I am using the reticulate package to import python modules into RStudio.I was able to import packages like cv2,pandas,sklearn but was unable to import the google.cloud.vision package.I have installed this package via pip and am using Anaconda 4.4.0 Python 2.7 version on ubuntu-trusty-14.04-amd64-server on AWS.Pic of the error in R-Studio Server
The Installation of the vision and the language libraries was done via
pip install --upgrade google-cloud-vision
pip install --upgrade google-cloud-language
I am also able to import these modules in the python interactive sessions without any error
>>>import google.cloud.vision
>>>import google.cloud.language
Can somebody please advise me on what I am doing wrong...
Edit 1 :I have tried using the approach that Yuan Tang had suggested.I have installed the requests and httplib2 package in the conda environment.I have also used the use_python command to point Rstudio to the correct python environment.
use_python("/home/avadhut/miniconda2/bin/python")
The cv2 package is installed in the conda environment and it is imported successfully which means RStudio is using the correct Python environment
Even after doing all this I am getting the following trace-back in the R studio console.
Error in py_module_import(module, convert = convert) :
ImportError: The requests library is not installed, please install the requests package to use the requests transport.
Detailed traceback:
File "/home/avadhut/miniconda2/lib/python2.7/site-packages/google/cloud/vision/__init__.py", line 36, in <module>
from google.cloud.vision.client import Client
File "/home/avadhut/miniconda2/lib/python2.7/site-packages/google/cloud/vision/client.py", line 20, in <module>
from google.cloud.client import ClientWithProject
File "/home/avadhut/miniconda2/lib/python2.7/site-packages/google/cloud/client.py", line 25, in <module>
import google.auth.transport.requests
File "/home/avadhut/miniconda2/lib/python2.7/site-packages/google/auth/transport/requests.py", line 30, in <module>
caught_exc,
File "/home/avadhut/miniconda2/lib/python2.7/site-packages/six.py", line 737, in raise_from
raise value
Here is the pic of my RStudio IDE with the error displayed.
!Reticulate Package Import Error
This is because the Python binary you are using inside RStudio via reticulate does not have those packages (e.g. requests, httplib2, etc) installed.
You can find out what Python reticulate is using via py_config(). You can also find the Python binary you are using on the terminal via which python.
Let's suppose the Python binary you are using on the terminal (the successful imports) is: "/Users/user1/anaconda3/bin/python". Then you use that particular Python binary for reticulate via the following:
use_python("/Users/user1/anaconda3/bin/python")
Then your imports should work as expected.

Resources