How to use python in Rstudio

created at 06-30-2021 views: 38

Pycharm has always been mentioned when we learn and test python, which is not only easy to use, but also supports the use of R language.

What about the classic Rstudio?

I saw some people mentioned that the useful Python IDE is Rstudio. I searched it and found it interesting.

Familiar interface for R users

Familiar interface, just select Python Script in New.

Run a piece of test code directly. If it has never been done, Rstudio will download Miniconda by default for environment construction and package management.

Environment configuration

1. Install the python operating environment, Anaconda is recommended.
2. Install R and Rstudio
R official 
Rstudio official
3. Install the reticulate package.


To run the python package in R, you must download and install it through this, which can be understood as an R-Python interface.

Test the installation environment

#Load the reticulate package
#Check if Python has been installed on your system, it is TRUE
#Select python version
#Specify the directory containing Python virtualenv
#Specify the name of the Conda environment

start python command line

Then enter the code in the console, or use the following methods. The code is not concise enough and is not recommended.

os <- import("os")
os$listdir() #View file
os$getcwd() #Return to working path
os$chdir("../Desktop/") #Modify working path

py_install(packages = "numpy") #Install numpy
numpy <- import("numpy") #Load numpy
y <- array(1:4, c(2, 2))
x <- numpy$array(y) #Build an array

Install python package

Install python package

If an error occurs in the attempt, it may be caused by the conda environment. According to different systems, there are two different operations: virtualenv is used for linux and mac; and Anaconda is used for Windows.

Use Python interactively

#Start python command line
# Enter python code
import pandas as pd
# Load data set
travel = pd.read_excel("text.xlsx")
# Display the number of rows and columns of the data set
# Get the object mydata created in R
# Back to R
# py object Get the object t in python

In this way, you can use Pandas to read and manipulate the data, and then use ggplot2 to easily draw the Pandas data frame, although there is also a corresponding drawing method for ggplot2 in python.

Simple plotting test

method #1:

Use the R package, and then draw directly in Rscript:

import numpy as np
import matplotlib.pyplot as plt

size = 3
x = np.arange(size)
a = [3, 6, 4]
a_SD = [1, 0.5, 1.5]
b = [4, 7, 3]
b_SD = [0.5, 1, 2]

total_width, n = 0.8, 2
width = total_width / n
x = x - (total_width - width) / 2
labels = ['Trial 1', 'Trial 2', 'Trial 3'], a,  width=width, yerr = a_SD, tick_label=labels, label='Control') + width, b, width=width, yerr = b_SD, tick_label=labels ,label='Experimental')


method #2:

Run directly in Python script

Of course, the shortcut keys of Rstudio are supported.


reticulate includes a Python engine for R Markdown with the following functions:

  • You can run Python blocks in a single Python session embedded in an R session. At the same time shared variables/states between Python blocks.
  • Printable Python output, including matplotlib graphical output.
  • You can use the py object to access the objects created in the Python block in R.
  • Use the r object to access the object created in the R block from Python.
import pandas as pd
# Load data set
travel = pd.read_excel("text.xlsx")

```{r,fig.width=7, fig.height=3}
ggplot(py$flights,aes(carrier,arr_delay)) + geom_point()

In the case of loading the reticulate package, you can directly call python.

Overall, in some respects, it is indeed convenient to run python directly on Rstudio. It is good to be familiar with a few invocation methods. At present, the only problem is that the drawing based on python is vague and needs to be debugged.

created at:06-30-2021
edited at: 06-30-2021: