rstudio / reticulate

R Interface to Python
https://rstudio.github.io/reticulate
Apache License 2.0
1.67k stars 327 forks source link

Include option bug #182

Closed ticocim closed 1 year ago

ticocim commented 6 years ago

Hello I found a possible bug in reticulate library.

Problem Testing this code:


\documentclass{article} \begin{document} <<R_test,include=FALSE,split=T,fig.path="./", eval=T,echo=T>>= a=10 print(a) @ Including the external results from file R_text.tex produces:

\input{./R_test}

<<Py_test,engine="python",include=FALSE,split=T,fig.path="./", eval=T,echo=T>>= a=10 print(a) @ Including the external results from file Py_text.tex produces:

\input{./Py_test} \end{document}


Running this Rnw file produces: Including the external results from file R text.tex produces a=10 print(a) ## [1] 10

Including the external results from file Py text.tex produces: a=10 print(a)

Description In this code I need that the results and source code from python and R goes to an external files.

Those files must be included in some latex file later using the include key.

The R chunk write to R tex file the source code and the output (##[1] 10). But the Python chunk only produces the source code, never the output from any print.

Running in R it works great. Running the Python do not.

Solution. I downloaded your reticulate library from github:

git clone https://github.com/rstudio/reticulate.git

and I change this:

FILE: knitr-engine.R Before: Lines 144 and 145: # append captured outputs if (nzchar(captured) && isTRUE(options$include))

After: Lines 144 and 145: # append captured outputs if (nzchar(captured) && isTRUE(options$eval))

Then I did R CMD build reticulate and R CMD INSTALL ./reticulate_1.5.0.9004.tar.gz

Now it work OK. The output now using the same code is: Including the external results from file R text.tex produces: a=10 print(a) ## [1] 10 Including the external results from file Py text.tex produces: a=10 print(a) ## 10

Now the ##10 is present. I write many documents using R with Sweave to produce PDF, and this options are very common to put the results in specifics point in the TEX files.

According to the knit documentation include=FALSE and split=TRUE, produces the output to a file but not include it in LATEX final code.

The options eval=TRUE, produces the output (of print cat, etc) and echo=TRUE produces the source code.

According to this include=FALSE must not disable the output results. This is the way that R works.

I do not have access to your GIT HUB to make the change and I need to verify this situation.

Please let me know if this information is correct.

Thanks Freddy Rojas. Costa Rica

jjallaire commented 6 years ago

@kevinushey Could you look at this today in case we want to take the change for the upcoming release?

kevinushey commented 6 years ago

If I render the following R Markdown document:

---
output: html_document
---

```{r, include=FALSE, eval=TRUE, echo=TRUE}
a = 10
print(a)
a = 10
print(a)


I don't see anything written to the document for each chunk (no source code nor outputs). The chunk documentation states (https://yihui.name/knitr/options/):

> include: (TRUE; logical) whether to include the chunk output in the final output document; if include=FALSE, nothing will be written into the output document, but the code is still evaluated and plot files are generated if there are any plots in the chunk, so you can manually insert figures; note this is the only chunk option that is not cached, i.e., changing it will not invalidate the cache

So I believe `reticulate` is doing the right thing here. For reference, how are you compiling the PDF in your case?