Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

test

Project: test
Views: 579
---
title: python3 test
---
# this setup code is important, otherwise it won't run! library(reticulate) use_python('/usr/bin/python3') # alternatively, you can use this to set an engine executable #knitr::opts_chunk$set(engine.path = list( # python = '/usr/bin/python3' #))

Hi, I'm Python

x = 12 + 23**5 print(x)

Running

import sys print(sys.version)

Plotting

import numpy as np import matplotlib.pyplot as plt xx = np.linspace(0, 10, 100) yy = np.sin(xx) plt.plot(xx, yy) plt.show()

R still works, too

x <- c(1,2,3,2,1) plot(x)

Interactions between R and Python

This uses the reticulate R library:

Retrieve the value of x from the Python session again:

py$x

Assign to a variable in the Python session from R:

py$y = 1:5

See the value of y in the Python session:

print(y)