Debugging code in R Markdown - r

I've been playing a little bit with R Markdown and I really like it, because after each of my analyses I need to write a (Word) report detailing the results. With R Markdown, the code and report are the same document, so it's easy to remember that I chose to perform analysis B at a certain point in the code, because of the results of analysis A before.
However, I'm not able to set breakpoints in R Markdown inside R Studio, and this limits severely the possibility of performing efficient debugging. This implies that in the end I can only use Markdown for very trivial analyses, where the R code is so simple that either I don't have to debug it, or I can debug it manually. It this to be expected, i.e., is it well-known that R Markdown can only be used for simple analyses? Or is there a solution?

You can use browser() in your code to set breakpoints. See https://support.rstudio.com/hc/en-us/articles/205612627-Debugging-with-RStudio#debugging-in-r-markdown-documents
I've find it more useful to write custom functions in a .R and source() it in Markdown. Then, you get the best of both worlds.

You can generate Word and other reports from normal R scripts using knitr::spin in combination with special roxygen2-style comments. Then you can run your script and use breakpoints as normal.
https://rmarkdown.rstudio.com/articles_report_from_r_script.html
There's also a toolbar button in RStudio to do this.

Related

Printing an R code and output using knitr

I'm trying to print an R-code and its output. In the past I remember using a function called knit for this. Looking online it seems it may be part of the package knitr (although I seem to remember there being a button in the file dropdown which did the same).
However I can't work out a simple way to use this, and what functions produce a MS Word output.
Just wondering what the simplest way is to print the r-code and output?

Stop Sweave code chunks in RMarkdown from displaying execution output whilst still displaying the code

I'm writing a publication manuscript for a new R package. The author guidelines expect a self-contained Sweave (Latex+R) project with self-contained executable code within R code chunks in the Sweave document. This allows for seamless reviewing. Recommendations are to use RStudio.
All is going well. However, some of my packaged R code prints to the terminal intermediate steps; notably in the parts that setup and execute parallel code. In terms of use, this is great. However, the intermediate output is bulking out the code chunks in the compiled PDF. Not great for a scientific manuscript with a limited page count (fine elsewhere, e.g., Github wiki etc).
I'm using the code chunk options:
<<eval=T, echo=T>>=
#R code to execute AND to display code here.
#But this print all internal R print() statements to the pdf document.
#
Is there a Sweave code chunk option (not a global option, as for some code chunks the current behaviour is fine) that executes and displays the code itself but halts the printing of any internal print statements in my R package?
In answer to my own question I figured this out through a process of elimination; I could have continued scrolling through online blogs and tutorial but I'm very much pressed for time.
To suppress the output of a calculation in an R code chunk whilst displaying the R code in the compiled pdf:
<<eval=T, echo=T,results=hide>>=
eval=T -- evaluate the code
echo=T -- spits the code into the pdf (and the code output)
results=hide -- overrides echo=T to prevent the code's output whilst maintaining the code display.

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.

Print Script with syntax highlighting R-Studio

After finishing statistical analysis in R I always like to print out the script since it gives a good overview and one can adjust eventual errors. I like the syntax highlighting in R-Studio because it facilitates reading and fast understanding within huge lines of code. So is there anyway I can print out the text with the highlighting I am used to see in the editor?
Try using knitr to produce a pdf (or some other output).

Write markdown documents with R code that doesn't work, on purpose

I'm experimenting with using Markdown to write homework problems for a course that involves some R coding. Because these are homework sets, I intentionally write code that throws errors;. Is it possible to use Markdown to display R code in the code style without evaluating it (or to trap the errors somehow)?
If you're using R markdown, putting eval=FALSE in the chunk options should work. Or use try(). Or, if you're using knitr as well, I believe that the default chunk option error=FALSE doesn't actually stop the compilation when it encounters an error, but just proceeds to the next chunk (which sometimes drives me crazy).

Resources