---
title: Python3 in RMarkdown on CoCalc
---
library(reticulate)
use_python("/usr/bin/python3")
import sys
print(sys.executable)
print(sys.version)
import numpy as np
x = np.array([1,2,3,-1,2.3,12])
Plotting
import numpy as np
import matplotlib.pyplot as plt
xx = np.linspace(0, 10, 1000)
yy = np.sin(2 * np.exp(-xx))
plt.plot(xx, yy)
plt.show()
R still works, too
plotting same plot via R
plot(py$xx, py$yy, type = 'l', col = "blue", lwd=2)
Interactions between R and Python
This uses the reticulate R library:
Retrieve the value of x
from the Python session again:
Assign to a variable in the Python session from R:
See the value of y
in the Python session:
colorFunc <- "terrain.colors"
Base R comes with many functions for generating colors. The code below demonstrates the r colorFunc
function.
Blah blah!
r colorFunc
image(volcano, col = get(colorFunc)(5))