Syntax highlight in R Sweave - r

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.

Related

IDE with LaTeX and R support: Inline output in .Rmd notebooks and weaving LaTeX document with R code

I'm trying to improve my workflow when working with R and generating documentation. I've been going between TeXStudio, JupyterLab and RStudio for a while, and I'm trying to improve my workflow. TeXStudio has limited R support, and RStudio limited support for LaTeX.
VS Code has support for multiple languages, including R and LaTeX. The fact that it can run both Jupyter notebooks, R notebooks, and LaTeX, and has plugins for other languages as well, makes it seem desirable. However, I am unable to find documentation on how to configure it to work with R and LaTeX code in the same file. In addition, I am unable to configure R notebooks to allow inline code execution output.
However, I am unable to (a) set up code execution output under the code for .Rmd notebooks, and (b) I can't figure out how to weave .Rnw (R/LaTeX) documents with Sweave/knitr.
I'm trying to find an IDE that would include features like:
Markdown, code and code execution output in the same document
Auto R and LaTeX code completion
Automatic display of R function documentation
Spell check
Simple R console access
Compile .Rnw
Syntax highlighting for both R code and LaTeX code
I am, primarily, requesting ways to configure VS Code, or, secondly, way to configure another IDE that can meet my requirements. A tutorial on this would be much appreciated.
After a bit of digging around, I found that VS Code does nearly all the things I need.
Auto R and LaTeX code completion, Display of R function documentation in a tab in VS Code, Simple R console access, and Syntax highlighting for both R code and LaTeX code:
The R and LaTeX Workshop extensions, will provide highlighting and autocompletion of code in both languages. By installing R, you can easily open a session in a terminal window in VS Code, and from there open documentation inside VS Code.
Spell check
Code Spell Checker offers spell check for multiple languages. Install the extension and any desired dictionaries, and set the langauges you want to be included in the extension settings.
Compile .Rnw files
Turns out LaTeX Workshop can actually do this by default.
Markdown, code and code execution output in the same document
This is the only thing VS Code doesn't do as far as I can tell. It can compile .Rmd files, however, but the output can only be seen in the compiled PDF. I consider this less important, since I can use Jupyter notebooks instead.

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.

How to get help on Rmarkdown chunks options?

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.

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).

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