Inline R Logo command in R markdown - 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

Related

How to share Rmd files between different bookdown projects?

I'd like to make multiple bookdown projects that have particular chapters in common. For example I would like the content of a particular file content_in_common.Rmd to be merged in to two different books when they are built. How can I achieve this? I understand that bookdown requires the Rmd files to be in a common working directory, so having
rmd_files: ["index.Rmd", "path_where_common_content_is_stored/content_in_common.Rmd"]
in my _bookdown.yml doesn't work because it returns
Error: Input files not all in same directory, please supply explicit wd
Execution halted
Is there a solution to this (short of manually making duplicate versions of content_in_common.Rmd)?
I found this link that suggests you can use
{r child = 'chapter1.Rmd'}
such as
---
title: My Report
output:
pdf_document:
toc: yes
---
```{r child = 'chapter1.Rmd'}
```
```{r child = 'chapter2.Rmd'}
```
To use other .Rmd files inside your bookdown project hopefully

Bookdown: Password protect a *single* page/chapter in HTML

I am producing a Tutorial Workbook for my class using bookdown. I have a large class (500+), and so have a couple of other people helping me with the course.
So I would like to produce the answers for these tutorial questions.
I can produce a whole new document... but then it would be tricky to (automatically) cross-reference the exercise numbers.
So I wondered: Is there a way to password-protect a single page, or a single chapter, in bookdown? (Thinking HTML here; in PDF I can just not include that page/chapter.)
Is this possible? If so how? If not... any other ideas...?
P.
This solution does not use a password, but since you say that for PDF you can simply distribute a version that does not include the material in question, perhaps the following simple approach might help
Inspired by this question on how to conditionally input material as well as the option to use parameters in Rmarkdown, consider two Rmarkdown files:
main.Rmd, which contains what you want to show everyone.
protected.Rmd, which should only be shown to some people.
These files look as follows:
main.Rmd:
---
output: html_document
params:
include:
label: "Include extra material?"
value: ""
input: select
choices: [True, False]
---
```{r, include=FALSE}
print(params)
show_all <- as.logical(params$include)
```
```{r conditional_print, child="protected.Rmd", eval = show_all}
```
protected.Rmd:
Hello World!
Assuming you are in RStudio, if you choose "Knit with parameters" on main.Rmd, you will be asked to select either TRUE or FALSE from an interactive dropdown. If and only if you choose TRUE, the output will include "Hello World". More generally, code blocks with eval = show_all will only be displayed when selecting to include extra material. Therefore, you can of course have multiple sections (each contained in a separate .Rmd file) that are only conditionally included.
In this way, you could knit the same document twice: once with questions only, and once with questions and answers both. Since this is the same for both pdf and html, it also gives you a consistent workflow for both of these output types.

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>")})

Compiling reveal.js-based R markdown presentation without RStudio

Fairly recently, RStudio has added support for beautiful reveal.js-based HTML presentations generated from RMarkdown (with some extensions). These are different from earlier HTML presentation formats provided by the rmarkdown R package, which relied on ioslides or Slidy.
Is it possible to compile such a presentation to HTML without having recourse to RStudio? I.e. is there a pure R function which will, given an R presentation source file, generate the same result as the IDE?
P.S. I suppose the underlying R package doing the conversion is revealjs by JJ Allaire, but on its own, it doesn't recognize some of the syntax extensions (e.g. those for customizing appearance by putting css: custom.css under the title of the first slide), which makes me think there must be an additional wrapper around it.
You can use the standard rmarkdown::render() function with the revealjs::revealjs_presentation format in the YAML header. Resources like custom.css are referenced relative to the Rmd location so there is no need to specify these within the render() step.
User notes on specific implementation diffs between native RStudio & scripted version
It's not a drop-in replacement though. The revealjs package, as it's available from CRAN, ships with a different (newer) version of the reveal.js library than the one used internally by RStudio (3.2 vs 2.4 as of March 8th, 2016). The default settings (e.g. transitions) are also different, so they need tweaking.
Circumspection is also warranted with the version of pandoc used as the workhorse for the Markdown → HTML conversion, as RStudio may internally be using an older one with different templates. All of this means that you may need to rework your customizations (e.g. css tweaks).
There are also syntax differences -- the metadata can't go under the first heading of the presentation (i.e. the title):
Presentation title
==================
author: Foo Bar
css: custom.css
Instead, they have to be put in a traditional RMarkdown YAML header:
---
author: Foo Bar
css: custom.css
---
I'm not sure whether per-slide special settings like incremental: true (which are put under the respective slide's heading in the RPresentation format) are recognized at all.
I use a script like this that I source to render my reveal.js markdown slides:
library(rmarkdown)
library(revealjs)
file.name <- "index"
path.to.file <- "."
rmarkdown::render(file.path(path.to.file, paste0(file.name, ".Rmd")),
revealjs_presentation(theme="white", highlight="tango", slideNumber = TRUE),
encoding = "UTF-8")
# Open the generated HTML file in the browser
browseURL( file.path(path.to.file, paste0(file.name, ".html")))