Learn how to use R Markdown

You can learn how to use R Markdown for Python from the following tutrials:

https://support.posit.co/hc/en-us/articles/360023654474-Installing-and-Configuring-Python-with-RStudio

https://medium.com/data-and-beyond/how-to-seamlessly-integrate-python-into-r-rmarkdown-codes-2fe09cfdd0ee.

Create a Python environment and configure it for your Python

I. Create a Python environment in your project

  1. Create a project using RStudio

  2. Navigate into the RStudio project directory

  3. Create a directory called “my_env”

  4. Create a virtual environment in my_env

If you use pipenv and install Python 3.11, run the following command in the terminal:

pipenv –python 3.11 pipenv shell

You can install libraries using pip:

pipenv install pandas

  1. Install reticulate

install.packages(“reticulate”)

  1. Configure reticulate
  1. Create a file called “.Renviron” in the my_env directory

touch .Renviron echo ‘RETICULATE_PYTHON=RETICULATE_PYTHON = path/to/your/python’ > .Renviron

You can show the path for your Python by running the following command in the my_env directory:

pipenv –venv

  1. Restart your R sesseion

Use R

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Use Python