Print Script with syntax highlighting R-Studio - r

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

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?

Jupyter/IPython: how to get results in traditional mathematical notation?

I have been using WxMaxima for my symbolic calculations for a while now. The good thing about WxMaxima is that you can get formatted outputs right in the program and then export them to LaTeX format with a click of the mouse.
Now I want to try the Jupyter/Ipython plus sympy for multiple reasons. I know how to use display(Math(r' some LaTeX math here ')) but what I want is to have the result/output of a cell in a nice mathematical form; something like the TraditionalForm[] command in Mathematica.
I would appreciate if you could help me know if/how I can get that right in a Jupyter notebook?
I think I found the proper solution and it is a sympy feature rather than Jupyter/IPython one. As explained here:
If all you want is the best pretty printing, use the init_printing() function. This will automatically enable the best printer available in your environment.
and
In the [Jupyter/]IPython notebook, it will use MathJax to render LATEX.
Then one can right click on the output and select Show Math As > Tex commands:
to get the LaTeX output.
P.S. A more proper formatting can be achieved via galgebra library. I will look into that and add it here later.

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.

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