-
-
Notifications
You must be signed in to change notification settings - Fork 872
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use knitr::spin() with R and Python code in one document #1773
Comments
I find this an interesting question. It make me dig into For now, Line 69 in dccdad7
and parse from R base only know how do deal with R code.
Independently of I think all this makes difficult for R script to contains anything else than R code. Technically, with knitr you can change engine with the Examplehere is an example of what is blocking here. I put this in #+ setup, include = FALSE
library(reticulate)
use_miniconda()
# Using python directly
#+ engine = python
import os
os.getenv("RSTUDIO_PANDOC")
# Using R with reticulate
os <- import("os")
os$getenv("RSTUDIO_PANDOC") We get this error Error in parse(text = x, keep.source = TRUE) :
<text>:7:8: unexpected symbol
6: #+ engine = python
7: import os
^ This is because what we know of being python code is parsed by R as R code and it is invalid R code. Solutions ?If we really want to have an equivalent of Out of curiosity, why do you prefer Also, the answer you got on SO is really interesting: using I am just sharing some thoughts here on this topic to contribute to the discussion. |
Thanks for sharing your thoughts on that. I fully agree that it's a fair split that R and Python have their own places in As regards your question: for me coming from the R side, it feels like a more native approach to start out with a plain |
That said, I probably won't have time to fix it in the near future... |
Oh thanks ! I did not noticed that. It was not intuitive for me that an My opinion being shared ( |
My solution was just to check if the language was R before the parsing step. If not, then skip that check (with the consequence that multiline comments etc. remain problematic in languages other than R).
I’ll try to dig out this code and make a clean pull request so that you can review it.
|
With 74bcff8, using cat(spin(knit = FALSE, text = '#+ setup, include = FALSE
library(reticulate)
use_miniconda()
# Using python directly
#+ engine = "python"
import os
os.getenv("RSTUDIO_PANDOC")
# Using R with reticulate
os <- import("os")
os$getenv("RSTUDIO_PANDOC")'), sep = '\n')
|
Question directly copied from StackOverflow:
With the advent of reticulate, combining R and Python in a single .Rmd document has become increasingly popular among the R community (myself included). Now, my personal workflow usually starts with an R script and, at some point, I create a shareable report using
knitr::spin()
with the plain .R document as input in order to avoid code duplication (see also Knitr's best hidden gem: spin for more on the topic).However, as soon as Python code is involved in my analysis, I am currently forced to break this workflow and manually convert (ie. copy and paste) my initial .R script into .Rmd before compiling the report. I wonder, does anybody know whether it is – or for that matter, will ever be – possible to make
knitr::spin()
work with both R and Python code chunks in a single .R file without taking this detour? I mean, just like it works when mixing the two languages, and exchanging objects between them, in a .Rmd file. There is, at least to the best of my knowledge, no possibility to add something likeengine = 'python'
to spin documents at the moment.By filing an issue to this repo, I promise that
xfun::session_info('knitr')
. I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version:remotes::install_github('yihui/knitr')
.I understand that my issue may be closed if I don't fulfill my promises.
The text was updated successfully, but these errors were encountered: