How to get help on Rmarkdown chunks options? - r

I started to play with Rmarkdown and I'm loving it. But I have newbbie question that, ridiculous or not, I can't find a straight forward answer:
I can't figure where in knitr documentation or elsewhere I can learn what are the useful things to write in the chunks insert to tell R what to plot or not. E.g:
```{r warnings=FALSE, message=FALSE}
Will prevent R messages and warnings to appear in the HTML output. I saw this in another question, and I am wondering how to figure things like that by myself using ? or similar.
Thanks.

Have you looked # the documentation yihui provides on the knitr site?
I also highly recommend checking out his book he recently released dynamic documents with R and Knitr he covers everything you'd reasonably use and then some.
You can also easily explore the options in R studio by creating an R chunk
}```
adding a comma
```{r,
}```
and pressing tab after the comma and it will give you all the potential options - they are well named and you can easily focus your search more thoroughly. For example, if you noticed the `purl` option you could explore more and find it is for extracting R code from the chunk.

Related

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

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.

print the data.table package's .onAttach messages with knitr

I have a bookdown rmd looking like...
Further introductory materials are offered when the package is loaded:
```{r dt-startup, echo=-1, message=TRUE, verbose=TRUE, hide=FALSE}
if ("data.table" %in% .packages()) detach("package:data.table")
library(data.table)
```
My intention was to show the reader the package's startup messages. However, they don't print. Is there some other chunk option to use here?
As you can see, I just threw several maybe-relevant chunk options at it to no good result. I'm not terribly familiar with management of output streams, so that's as far as I knew to go. I also tried calling directly with data.table:::.onAttach(), but no dice.
I'm not sure what else would be relevant here, but ...
Currently the package has not been loaded before this chunk. I just added the first line for robustness in case I rearrange the document.
My before_chapter_script contains nothing but knitr::opts_chunk$set(comment="#").
My knit header value is bookdown::render_book and the output is bookdown::html_book.
Don't. Anything hacked in for this would be fragile and arguably not terribly useful.
Yihui Xie (knitr's author) makes a good case. My synopsis:
This is not useful. You're writing a tutorial, so why include dynamic content (that may change when the package changes)? Moreover, why not point to resources directly rather than to the list of resources printed there?
This is very hard. It is not just a matter of output streams. The messages don't print because they are walled behind an interactive() check. It's not obvious how this should be overridden and, supposing it could be done, what weird side effects that might introduce.

Debugging code in R Markdown

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.

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.

How do I generate reports in R without texi2dvi or TeX installed?

I've been struggling for a week now trying to figure out how to generate reports in R using either Sweave or Brew. I should say right at the beginning that I have never used Tex before but I understand the logic of it.
I have read this document several times. However, I cannot even get a simple example to parse. Brew successfully converts a simple markup file (just a title and some text) to a .tex file (no error). But it never ever converts tex to a pdf.
> library(tools)
> library(brew)
> brew("population.brew", "population.tex")
> texi2dvi("population.tex", pdf = TRUE)
The last step always fails with:
Error in texi2dvi("population.tex", pdf = TRUE) :
Running 'texi2dvi' on 'population.tex' failed.
What am I doing wrong?
The report I am trying to build is fairly simple. I have 157 different analysis to summarize. Each one has 4 plots, 1 table and a summary. I just want
output plot 1,2,3,4
output table
\pagebreak
...
that's it. Can anyone help me get further? I use osx, don't have Tex installed.
thanks
You cannot run this without texi2dvi or TeX installed.
An alternative may be html output -- the hwriter package is useful for that.
That said, if you want to produce pdf out, Sweave is the way to go. Frank Harrell's site has a lot of useful info but all this requires a bit of familiarity with LaTeX so you may need to install and learn that first.
If you are on OSX, might as well install the full tex live
http://mirror.ctan.org/systems/mac/mactex/MacTeX.mpkg.zip
It is a big download, but it will be nice to never have to install additional packages.
Another solution: the ascii package in conjonction to your favorite markup language (asciidoc, txt2tags, restructuredtext, org or textile).
http://eusebe.github.com/ascii/
It may be worthwhile spending a week or so just using LaTeX without R and going through a bunch of introductory LaTeX tutorials.
Thus, when you start producing Sweave or Brew documents and you get errors, you will be better able to identify whether the error is arising from LaTeX or Sweave / Brew.
A couple of Windows tools that make it easy to get started with LaTeX include MikTeX + TeXnicCenter or MikTeX + WinEdt.
Another solution is to try a solution of connecting R to microsoft.
It is much weaker then Sweave, but for basic reporting might be what you need.
You might want to go through the example sessions given here: Exporting R output to MS-Word with R2wd (an example session)
I've also been hearing a lot of good things about the knitr package. It seems to resemble Sweave a lot, but add some more to it. I would definitely take a look at it.

Resources