Tag Archives: Anaconda

Set up Python for RStudio

I consider myself a R programmer for data science but Python is also a great language for the same task. RStudio understood that and since the version 1.4 supports Python https://blog.rstudio.com/2020/10/07/rstudio-v1-4-preview-python-support/

The key to do this is to set up a virtual environment, this allow you to avoid compatibilities issues. The most easy way that I found to achieve this is through Anaconda: https://www.anaconda.com/products/individual-b

After the installation just open the anaconda prompt to set up a new virtual environment and type the following:

conda create -n your_env
conda activate your_env
conda config --env --add channels conda-forge
conda config --env --set channel_priority strict
# and to install a new package
conda install python=3 geopandas
conda deactivate

Now you can open RStudio and:

install.packages("reticulate")

And finally, you can look for the python interpreter in the Global Options and start to work with Python in RStudio

reticulate::repl_python()
#libraries
import geopandas as gpd
import fiona
from plotnine import (ggplot, aes, geom_point, geom_smooth, labs)
#kmldata
gpd.io.file.fiona.drvsupport.supported_drivers['KML'] = 'rw'
zn_rrl = gpd.read_file('data\\a.kml', driver='KML')
zn_rrl.head()
zn_stgo = gpd.read_file('data\\b.kml', driver='KML')
zn_stgo.head()
(ggplot() + 
  geom_map(a) + 
  geom_map(b))
exit