-
-
Notifications
You must be signed in to change notification settings - Fork 877
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')
|
I might be missing something...is there currently a way to spin a fully python script into Rmd? As in start with
Run something like
The closest I've got is adding this to the top of my python file:
However, here I'm adding some R to a python script, so the script can no longer run fully on its own. I know one way is to set the option at the knit stage, but I would like to produce an Rmd script that runs on its own. |
I think the concept here is that you skip the Rmd file – it is never produced. Instead of RMD -> MD -> final document, you go from source script -> MD -> final document. In the case of a Python script it would go from PY -> MD -> final document.
BITD a lot of us used to write source code with lots of comments. “Spinning” is just a way of supercharging those comments to mimic knitting and weaving.
Doug Hemken
Statistical consultant (retired)
Social Science Computing Cooperative
Univ. of Wisc. – Madison
|
@katrinabrock currently, #' Example text
#'
#+ engine='python'
# example comment
print([i for i in 'abcdefg'])
@Hemken with |
@cderv I'm trying to think if there is a creative way...maybe with multiline quotes and/or adjusting the spin regexes ...to get python interpreter to ignore that line while spin can see it. EDIT: Here's the best I've come up with so far:
It's super ugly, but it does result in both not creating a python syntax error (or any behavior change), and adding adding the following to the
|
I know that, but this is R code, so it will lead to R cells in the .Rmd created. I thought you did not want that. Especially because script is I think the best way would be to maybe consider that all code in But that is not how |
Yes, the result of my workaround in the Rmd is there is one (real, hidden) R cell that sets to the option. Then all the rest of the cells are "R" cells with Indeed, I would prefer a "real" fix where |
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: