Markdown and outline an R script - r

I am using Emacs for an R-script with markdown using oxygen comments. With rmarkdown::render() I produce either a HTML or pdf file. Works great.
However, it would be great if I could use outline mode for headings in this file (coloured differently and collapsing or showing stuff). I have searched the internet but haven't found a solution, how to make this work.
An example would be (R-File):
#' # Heading 1
#' some test
t <- c(1,2)
#' ## Heading 2
#' some more text
s <- c(1,2)
It would be nice, if I could collapse it and have different colors for the heading levels.

HTML
One possibility is to use knitr package to render the R markdown.
In addition it is possible to feed it a CSS file that will allow you to style headings (like colors). This answer Custom CSS with knitr and markdown in R may be useful to help you achieving this.
If you are familiar with css, you can always add the collapsing effect. If you aren't I suggest to have a look at this: http://www.webdeveloper.com/forum/showthread.php?77389-Expand-Collapse-Paragraph
Twisting the idea around would be to use Shinypackage to create an interactive page with R code, however it is a bit far-fetched considering that it started from a Markdown.

Related

markdown insert tabulator (R)

Is there a way to insert tabulators in R markdown code?
I just want to show sth. like
attendees: 33
sick leave: 1
presenters: 5
Text is Markdown text, numbers are inline R code. Simply putting spaces does not work, since there is a variably spaced font.
Ideally working for both HTML and PDF output. (I work on Win-10 in RStudio)
All my searches ended up in (a) creating markdown tables (too big), or (b) how to create tabs for tabbed browsing, much more complicated. I tried \t and \t, does not work.
I use either the RStudio add-in BeautifyR which has a nice Beautify Table. Got the top banner under Tools there is the Add-ins button load BeautifyR.
Then there is also using Kable. Look for the R packages and install: knitr::kable and kableExtra.

Changing keywords to highlight in an RMarkdown document

I have been writing a document in bookdown where within the *.Rmd file I call a figure by using the following syntax
\#ref(fig:MyFigureName)
This differs slightly from the notation that you would use in a normal RMarkdown file exporting to latex which would be
\ref{fig:MyFigureName}
The issue I am running into is that when I write something in bookdown the function calling the Figure is not being highlighted properly (see image below).
I have imported my own rsTheme (which is from my understanding, basically a .css file) but I don't see an option to add keywords to highlight.
But I would like for the entire function to be colored differently from the inline text (Photoshop version of desired output shown below)
Does anyone know how I would edit my *rsTheme file in order to accomplish this?
Thanks!

Stargazer in a bookdown site

I'm trying to show Stargazer tables (both for regressions and data summary) in a bookdown based website. stargazer output does not look like it looks in outside bookdown (i.e markdown/knitr/html). It has spaces between rows, rows are zebra striped (abit like bootstrapped theme)
I believe that the style.css overrides stargazers' known table format, however I couldn't find any evidence to that, nor couldn't modified it by myself.
I'm sorry for asking without reprex, I find it a bit hard to make reprex to a book. Anyway, stargazer chunk is:
```{r ,results='asis'}
stargazer(lm1, type=`html`)
````
So to problem is that stargazer tables are wrapped again according to the styling in the "plugin-table.css" file that located in
"./_book/libs/gitbook-2.6.7/css/plugin-table.css".
I get rid from the Kable-like formating when I remove this file it after rendering the book with these command:
bookdown::render_book("index.Rmd","bookdown::gitbook", clean_envir = TRUE) # render to HTML
file.remove("./_book/libs/gitbook-2.6.7/css/plugin-table.css")
Of course, this is not the best practice, this solution hurts kableextra formating for example. However, as I tend to use mainly Stargazer, I don't find it that bad.

Aligning XTable Output in R Markdown

I am putting an assignment together in R Markdown to PDF (through Latex) and been using xtable to nicely format my output. The below code generates a small table that I would like to align right in the document and have text wrap around it. Is there a way to achieve this?
summary.table <- xtable(ddply(aircon, ~when, summarise, Mean=mean(hours), StdDev=sd(hours)), caption="Mean and Standard Deviation (in Hours) Before and After Change")
print(summary.table, include.rownames=FALSE)
Given the update regarding using LaTeX to do the wrapping, how could I write this in R Markdown to take advantage of this? I have tried to print inline using r <code> but that didn't work.
I guess you could add the begin/end wraptable commands in the r-chunk that creates the table, like this:
cat("\\begin{wraptable}{r}{5.5cm} \n")
...
print(summary.table,...)
cat("\\end{wraptable} \n")
You can use the Includes mechanism documented here, to include a custom header.tex which would contain usepackages, etc.

R2HTML or knitr for dynamic report generation?

I want to write an R function which processes some data and then automatically outputs an html report. This report should contain some fixed text, some text changing according to the underlying data and some figures.
What is the best way to go?
R2HTML or knitr?
What are the advantages of one over the other?
As far as I understood R2HTML allows me to build the html file sequentially while knitr already operates on an predefined .Rhtml file.
So, either use R2HTML or stitch and spin from knitr for on the fly report generation.
I would appreciate any suggestions or hints.
I grab this nice opportunity to promote pander a bit :)
This package was written for similar reasons like #Yihui's great knitr, although I wanted to let users really concentrate on the text and R code without dealing with chunk options etc. So letting users generate pretty HTML, pdf or even docx or odt output automatically with some predefined options.
These options affects e.g. the cache engine (handling dependencies without any chunk options) or the default plot options (let it be a "base" R graphics, lattice or ggplot2), so that you do no thave to set the color palette or the minor grid in each of your plots, just once - or live with the package defaults :)
The package captures the results (besides errors/warnings and other messages and the output) of all run R expression and can convert to Pandoc's markdown automatically. There are some helper functions that let you convert the resulting document written in a brew-like syntax automatically to e.g. HTML if you have pandoc installed, or export R objects to markdown/HTML/any other supported format in a live R session with a reference class.
Short demo:
brew file
Pandoc.brew('file_name.brew', output = 'foo.html', convert = 'html')
HTML output
knitr, every time. Handles graphics, lets you write your report with markdown instead of having to write html everywhere (if you want), caches things, makes coffee for you etc.
You can also build an HTML file sequentially as long as you have a decent text editor like Emacs/ESS or RStudio, etc. R2HTML is excellent in terms of its wide support to many R objects (see methods(HTML)), but I'll probably frown on RweaveHTML() due to its root Sweave().
That said, I think it may be a good idea to combine R2HTML and knitr, e.g.
# A LOESS Example
```{r loess-demo, results='asis'}
cars.lo <- loess(dist ~ speed, cars)
library(R2HTML)
HTML(cars.lo, file = '')
```
I was using the R Markdown syntax in the above example. The key is results='asis' which means to writing raw HTML code into the output.
I believe that you can also use Sweave to create HTML files, though I have heard that knitr is easier to use.

Resources