Make R Markdown output accessible - r

I'd like to output an accessible version of a presentation I'm making with R Markdown ioslides. At this point, I've been struggling to find much documentation on alt-text for dynamically generated images - not pointers to images - there's lots of information available online on that front.
Seems like the easiest thing to do might be to output my document as a word_document instead of a presentation, and ensure that figures have captions, using fig.cap argument in the chunk header.
However, I'd like the captions to be available only as alt-text - so that this document doesn't look strange to folks who aren't using screen reader tech (I'd like to make my alt-text nice and descriptive, but for folks who can see ok, this will seem odd, because the graph will do the talking instead).
I've included a reprex below. How can I still include the fig.cap as alt-text in the output word document, but not have it show up underneath the graph?
Also, if you are aware of any good resources on making RMarkdown docs accessible, I am all ears!!
Thanks for your help.
---
title: 'test doc'
author: "Nova"
date: "April 2020"
output:
word_document:
fig_height: 5
fig_width: 10
height: 500
widescreen: yes
width: 500
---
```{r setup and comments, include=FALSE}
knitr::opts_chunk$set(echo = FALSE,
message = F,
warning = F,
fig.height = 5,
fig.width = 10,
fig.cap = NA)
library(ggplot2)
```
## cars
```{r ggplot, include=T, fig.cap = "this is some alternative text about this figure"}
ggplot(cars)
```

I'd go back to some kind of HTML output, because it's simpler.
If you want ioslides output, you can use fig.cap="Some alt text" in the chunk option to set the alt text for the figure. Unfortunately, this also generates a visible caption, but you can suppress that. For one slide, just insert inline CSS:
<style>
.caption {
display:none;
}
</style>
somewhere on the slide before the figure. To do it for the whole presentation, put that text in a .css file and include it with YAML like
output:
ioslides_presentation:
css: style.css
It's probably also possible to change the template for this output style, but that looks like a lot more work.

Related

Select Slide Layout in R Markdown PowerPoint Export

I create a PowerPoint with R markdown.
---
title: "Presentation Title"
author: "That is me"
date: "`r format(Sys.time(), '%d.%m.%Y')`"
output:
powerpoint_presentation:
reference_doc: template.pptx
slide_level: 2
---
As you see, I use a custom template file template.pptx.
Now my goal ist to select which layout to be used from that template. I know that officedown allows for a chunk option layout (see https://www.apreshill.com/blog/2021-07-officedown/), but I think it is possible without the additional package as well. Why?
When I put a table on the page via kable, the layout "Content with Caption" from the template is used.
```{r results="asis"}
cat("\n\n## Page Title\n\n")
cat("\n\nSome content\n\n")
data.frame(matrix(c(1))) %>% kable
```
When I use flextable instead, the layout "Title and Content" is used.
```{r results="asis", ft.left=0.9}
cat("\n\n## Page Title\n\n")
cat("\n\nSome content\n\n")
flextable(data.frame(matrix(c(1)))) %>% flextable_to_rmd
```
So it seems that kable() can somehow tell the output to use the other layout. Any ideas why that is the case?
The Pandoc manual says
Content with Caption
This layout is used for any non-two-column slides which contain text followed by non-text (e.g. an image or a table).
So that probably means that the flextable is not recognizes as "non-text" content. Can I add non-text content that won't show up in the presentation? It's not that I wish back times of the clearpixel.gif, but ...
I am not especially proud of this, but the good old clearpixel does the job...
```{r results="asis", ft.left=0.9}
cat("\n\n## Page Title\n\n")
cat("\n\nSome content\n\n")
flextable(data.frame(matrix(c(1)))) %>% flextable_to_rmd
cat("![](clearpixel.png)\n")
```
Whoever finds a cleaner solution (the invisible elemente will still be selectable in the PPT file), please kindly post it here :)

Text wrapping in code sections using Tufte format

Text in the code section of a Tufte formatted, Rmarkdown document does not seem to wrap. I have tried several options and suggestions, but nothing works. I am guessing that all of the other suggestions I could find on StackOverflow did not work, because of the "tufte" format. I am using knitr to construct HTML or PDF documents. Here is the minimal example:
---
title: "Width Minimal Example"
output:
tufte::tufte_handout: default
tufte::tufte_html: default
---
```{r setup, include=FALSE}
library(tufte)
library(formatR)
# invalidate cache when the tufte version changes
knitr::opts_chunk$set(tidy = TRUE, cache.extra = packageVersion('tufte'))
options(htmltools.dir.version = FALSE)
```
```{r, tidy='styler', tidy.opts=list(width.cutoff=50)}
filename <- "/Test/test/test/test/test/test/test/test/test/test/test/test.data"
```
When I use knitr to produce HTML or PDF (handout), the "filename" variable runs out of the textbox allotted for code. I've looked through the "tufte.css" file and nothing there seems to describe the width of text in code boxes.
Ideally, the text in the code box would not only wrap, but indent a bit so that the person reading the code would easily be able to see which lines were new lines and which lines were wrapped lines. Thanks!

Rmarkdown tab name in browser

