use of tikzdevice in RMarkdown with beamer output - r-markdown

I was wondering whether tikzdevice package can be used for producing R plots in RMarkdown, to be exported to a beamer presentation.
I would have thought that the solution I am looking for might be similar to the example with Sweave on page 15 of the tikzdevice manual.
What I have tried, without knowing exactly what I am doing, is to amend Yihui's example from the RMarkdown book:
---
title: "Habits"
author: John Doe
date: March 22, 2005
output: beamer_presentation
---
# In the morning
## Getting up
- Turn off alarm
- Get out of bed
---
```{r, echo=FALSE, results='tex'}
require(tikzDevice)
tikz(console=TRUE)
plot(sin, -pi, 2*pi, main="A Stand Alone TikZ Plot")
dummy <- dev.off()
```
Unfortunatly, this does not result in the tikz plot being rendered in the beamer presentation. Does anyone see how this can be achieved? Thanks.
Regards,
Michael

There are a few problems in what you wrote. LaTeX needs to be told to use the tikz package; and in knitr, you use results='asis' instead of results='tex'. Finally, you would need to tell tikz() what size figure to use.
However, rather than fix those things, it's easier to tell knitr to handle everything, by using the 'tikz' graphics device. So this works:
---
title: Demo with dev='tikz'
output: beamer_presentation
---
## A Tikz plot
```{r echo=FALSE, dev='tikz'}
plot(sin, -pi, 2*pi, main="A Stand Alone TikZ Plot")
```

Related

Why won't RMarkdown with Bookdown recognize Tufte formatting?

I am working on a Bookdown project and would like to use Tufte formatting.
I downloaded the Bookdown demo and the tufte package. Then, I simply changed the YAML in index.Rmd to:
---
title: "A Minimal Book Example"
author: "Yihui Xie"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
output:
bookdown::tufte_html_book:
toc: yes
css: toc.css
documentclass: book
bibliography: [book.bib, packages.bib]
biblio-style: apalike
link-citations: yes
github-repo: rstudio/bookdown-demo
description: "This is a minimal example of using the bookdown package to write a book. The output format for this example is bookdown::gitbook."
---
Where the only change is in the output: option, which is specified by the Bookdown book
And then tried both the Knit button and bookdown::render_book(). The demo built successfully.
However, it did not produce Tufte formatting. Instead I get default formatting, with a white background, non-italic section headings, and footnotes instead of margin notes (I added a footnote to the doc on a second run to make this visible):
I also tried this with different spacing before the "bookdown"/"toc" etc. stuff, and with tufte::tufte_html: default instead of any of the "bookdown"/"toc" stuff.
What do I need to do to get Bookdown to recognize the Tufte formatting request? I feel like I am missing something exceedingly simple.

Inline R Logo command in R markdown

