Cannot knit python chunk in rmd using Reticulate

Hi,

I have tried to use reticulate (v 1.18) so I can do both R (4.0.3) and python (3.8) in a rmd for data analysis.
I can run python code when using the "Run" button, but when I try to knit my Rmd to a html file.
It stucked, with a message like this below.

|.......... | 14%
ordinary text without R code

|.................... | 29%
label: setup
|.............................. | 43%
ordinary text without R code

|........................................ | 57%
label: unnamed-chunk-1 (with options)
List of 1
$ engine: chr "python"

For the setup
I did this

knitr::opts_chunk$set(echo = TRUE)
library(reticulate)
use_python("/Users/xxxxx/opt/anaconda3/bin/python")
knitr::opts_chunk$set(python = reticulate::eng_python)

I also tried to replace the reticulate engine by doing this

knitr::opts_chunk$set(python.reticulate=FALSE)

And it gave me error in my second python chunk (I also import pandas in the first python chunk).
If I don't use reticulate python engine, I think to remove the error is to import python module in every single python chunk....

"NameError: name 'pd' is not defined" 
Calls: <Anonymous> ... call_block -> block_exec -> in_dir -> engine -> eng_interpreted
In addition: Warning message:
Execution halted

Is there any way to fix this which I can use reticulate ?

Thx!!!

Can you share the code of your Rmd file ?

For the setup, you don't need to specify anything. The reticulate engine will be used by default:

Try a small example to check your installation is working

---
title: "Python"
output: html_document
---

```{r setup}
library(reticulate)
use_python("/Users/xxxxx/opt/anaconda3/bin/python")
```

```{python}
print ("ok")
```

Also, as you are using anaconda, know that there is support for conda env.

Hi,

It freezed with the message I attached previously at the line "engine: chr "python" when I clicked the "Knit" button, and there was not output.
Oh, the command rmarkdown::render("file.Rmd") you provided worked! I guess sth didn't work in the R studio GUI.

Thanks a lot!!

Strange. You could try the new 1.4 preview to see if you still have issue if you want

Hi,

Thanks for your reply!

I just used your example to test .

If I run all the chunks, it can print "ok", so it worked.
But when when I attempted to knit the Rmd to a html. The same problem occurred.

processing file: reticulate.Rmd
  |..................                                                    |  25%
  ordinary text without R code

  |...................................                                   |  50%
label: setup
  |....................................................                  |  75%
  ordinary text without R code

  |......................................................................| 100%
label: unnamed-chunk-1 (with options) 
List of 1
 $ engine: chr "python"

Does it throw an error ? Does it hang ?

If it works in the IDE it should work when knitting.

You can try with console using rmarkdown::render("file.Rmd") also

Yes that is strange. Which is your version of Rstudio ?
Maybe you need an update.

Yes, it's strange! Because of the error, I actually updated my RStudio and R yesterday.
My RStudio version is 1.3.1093. The previous version that I had is also just a few months old, as I usually update it every few month.