How does the knitr language engine for Python in RMarkdown keep track of variables across code chunks? - r

I was looking at the source code of knitr in order to better understand how the language engines work, and it seems that evaluating code in a single chunk is a relatively simple matter, but what I have trouble understanding is how it is possible for the Python chunks to call the variables across chunks. I get the impression from earlier packages by Yihui that attempts were made to do this with sockets, but it seems that the current code for both reticulate and knitr does not contain code for sockets, hence my question.

This is done through the reticulate package instead of runr. You may see Section 2.7.1 of the R Markdown Definitive Guide for details.
P.S. runr was a very early experiment and never officially used in knitr.

Related

Execution of Rcpp chunks in RStudio

The knitr language engines makes it possible to include an Rcpp chunk in your R Markdown document. This works perfectly when knitting the entire document.
However, it doesn't seem to be possible to execute (that is, compile) the Rcpp chunk interactively in RStudio (v. 1.1.364), or am I missing something?
I could keep the C++ code in a separate file and use sourceCpp in a chunk, which also works fine. However, for small examples I use in teaching it's more convenient to have everything in one document. I could then use cppFunction, but that doesn't give proper syntax highlighting.
I'm either looking for an answer that shows that I, indeed, missed how to interactively compile Rcpp chunks in RStudio, or answers that suggest good practices for i) having all code in one file, and ii) being able to execute chunks interactively.

Syntax highlight in R Sweave

I am fairly new to doing report with R Sweave and know the very basic applications of Latex. And I have been asked to produce some statistical reports. The R markdown is great and simple, and by default it has really nice syntax frame and grey background and syntax highlights, however, it is quite limited in terms of other type setting, not really optimal when you want to produce lengthy reports. Then I am switching to use R Sweave in R studio.
I basically want the same after-effect similar to R markdown in the Sweave. What are the easiest ways to do it? I have previously read the following post discussing:
Sweave syntax highlighting in output. And I have tried reading those package pdf, but have no clues what they are talking about, as they seem to assume readers have prior knowledges about the rendering process.
i have checked them out, but I seem to get stuck in making it to work. Can anyone tell me step by step on how to set it up (such as what to include in preamble), if possible can you kindly upload a simple Rnw file with a demonstration?
Thank!
If you use knitr rather than Sweave, you'll get syntax highlighting. It's probably possible to do it in Sweave, but knitr makes it easier.
Go to your Tools | Global Options | Sweave menu (or the similar one in Project Options) in RStudio, and choose to Weave Rnw files using knitr.
The two systems are very similar, but knitr is generally preferable these days.

Can Roxygen really document R script (not a package) just like Doxygen does for C++?

Roxygen is inspired by the Doxygen documentation system that is used by C, C++ programmers. I have used Doxygen and I find it really easy to document any program as long as you have the doxygen comment. It also generate call graphs for functions and classes. I thought roxygen would work the same way but when I search for roxygen help, I only find solution to documenting R packages.
I have checked Hadley Wickham's online roxygen2 help but that does not describe anything about the R script documentation.
My scripts sometimes become 500-1000 lines and have several functions which I always document with the comments. I want to generate PDF or HTML documentation with graph-viz diagrams. Is Roxygen capable of making call-graph and document standalone R scripts?
No, roxygen2 will work only for writing package documentation. Sounds to me you're after a report generating tool. You can use knitr for that. You can include code, comments, text, MathJax, or even use LaTeX. It also supports table of contents and references. This is by no means an exhaustive list of functionality. With some logical limitations, you can produce pdf and html documents, among others.
Though this is an old thread, for future reference, the following library is able to achieve that:
https://rdrr.io/cran/document/
From its docs: Have you ever been tempted to create 'roxygen2'-style documentation comments for one of your functions that was not part of one of your packages (yet)? This is exactly what this package is about: running 'roxygen2' on (chunks of) a single code file.

Compiling *.Rnw files with knitr --without Rstudio

I would like to use knitr to create presentations that embed R objects and code.
For IT reasons I am restricted to vim, so i have found the available Rstudio+knitr examples fairly unhelpful. The vim section of the knitr documents is also very skinny, and therefore unhelpful.
Is someone able to provide some guidance on how to compile a *.Rnw or *.Rmd file using knitr (or alternately point me to a decent online tutorial?) using some combination of vim, R, and the command line?
thanks in advance
Instead of going through Rstudio, you can use the functions in the knitr package directly. There are some options you can tweak, but to get started, all you have to do is call the knit() function on your .Rnw file:
library(knitr)
knit('my_input.Rnw')
If you're missing some of Rstudio's features, it's worth remembering that most of them are just making use of things that are already available in various R packages, so you can usually find a way to use them when you don't have Rstudio available.

R Programming brew vs. Sweave-- suppressing output and brew limitations

I am a newbie to the R world-- a couple of weeks. I have been tasked to automate the generation of a database codebook. The thought was to use R, LaTex, and Sweave as a solution. Because of the repetitive nature of the codebook tables and the use of looping over code chunks, Sweave may not be a viable means. It looks like the brew package could be an alternative. As I have been doing some testing with brew, I'm running into an issue with loading in the RMySQL package (via library(RMySQL)) as I cannot suppress the output--
Looking in C:Files/MySQL, C:/MySQL, D:/MySQL, E:/MySQL, F:/MySQL,
G:/MySQL, C:/xampp/MySQL, D:/xampp/MySQL, E:/xampp/MySQL, F:/xampp/MySQL,
G:/xampp/MySQL, C:/Apps/MySQL, D:/Apps/MySQL, E:/Apps/MySQL, F:/Apps/MySQL,
G:/Apps/MySQL Found C:Files/MySQL
I have tried to set the Library function arguments verbose=TRUE and quietly=TRUE but that did not work. In Sweave, I am able to suppress by setting the code chunk option to results=hide. Is there any alternative to suppress this output information when using brew?
In determining whether or not brew is a good solution for the codebook generation, should I be aware of any limitations of brew as compared to Sweave?
To supress messages during loading of a library:
suppressMessages(library(RMySQL))
Also, definitely take a look at the knitr package. It roughly does what Sweave does, but then better. It supports caching of objects (which kept me from heavily using Sweave), and much more flexibility. Take a look at the website, or on a blogpost of mine showing a simple example of a presentation in Latex made using knitr and beamer.

Resources