Having monkeyed around with the recommended solutions of Add inline image in Rmarkdown as applied to the problem of wanting to display an inline R logo, (for example https://images.app.goo.gl/4C57fs4D6AtS3D9D6) I found that while the recommended solutions didn't work for me for some reason, I could get an approximate solution to my problem using the command ![](R_logo.png){#id .class width=auto height=16px}.
Being an avid user of LaTeX, though rather newly introduced to R Markdown I thought it prudent to try to define the inclusion of the image as a command in a LaTeX-like fashion as recommended by R Markdown similar feature to "newcommand" in LaTex?, as I might be using it from time to time.
R Markdown seemed rather ambivalent to this approach however, having defined the command with \newcommand{\Rlogo}{![](R_logo.png){#id .class width=auto height=16px}}.
Do you have any recommendations as to what I'm doing wrong, and how/If I might define such a command within R markdown?
The icon package works really well to insert icons.
Here is an example, extracted from the package's page:
---
title: "Untitled"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(icon)
```
# Icon package
The `ìcon` package works really well:
`r icon::fontawesome("r-project", style = "brands")`
For a bigger icon:
```{r icon-style}
icon_style(fontawesome("r-project", style = "brands"), scale = 4)
```
-output

DiagrammeR and R Markdown pdf: How to crop to the diagram?

I have made a nice flowchart in DiagrammeR. I have also plotted it into R Markdown and I am able to knitr it into a pdf. As you see in the following screenshot, the figure is not centered (even though I included fig.align = 'center'). But there is also a huge gap between my figure and the following text. How do I "crop" the diagram so I removes the space and center the diagram?
You will find problems generally with markdown problems so you will have to modify manually from your latex code so I would recommend that you put in your yaml:
---
output:
pdf_document:
latex_engine: pdflatex
keep_tex: true
---

RMarkdown - Change Inline Code Color *without changing workflow*

I teach an introductory statistics course using R Markdown in RStudio (Server). We have students knit to html_notebooks, and we often have them use inline code to report various elements of their statistical analyses. It'd be really helpful for grading purposes if we could have the result of inline code output in a different color -- that way we could easily see if they were indeed using inline code or if they copy-pasted a number from their output into raw text.
There's a couple of ideas for solutions posted here, but these won't super work in my case. These are introductory students who are generally kinda afraid of RStudio to begin with, so asking them to do anything complicated with text_spec or sprintf will likely cause mild riots. I really need something that won't change students' workflow at all.
I wonder if there's any way to configure things either on the backend in RStudio Server (maybe by messing with knitr?), or through some kind of <style> tag wizardry in the preamble, so that inline code will print its results in a different color.
Thanks!
EDIT: Arthur Berg below has provided something that's almost exactly what I need. Here's a MWE:
---
title: "test knit_hook"
output: html_document
---
```{r, setup, include="FALSE"}
knitr::knit_hooks$set(inline=function(x){paste0("<span style=\"color: #0000FF;\">",
x,"</span>")})
```
`r pi`
The only issue with this is that it doesn't work if I change to html_notebook in the YAML header and thus use the "Preview" button in the RStudio IDE. For external reasons, it's important for us to have the output type as html_notebook. Anyone know how we might modify this to get it to work with html_notebook?
A way to achieve this without changing the workflow too much is to create your own format (e.g. html_notebook2) that is derived from the original but modifies the inline hook of knitr.
To get started you can check out this document.
Basic steps include
Create a new R package
Within this project run usethis::use_rmarkdown_template(). This creates the folder structure for your new format.
Edit skeleton.rmd and template.yaml
Define your format in a R file which has the same name html_notebook2.R(kind of a convention).
The content of the html_notebook2.R file could be
#'#import knitr
set_hooks <- function() {
default_hooks <- knit_hooks$get()
list(
inline = function(x) {
paste0("<span style=\"color: #FF0000;\">", x,"</span>")
})
}
#' #importFrom rmarkdown output_format knitr_options pandoc_options html_notebook
#' #export
html_notebook2 = function() {
output_format(
knitr = knitr_options(knit_hooks = set_hooks()),
pandoc = pandoc_options(to = "html"),
clean_supporting = FALSE,
base_format = html_notebook()
)
}
In the first part we define a new inline hook which only changes the font color.
The second part is the definition of the new format.
After building and installing the package you can create a new rmarkdown document and use output: packagename::html_notebook2 as the output format. All inline code output will be colored red using my code. Here is an example:
---
title: "Inline"
output: cformat::html_notebook2
---
## R Markdown
`r pi`
I created such a package and you can find it on GitHub. Feel free to copy it and rename it (cformat is a pretty lame working title ;) ).
Notice though that your students could change the color manually using HTML/CSS anyways. A way around could be some kind of key generation using a certain rule (unknown to the students obviously). For each inline chunk a key is generated and embedded using
paste0("<span code=", key," style=\"color: #FF0000;\">", x,"</span>")
If a valid key is embedded, the output was generated using R and not simply copied.
For those looking for a fix to a simple R markdown document, adding this line changes the inline output to blue.
knitr::knit_hooks$set(inline=function(x){paste0("<span style=\"color: #0000FF;\">", x,"</span>")})

RMarkdown to PPT: not loading reference_doc()

I am trying to use a PPT template to format a RStudio > RMarkdown > PPT file. Here's my testing file (saved as testing.Rmd):
---
title: "Aarduous Aardvarks"
author: "Aanonymous Aardvark"
date: "5/4/2019"
output: powerpoint_presentation
reference_doc: template.pptx
---
## My title
```{r myplot}
plot(mtcars)
```
When I first knit this, a PPT file was generated. I opened that file, made the Slide Master background red and the font blue (deliberately hideous for testing), saved it as "template.pptx" in the same folder as above.
However, when I re-knit the document, the background is still white and the font is still black. What do I need to get my template file recognized?
System:
Windows 7 Professional
Rstudio 1.2.1335
Rmarkdown 1.12
Pandoc: 2.6
PPT 2010 v14.0.6129.5000
Indentation in YAML is important. Use the structure as described in the official docs:
---
title: "Aarduous Aardvarks"
output:
powerpoint_presentation:
reference_doc: template.pptx
---
I have had this same problem, but it does not repair when the YAML above is inserted. The additional changes helped me solve the problem:
Save the reference_doc as a .potx file format, and refer to that extension in the YAML header
Go into View -> Slide Master to make sure the slides in the master are the format you expect (one time I thought I made the changes, but they showed up on the Home menu and not the Slide Master)
Under the Home menu, click the Dropdown under Layout and make sure AT THE LEAST that you see these four elements:
Title
Section Header
Title and Content
Two Content
When all three of these are taken care of, my slides knit fine from the Rmd.
I was facing a similar error when knitting to a powerpoint template- the indentation as documented (indenting 'reference_doc:...') was giving me an error: "Scanner error: mapping values are not allowed in this context", which suggested that the indentation was wrong in the YAML. I was able to resolve (through trial and error) with the following formatting:
---
title: "TITLE"
author: "Mike Harris"
date: "4/9/2021"
resource_files:
- template.pptx
output:
powerpoint_presentation:
reference_doc: template.pptx
---
Hopefully this saves someone a little extra time.