Syntax highlighting for Python chunks does not work - r

I'm trying to create notebooks in which I present the same algorithm written in R and Python. I can easily integrate the R and Python code in a Rmd file and then create a HTML output using the 'Knit HTML' button in RStudio. However the Python code is always displayed without syntax highlighting (not like in the example at https://github.com/yihui/knitr-examples/blob/master/023-engine-python.md). I have Andre Simon's highlight software installed and functioning on my computer (MacBook Pro Retina, MacOS X 10.9).
Currently, I have the following libraries installed in R: markdown 0.6.4, knitr 1.5.15, highr 0.3.1.
Any idea what could be the problem? Thanks for your help!.

That is specific to the RStudio Markdown render, which did not enable syntax highlighting for code blocks that are not R or C++. RStudio uses highlight.js, and you can use your own version of highlight.js to highlight Python code. Or use other Markdown converters such as Pandoc.

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.

How to use inline code in markdown in Jupyter with R

How to use inline code in markdown in Jupyter with R?
CODE CELL
n <- 8
MARKDOWN CELL
The Number is {{n}}
What would be the correct syntax in Jupyter Markdown for R? Is it even possible?
In case you haven't done it already, you might want to give the python markdown extension a shot, that adds the markdown inline code functionality for Python to Jupyter notebooks. On their github they claim:
The Python Markdown extension allows displaying output produced by the current kernel in markdown cells. The extension is basically agnostic to the kernel language, however most testing has been done using Python.
Installation instructions are on the github page of the nbextensions. Make sure you'll enable the python markdown extension using a jupyter command or the extension configurator.
Calling variables then should work inside a markdown cell with the {{var-name}} syntax that you've already given (described in the readme of the corresponding github page (linked in the wiki)).
If this doesn't help, you might want to join the discussion of the corresponding issues in the issue trackers for ipython and jupyter.

compiling legacy Sweave with RStudio 0.99.902

I'm having multiple problems with the “sos” vignette in the “sos” package that has been on CRAN since 2009. I recently noticed that it fails in multiple ways, and I can't figure out how to fix several of the problems. "Compile PDF" on “sos.Rnw” in RStudio with R 3.3.0 under Mac OS X 10.11.5 produced an XQuartz pop-up as follows:
>Sweave Noweb Syntax?
>>It seems you are using the Sweave-specific syntax in line(s)
>>9, 15, <snip>; you may need Sweave2knitr("sos.Rnw") to
>>convert it to knitr'.
I did the following, as it asked:
Sweave2knitr('sos.Rnw')
This produced a file 'sos-knitr.Rnw'. I opened this file in RStudio and clicked 'Compile PDF'. I got one line only:
>Loading required package: knitr"
If I got anything else, I don't know where it went.
Any suggestion on how I can get past this?
Thanks, Spencer Graves
RStudio is using knitr by default instead of sweave to generate the code. Revert this decision by doing the following:
RStudio:
Tools -> Global Options -> Sweave -> Weave Rnw files using: Sweave

R Syntax highlighting from knitr to ghost.io blog

I am starting up a blog for R models and I currently use knitr and R markdown to produce documents. What I have tried to do is just copy the HTML from the the knitr output into the ghost.io blog, and it works perfectly with the exception of the code syntax losing its color.
Does anyone know a way to have my pasted HTML include the colored R code?
Installing Prism to Ghost should enable syntax highlighting for multiple languages, including R and html
For the install instructions, follow this guide:
http://blog.davebalmer.com/adding-syntax-highlighting-to-ghost/

Syntax coloring input using Sweave with TeXShop?

I've just started using Sweave on TeXShop ( which runs on OSX). Is it possible to have syntax coloring of the source code so that I can distinguish between raw text and R-code?
For example, the following code snippet in my Sweave file is the same as the rest of the text, which makes it harder to read than an R editor that has syntax coloring.
<<reg, fig=TRUE, echo=FALSE>>=
a = c(10,15)
plot(a, a)
#
(I have also tried this out using the wonderful RStudio which does have syntax coloring for both R and Latex, but I think TexShop is superior for LaTeX.)
I don't believe TexShop supports mode switching (e.g. highlighting in two different languages--R and LaTeX--within the same file), since it was designed around a single language (LaTeX). Editors that do and therefore properly handle Sweave are to my knowledge:
Eclipse with Stat/ET
RStudio
Emacs with ESS
Actually, you can use highlighted syntax out of the box. Use pgfSweave package. Just add pgf=TRUE (and echo=TRUE) to your Sweave chunk, and you're good to go. Install pgfSweave, and load a package vignette (vignette("pgfSweave")) for further assistance.
Version 1.1.0 of pgfSweave introduced
the highlight option. The default is
TRUE by default so code will be
syntax-highlighted with the highlight
package.

Resources