Use Python in RStudio

Поделиться
HTML-код
  • Опубликовано: 21 авг 2024
  • Now you can use Python in RStudio. So easy and manageable.

Комментарии • 39

  • @ajaykushwaha4233
    @ajaykushwaha4233 7 месяцев назад +11

    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())

  • @hrishikeshhardikar4383
    @hrishikeshhardikar4383 Год назад +6

    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.

  • @valorousspirit4905
    @valorousspirit4905 Год назад +2

    Very informative and useful

  • @hrishikeshhardikar4383
    @hrishikeshhardikar4383 Год назад +2

    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.

  • @otsienocharles7300
    @otsienocharles7300 Год назад +6

    It could better if you had added the commands in the comment

  • @ransinghray3688
    @ransinghray3688 2 года назад +2

    this is amazing

  • @nargessalehi5390
    @nargessalehi5390 Год назад +5

    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?

    • @eduartobando2260
      @eduartobando2260 Год назад +2

      was able to resolve the issue

    • @shakeelx
      @shakeelx Год назад

      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

    • @divyaagrawal6740
      @divyaagrawal6740 Год назад +1

      same , i am getting this error too

  • @jink3280
    @jink3280 Год назад +2

    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?

    • @DataAnalytic
      @DataAnalytic  Год назад

      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.

  • @shreyashagawane5126
    @shreyashagawane5126 9 месяцев назад +2

    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 ?

    • @DataAnalytic
      @DataAnalytic  9 месяцев назад

      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.

    • @shreyashagawane5126
      @shreyashagawane5126 9 месяцев назад +1

      @@DataAnalytic thanks alot sir 🙏

  • @joshstat8114
    @joshstat8114 8 месяцев назад +1

    Thanks for this video. Can you able to add a Python file and use its functions and objects into the R package you wrote?

    • @DataAnalytic
      @DataAnalytic  8 месяцев назад

      Hi there
      Yes, you can. See the examples here
      cran.r-project.org/web/packages/reticulate/vignettes/calling_python.html

    • @joshstat8114
      @joshstat8114 8 месяцев назад

      @@DataAnalytic the link is dead

    • @DataAnalytic
      @DataAnalytic  8 месяцев назад

      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

  • @mikiallen7733
    @mikiallen7733 11 месяцев назад +1

    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

    • @DataAnalytic
      @DataAnalytic  11 месяцев назад

      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..)

    • @mikiallen7733
      @mikiallen7733 11 месяцев назад +1

      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
      "

    • @mikiallen7733
      @mikiallen7733 11 месяцев назад +1

      it worked now all is fine thank you for the follow up@@DataAnalytic

    • @DataAnalytic
      @DataAnalytic  11 месяцев назад

      All the best.

  • @nadiadansani2139
    @nadiadansani2139 6 месяцев назад +1

    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

    • @DataAnalytic
      @DataAnalytic  6 месяцев назад

      Hi, try to install Python standalone and see if gets installed properly.

  • @baneledludlu7983
    @baneledludlu7983 Год назад

    great, thank you

  • @AP-yx1mm
    @AP-yx1mm Год назад

    Awesome! Thank you very much :)
    Would it be possible to install the package "snscraper" to scrape tweets without the API?

    • @DataAnalytic
      @DataAnalytic  Год назад +1

      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'])

    • @AP-yx1mm
      @AP-yx1mm Год назад

      @@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
      @DataAnalytic  Год назад

      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.

    • @AP-yx1mm
      @AP-yx1mm Год назад

      @@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.

    • @DataAnalytic
      @DataAnalytic  Год назад

      I have often seen that creating a new virtual environment works and install all the needed packages in the new environment.

  • @joseoscardelgadobautista2105
    @joseoscardelgadobautista2105 2 месяца назад +1

    Hi!!!! excellent!!! but for work again with R?

    • @DataAnalytic
      @DataAnalytic  2 месяца назад

      Open an existing R script or open a new R Script page and you can start working in R.