Python and R Studio
There is no better place to write code than R Studio. At least scripting code. I think its an objective fact not an opinion that it is better than juptyer notebook. First, it allows you to view the data in the environment. Second, it allows you to run whatever line of code you want and you get view the results in the console. Finally, it also allows you to access to the terminal.
It works natively with R but the good folks at R Studio have now made it work with Python.
Install reticulate
and packages
First things first, we need to install reticulate
. We won't be busting out the pip
installer for this one. reticulate
is an R package that makes it possible to use python in R Studio. If you don't have python installed on your computer, install it first. If you do, let's install reticulate
:
install.packages("reticulate")
Next, create a python script and trying running something in python:
print("hello world")
How good would python really be if you couldn't use packages. Installing python packages will oddly be done in R not in python. So back over to the R side of things:
library(reticulate)
py_install("pandas")
Once you run this, it will ask if you want to create a default python environment. Answer yes and it will create a venv
environment called r-reticulate
. R Studio will automatically install python packages into this environment when you run py_install()
Ok, now you have installed pandas into that environment. Next you need to make sure that environment is being used by python. In order to do this, go to "Tools >> Global Options >> Python" and then "Python Interpreter". Then select the version of python created by R Studio.
It should be something like this:
~/.virtualenvs/r-reticulate/bin/python
Now, lets try loading the pandas
package in our python script:
import pandas as pd
And boom, it works!
Creating a Different venv
Python is a total mess at package management. Unlike in R where you have the CRAN repository, you often will find yourself in package management hell. Or at least, I do.
So in many cases, it may make sense to create a separate venv
for a given project. In this instance, we are going to create one for web scrapping.
First, in an R script, do the following:
library(reticulate)
# create a new environment
virtualenv_create("webscraping")
# install beautiful soup
virtualenv_install("webscraping", "beautifulsoup4")
This will create a venv
called webscraping
and then install beautifulsoup4
into it.
I have found that the easiest way to switch to that venv
is to go to "Tools >> Global Options >> Python" and then select the python interpreter that uses the webscraping
virtual environment.
And now, once we have switched the venv
, we can test if our package loaded correctly like so:
from bs4 import BeautifulSoup