R Markdown conditionals for knitting HTML vs PDF - r

In LaTeX I can create conditionals in the following manner
\iftoggle{ebook}{
\newcommand{\textbreak}{\newline\hrule\newline}
}{
\newcommand{\textbreak}{\begin{center}\LARGE{$\Psi\quad\Psi\quad\Psi$}\end{center}}
}
Can I do the same while knitting R Markdown, depending on if the output, is say HTML or PDF.

If you just need to include a short command in your target format, then you could use raw elements for your target format:
`<br><hr><br>`{=html}
`\begin{center}\LARGE{$\Psi\quad\Psi\quad\Psi$}\end{center}`{=latex}
The first line will only be included in HTML formats (like epub), while the latter will be used when exporting to or via LaTeX.
For longer text, or if you don't want to write in the target format directly, I'd recommend using fenced divs in combination with a pandoc filter, e.g. a Lua filter; this works both with raw pandoc as well as with RMarkdown.

Related

How to pass options to a LaTeX font in R Markdown?

rmarkdown, following {xe|lua}latex, allows to specify fonts for main text, sans-serif text, monspaced text (most notably code chunks !) and math fonts in the YAML header. At least for PDF rendering via xetex, this works.
However, I found no (documented) way to pass options to the underlying setxxxfont \LaTeX command. For example, the YAML fragment :
```
monofont: Inconsolata
```
generates the following \LaTeX fragment :
\setmonofont[Mapping=tex-ansi]{Inconsolata}
I have two questions with this:
why is the Mapping=tex-ansi added ? And how to control it ? (I'm working in UTF8...).
How could I set additional arguments for the font options i.e. \setmonofont[Scale=0.91]{TeX Gyre Cursor}?
The R Markdown book and the Pandoc's User's Guide did not reveal anything pertinent.
Background:
When R Markdown converts the knitted code to the output format (PDF), a pandoc template is used. The template is stored within the package, and any variables which get replaced by the YAML variables are contained in the $$ Notation
1. Encoding
The Mapping=tex-ansi is added to the code as a workaround an issue as reported on GitHub. Therefore I would be cautious of deleting this for potential side effects.
If you do indeed wish to change this code, you will have to make a copy of the LaTeX template file used to convert the document. You can find the default template here. See here for some more information on providing custom templates.
2. Additional Font Options
You can use the monofontoptions YAML argument to add additional arguments to the font options.
Documentation of the variables which can be parsed by the LaTeX output are available in the pandoc documentation

Formatting R output in an R Markdown documents

I have a code that generates a text output. For example:
pick <- sample(c("Alex", "Greta", "Zoe"), 1)
Is there a way of formatting the result in an R Markdown documents.
Say something like this if I wanted it to appear in bold on the report.
**pick**
You can execute any R command in-text in an rmarkdown document without embedding it inside a chunk by placing it within ticks prefaced by the letter r (see below). You can then surround this r code with latex code, in this \bf{foo} for bold font, bookmarked by $. Try placing
The OP's chosen name is $\bf{`r sample(c("Alex", "Greta", "Zoe"), 1)`}$
inside an rmarkdown document. Or you could place the function in a chunk and then call it outside the chunk, anywhere in subsequent text, like this
The OP's chosen name is $\bf{`r pick`}$

Define commands for frequently used text in knitr

Is there a way to define a command that can be used as a short cut for frequently used text or html commands in knitr when compiling to html?
I use knitr to compile an rmkardown file (.Rmd) and the output is a html file (i.e., I press Knit HTML in RStudio).
To be more specific, let me add an example: I want to separate the percent sign by a hair space from the number before, which I achieve by typing, e.g., 5 %. It would be very convenient, if I could define a command, let's say \perc, that I can use instead, such that 5\perc would be equivalent to 5 %.
Is this at all possible and if yes, how can it be done?
You can define an R function and then call it inline. For example:
```{r}
perc <- function(){
" %"
}
```
This is inline r code 5`r perc()`
I think you could also use it in chunks where the result would be 'asis'.

Converting Tex file generated by Knitr into .doc

Why is it important? My collaborators would like to be able to modify my .pdf reports and they are not able to modify the actual .tex code. They are sooo used to word !
How I write my reports? I use knitr. What a great and useful tool; thanks Yihui :)
What I'm looking for? convert the generated pdf report into .doc (my collaborators favorite !)
a solution might help: Pandoc (http://johnmacfarlane.net/pandoc/). As it's explained in the introduction of page of Pandoc, it can be used to convert LaTex to .docx and to markup. I then thought converting the .tex file of each of my reports (a .tex file is generated when I run my knitr .Rnw file) and convert it to .docx using pandoc:
pandoc -s myTexRepot.tex -o aDocReport.docx
issue: 1) Figures are missing in my .docx file. 2) I generate all my tables in my reports using xtable(), none of them can be converted! 3) Also, I lose all table of contents, section numbering.
Markup to Word: Issue (1) above is still an issue in converting Markup code to .doc
Is there any better idea to approach the issue of converting latex to doc. If Pandoc is the best, would you know how I can solve the issues above?
I really appreciate your help.
since you are already using knitr, you can call the rmarkdown:render() function to knit the document.
there is a parameter in render() called output_format[1]. the parameter along with the YAML header in your knitr document will allow you do generate the pdf and word document simultaneously
[1] output_format
R Markdown output format to convert to. Pass "all" to render all formats defined within the file. Pass the name of a format (e.g. "html_document") to render a single format or pass a vector of format names to render multiple formats. Alternatively you can pass an output format object; e.g. html_document(). If NULL is passed then the output format is the first one defined in the YAML metadata of the input file (defaulting to HTML if none is specified).

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