Added suggestion: If, by chance, you have already initialised an environment of python (such as by using r-miniconda), you simply have to restart the R session to use a different environment.
Anyone knows why I receive this error: Error in setwd(root) : cannot change working directory, when I try to run install_python line, though I set the working directory to the current session?
Thank you. Is it possible to combine different functions from python and R and run them simultaneously? For example, if I have output values from some R function, can I pass them to python and then get the output of the python back to R?
rpy2 allows r objects to be used in Python. pyreadr allows the .RData files in Python. Haven't tried a full blown project yet to see all possibilities.
very informative video but i have a doubt, currently i am working on a project in data science which uses python libraries like cv2 , tensorflow and few more and i have to compile a code in rstudios, ihave the code for it in python but our guide has strictly asked us to convert the code in r is that possible ?
Hi, thanks for your message. So if I understand it correctly, you are asked to covert an existing Python project which involves tensorflow, OpenCV etc. ( so for example something like ML based image recognition etc..). So , in general it should be possible to do so in R. As R has equivalent libraries for TensorFlow and OpenCV. If you are new to R then try ChatGPT to convert your python code to R. It won't convert the whole of it. But you can try one step at a time. All the best.
Hello. Reconfirming that this link worked on my browser, try copy and pasting the link in your browser. cran.r-project.org/web/packages/reticulate/vignettes/calling_python.html
why am getting Traceback (most recent call last): File "", line 1, in NameError: name 'plt' is not defined every time I run " plt.bar(Country , Cases , color = Colors) " ? in fact , when I "run import matplotlib.pyplot as plt" , I get the following Traceback (most recent call last): File "", line 1, in File "/home/ash/R/x86_64-pc-linux-gnu-library/4.3/reticulate/python/rpytools/loader.py", line 119, in _find_and_load_hook return _run_hook(name, _hook) I followed your steps , but instead I created an virt.env using the version "3.9.18" as I have it already installed before on my computer with linux , Ubuntu 20.04 , and R version 4.3.1 your input is highly appreciated
Yes, whatever is the latest version you have is fine, before using the matplotlib in your python script, does it install when you run the reticulate library, create the new virtual envrionment and install the packages in the virtual_env , I believe that either your did not install the matplot lib in your newly created virtual environment or if you did try to install but it (matplotlib) did not get installed in the virtual environment. Try creating a new virtual environment from scratch. ( I have seen that if I try to use the existing virtual environment then sometimes it gives errors..)
Thanks sir , I followed your steps , and I have used " reticulate::py_install("matplotlib", pip = TRUE) " and it did not throw the same error as before anymore , so it should be fine , however , the plot does not come up at all every time I run the python script after first activating the environment and uploading the "reticulate" as you have shown above However I still get the below " plt.show "
Yes you can do that import os import snscrape.modules.twitter as sntwitter import pandas as pd from datetime import date # Creat a list to append tweet data to tweetlist = [] # TwitterSearchScraper to scrape data # and append tweets to list for i, tweet in enumerate( sntwitter.TwitterSearchScraper('adani').get_items()): tweetlist.append([tweet.date, tweet.id, tweet.content, tweet.user.username]) print(tweet.date, tweet.content) # Create a dataframe from the tweets list above tweet_dataframe = pd.DataFrame(tweetlist, columns=['Datetime', 'Tweet Id', 'Text', 'Username'])
@@DataAnalytic Should I also install all these packages in the rscript? Like virtualenv_create(envname = "xyz", "snscrape", ignore_installed=FALSE, pip_options=character()) and so on?
@@DataAnalytic I preferred to use it in RStudio because I get almost always the "mkl error" on my intel macbook. I disinstalled everything (pip, conda etc...) And will try again. I am quite new to Python, I use mostly R, and get quite confused by all these specifics, environments, package managers, etc... I assume they are conflicting but have no clue how to solve so I disinstalled everything and will try again.
library(reticulate)
version = "3.9.6"
install_python(version = version)
virtualenv_create("my-python", python_version = version)
use_virtualenv("my-python", required = TRUE)
virtualenv_install(envname = "my-python","numpy",
ignore_installed = FALSE,pip_options = character())
virtualenv_install(envname = "my-python","pandas",
ignore_installed = FALSE,pip_options = character())
virtualenv_install(envname = "my-python","matplotlib",
ignore_installed = FALSE,pip_options = character())
virtualenv_install(envname = "my-python","sklearn",
ignore_installed = FALSE,pip_options = character())
Added suggestion: If, by chance, you have already initialised an environment of python (such as by using r-miniconda), you simply have to restart the R session to use a different environment.
Very informative and useful
Simple yet amazing. Not gonna mention but there are a few videos that appear on the top when searched and do nothing but confuse you.
Awesome, thank you!
It could better if you had added the commands in the comment
this is amazing
Anyone knows why I receive this error: Error in setwd(root) : cannot change working directory, when I try to run install_python line, though I set the working directory to the current session?
was able to resolve the issue
yeah. set the working directory to the current folder, also take care to use change \ to \\ if you are copying the path from windows to R
same , i am getting this error too
Thank you. Is it possible to combine different functions from python and R and run them simultaneously? For example, if I have output values from some R function, can I pass them to python and then get the output of the python back to R?
rpy2 allows r objects to be used in Python. pyreadr allows the .RData files in Python.
Haven't tried a full blown project yet to see all possibilities.
very informative video but i have a doubt, currently i am working on a project in data science which uses python libraries like cv2 , tensorflow and few more and i have to compile a code in rstudios, ihave the code for it in python but our guide has strictly asked us to convert the code in r is that possible ?
Hi, thanks for your message.
So if I understand it correctly, you are asked to covert an existing Python project which involves tensorflow, OpenCV etc. ( so for example something like ML based image recognition etc..).
So , in general it should be possible to do so in R.
As R has equivalent libraries for TensorFlow and OpenCV.
If you are new to R then try ChatGPT to convert your python code to R. It won't convert the whole of it. But you can try one step at a time.
All the best.
@@DataAnalytic thanks alot sir 🙏
Thanks for this video. Can you able to add a Python file and use its functions and objects into the R package you wrote?
Hi there
Yes, you can. See the examples here
cran.r-project.org/web/packages/reticulate/vignettes/calling_python.html
@@DataAnalytic the link is dead
Hello.
Reconfirming that this link worked on my browser, try copy and pasting the link in your browser.
cran.r-project.org/web/packages/reticulate/vignettes/calling_python.html
why am getting
Traceback (most recent call last):
File "", line 1, in
NameError: name 'plt' is not defined
every time I run " plt.bar(Country , Cases , color = Colors) " ?
in fact , when I "run import matplotlib.pyplot as plt" , I get the following
Traceback (most recent call last):
File "", line 1, in
File "/home/ash/R/x86_64-pc-linux-gnu-library/4.3/reticulate/python/rpytools/loader.py", line 119, in _find_and_load_hook
return _run_hook(name, _hook)
I followed your steps , but instead I created an virt.env using the version "3.9.18" as I have it already installed before on my computer with linux , Ubuntu 20.04 , and R version 4.3.1
your input is highly appreciated
Yes, whatever is the latest version you have is fine, before using the matplotlib in your python script, does it install when you run the reticulate library, create the new virtual envrionment and install the packages in the virtual_env ,
I believe that either your did not install the matplot lib in your newly created virtual environment or if you did try to install but it (matplotlib) did not get installed in the virtual environment.
Try creating a new virtual environment from scratch. ( I have seen that if I try to use the existing virtual environment then sometimes it gives errors..)
Thanks sir , I followed your steps , and I have used "
reticulate::py_install("matplotlib", pip = TRUE)
" and it did not throw the same error as before anymore , so it should be fine , however , the plot does not come up at all every time I run the python script after first activating the environment and uploading the "reticulate" as you have shown above
However I still get the below
" plt.show
"
it worked now all is fine thank you for the follow up@@DataAnalytic
All the best.
I'm getting a python error with install_python (version = version). It's saying I need to download git and ensure it is in the path
Hi, try to install Python standalone and see if gets installed properly.
great, thank you
You are welcome!
Awesome! Thank you very much :)
Would it be possible to install the package "snscraper" to scrape tweets without the API?
Yes you can do that
import os
import snscrape.modules.twitter as sntwitter
import pandas as pd
from datetime import date
# Creat a list to append tweet data to
tweetlist = []
# TwitterSearchScraper to scrape data
# and append tweets to list
for i, tweet in enumerate(
sntwitter.TwitterSearchScraper('adani').get_items()):
tweetlist.append([tweet.date, tweet.id, tweet.content, tweet.user.username])
print(tweet.date, tweet.content)
# Create a dataframe from the tweets list above
tweet_dataframe = pd.DataFrame(tweetlist, columns=['Datetime', 'Tweet Id', 'Text', 'Username'])
@@DataAnalytic Should I also install all these packages in the rscript? Like virtualenv_create(envname = "xyz", "snscrape", ignore_installed=FALSE, pip_options=character()) and so on?
Yes, it should work. Though it took longer to run the script in RStudio as compared to running it directly in pycharm or Visual Code.
@@DataAnalytic I preferred to use it in RStudio because I get almost always the "mkl error" on my intel macbook. I disinstalled everything (pip, conda etc...) And will try again. I am quite new to Python, I use mostly R, and get quite confused by all these specifics, environments, package managers, etc... I assume they are conflicting but have no clue how to solve so I disinstalled everything and will try again.
I have often seen that creating a new virtual environment works and install all the needed packages in the new environment.
Hi!!!! excellent!!! but for work again with R?
Open an existing R script or open a new R Script page and you can start working in R.