I'm struggling with what to Google to start solving this one. It might be a Windows question and not an R question but I could do with some help.
I am using rmarkdown::render to generate html reports. I have a master.Rmd which calls some child_docs. I use the argument output_file = to name the html document. This works fine and I can successfully generate documents called my_report1.html. When I open the html document in my browser (both Chrome and FireFox) the browser tab is labelled as master.utf8.md:
In the past the tab label used to be my_report1.html. I want to fix this because I regularly have multiple reports open and navigating between the tabs to find which report I want is now painful.
Any thoughts on what to check?
The YAML:
---
output:
html_document:
toc: true
toc_float: true
params:
lot: 1
editor_options:
chunk_output_type: console
---
Chunk setup:
```{r setup, include=FALSE}
## GLobal chunk options
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
```
Update: I think this is to do with the YAML title:. I'm going to open a new question with a better example.
I managed to cobble together a work around for this. The tab name in a browser comes from the title declared in the YAML even if you supply a file name to output_file in rmarkdown::render. I wanted to set my title dynamically and this can be done with using the methods described here, example:
---
output: html_document
---
```{r}
title_var <- "Sample Title"
```
---
title: `r title_var`
---
However, I needed an extra work around to suppress the title because I didn't actually want it to make a title. This can be done by using the following css (top of doc or separate file):
<style>
.title{
display: none;
}
</style>

R: how to change font and alignment of text in knitr word

```{r results = 'asis'}
for(i in 1:10){
cat(paste("This is iteration number" i))
}
```
After I output the resulting .docx file, the text is left-aligned in size 12 Cambria font by default. How can I change the alignment to center, change the font size, and font type in my .Rmd file?
One way to do this is to use the reference_docx tag in the YAML header like this:
---
title: "Untitled"
author: "Me X. Person"
date: "April 3, 2018"
output:
word_document:
reference_docx: word-style-02.docx
fontsize: 10pt
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r results = 'asis'}
for(i in 1:10){
cat(paste("This is iteration number", i, "<P><br>"))
}
```
Next, prepare a file in Word where all you do is control the styles -- I knit to Word and found out that in this example, the code is controlled by the style titled Compact. So I clicked on that square in the ribbon in the MS Word header, and set it to 16 point Courier New (centered). The Word file that I used is here so you can download and open/try it: Drive link
The output it generates looks like this in Word:
If this is way off of what you were looking for, just let me know and I will delete it. Hopefully it is at least little bit useful.
The above solution didn't really work for me but I found a tweak to fix.
In Microsoft Word, you want to make sure you modify a section of text in the .docx reference document for each of the styles:
First Paragraph
Body Text
Compact
Changing font size from the home tab won't do anything. You have to right click on the selected style ribbon -> Modify and change the font size in the popup window for that specific style.
Here's a download link that makes the style more similar to SAS outputs when using the cat() function to print text in your .Rmd to word output:
https://drive.google.com/file/d/12VZUPXaAGMKPUAJsohZRYQrToFr6awzY/view?usp=sharing
note that I call the file word-style.docx not word-style-02.docx

Changing the font size of figure captions in RMarkdown Word output

I recently asked
Changing the font size of figure captions in RMarkdown HTML output
and I got a very nice answer which uses this CSS method. I wanted to try the same, but this time with Word output. If you don't want to read my former question, I summarize the issue here: I'd like to make the font size of all figure captions in my R Markdown document smaller. The final output is Word,this time, and I'm working in R Studio. To load the picture, I use the include_graphics function from knitr, because I've been told it's the best way (see here). My .Rmd file is:
---
title: "ppp"
author: "ppp"
date: "`r Sys.Date()`"
output:
word_document:
fig_caption: yes
html_document:
fig_caption: yes
---
<style>
p.caption {
font-size: 0.8em;
}
</style>
```{r setup, include=FALSE}
library(knitr)
opts_chunk$set(echo = FALSE)
```
```{r foo, fig.cap="$f_{p}$ as a function of $g$ for various values of $r=\\frac{\\rho_{w}}{\\rho_{a}}$"}
# All defaults
include_graphics("download.jpg")
```
This is regular text.
The corresponding output is:
Clearly, this CSS method doesn't work (I guess it's something related to HTML, so it doesn't render in Word). In Word I can manually change the font size for each caption, but I'd rather set some global R Markdown parameter. Is that possible?
Almost as easy as in the HTML case. The following applies to the workflow using LibreOffice. But in Word it should be almost the same:
Produce your docx output file.
Open it in LibreOffice (or Word, or Pages, ...)
In LibreOffice, right-click the caption and choose Edit Style (in Word you can open the styles pane with Ctrl+Shift+Alt+S)
In the menu that popped up you can modify the style for Image Captions
When you are done editing the style, click Apply and just save the file as a docx called template.docx
Finally, add a style reference in the YAML header of your Rmd document like
title: "ppp"
author: "ppp"
date: "July 4, 2017"
output:
word_document:
reference_docx: template.docx
fig_caption: yes
And the captions should be smaller now according to how you changed the style in your reference document.

Resources