I want to configure Rstudio and python so that I can work on R markdown files and use python code in Rstudio as I want to take advantage of the ggplot2 package. My current R markdown file is as follows:
---
title: "plottingpython"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(reticulate)
use_python("C:/Users/Joe's PC/AppData/Local/Programs/Python/python38-32")
```
```{python}
import numpy as np
```
However on running this I get the following error,
Error in py_run_string_impl(paste0("import sys; sys.path.append('", system.file("python", :
SyntaxError: invalid syntax (<string>, line 1)
and I cannot find the solution to this error. I have ensured that both R and Rstudio are up to date. Rstudio: 1.3.1056 R: 4.0.2 2020-06-22
I have set use_python("") to the interpreter path which I use in PyCharm.
I am a little stumped at the source of the error. The same error appears if I directly code using
library(reticulate)
py_run_string("import numpy as np")
py_run_string("tester = np.array([2,3,4,5])")
py_run_string("print(tester)")
into an R script. Does anyone know the root cause of the above error and if possible any solutions? If anyone can help that would be greatly appreciated thanks!
I am on a Mac and the line use_python("C:/Users/Joe's PC/AppData/Local/Programs/Python/python38-32")
didn't throw an error, I thought it would. But Just using your code worked well, Are you still experiencing issues with Python in Rmarkdown?
---
title: "plottingpython"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(reticulate)
```
```{python}
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-1, 1, 50)
print(x)
y = 2*x + 1
plt.plot(x, y)
plt.show()
```