source("../../examples/python_pdf_report/manuscript/global.R")
if (!require("reticulate")) install.packages("reticulate")
library(reticulate)

Reticulate

The MPIThems templates aren't just for R users, Python programmers may also enjoy them by reticulate package. The reticulate brings a Python engine for R Markdown and allowing simple interchange between Python and R chunks. For install reticulte use cran repository as below:

install.packages("reticulate")

Choose Python version

The first step to running Python code is to choose the Python engine or environment. There are two ways to choose a Python environment:

  1. By default, reticulate uses the system Python found on your OS. To find the path, run the R command Sys.which("python") on the console. Alternatively, to use your installed Python, add your Python path with the command use_python() to set the default engine.
library(reticulate)
use_python("/usr/local/bin/python")
  1. If you have an Anaconda or Miniconda, you may use the use use_condaenv() or use use_virtualenv() to run Python code in any of these environments.
use_condaenv("Python3.8", required = TRUE) # Name of environment

The r-reticulate Conda environment will be created when the reticulate package is installed. To see the list of your Conda environment, run command conda list env in bash or retculate::conda_list() R command. Run the py_config() command to ensure that reticulate is utilizing your updated Conda env.

Install Python Package

The reticulate env has minimal Python packages. In order to install a new package, use py_install() command. The packages will be installed by default in r-reticulate env. For more information you can see reticulate's vignette Installing Python Packages

use_condaenv("r-reticulate", required = TRUE)
py_install("pandas")
py_install("numpy")
py_install("mapplotlib")
py_install("plotnine")
py_install("pandas")

Check Python working

To ensure that everything is working properly, try writing a basic Python code in RMarkdown. Set the Python command in the Python chunk {python} for this.

'''{python}
import numpy as np
import pandas as pd
np.arange(1, 10)
'''

```{python,eval=FALSE, echo=FALSE} import numpy as np import pandas as pd np.arange(1, 10)

For the convenience of Python coding in RMarkdown, it is recommended that you create a shortcut to generate the python chunks.
To create shortcut go to `Tools -> Modify Keyboard Shortcuts` and search `python` word to find `Insert Chunk Python`. 
Then click on it and set a suitable shortcut for it. `Ctrl + Alt + P` sounds a good option. 

In the following example, we use the `pandas` package to construct a data frame and `matplolib` to plot the data.

```{python matplotlib,eval = FALSE,echo=TRUE,fig.height=2}
import matplotlib as plt
df = pd.DataFrame(data = {"x":np.arange(1,10,.01)})
df = df.assign(y=np.cos(df["x"]))
df.plot(x="x", y = "y", title = "Simple Matplotlib Plot in RMarkdown")

Fortunately, Package ggplot2 has also been implemented for Python. Below is an example of drawing a ggplot in Python.

```{python ggplot,eval = FALSE,echo=TRUE,fig.height=2} from plotnine.data import economics from plotnine import ggplot, aes, geom_line ggplot(df) + aes(x="x", y="y") + geom_line()

<p align="center">
<img src="../../examples/python_pdf_report/figure/ggplot2R-5.png"/>
</p>

## Calling Python from R

All objects created within Python chunks are available to R by calling the `py$object`. 
In the below example, we plot the previously Python-produced data with ggplot in R.

```r
ggplot(py$df, aes(x=x, y=y)) + 
  geom_line()



Ehyaei/MPIThemes documentation built on Jan. 13, 2022, 7:28 p.m.