Two versions of R Markdown slides

I have sets of slides written in R Markdown, specifically using beamer. I would like to produce two versions of these slides, one with everything and one in which certain material is blanked out. Think of this as a quiz question and an answer. I'll use one version with the answer blanked out and another where I show both the question and the answer.

What makes this hard is that I want spacing to be maintained where material is blanked out. So I can't just omit certain material because that would re-arrange where material appears. (If it matters, I need to retain the output as beamer format pdfs.)

I haven't the foggiest how to do this. Any clever ideas?

I don't have a sure solution. First, just a note that I asked a similar question some time ago (but doesn't answer your case).

Then, is the material to display text or image? I think you could get a switch mechanism as described here, the idea is that you'd need some kind of placeholder. For text you could try a text the same length but in white (as implemented in the CTAN package hideanswer, or if you're using a monospace font, a series of spaces the same length so that people can't copy-paste the hidden text), and for images you could create placeholder images of the same size.

For example some code that halfway works (but would take some effort to make it fully work well):

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)

soln <- TRUE

fill_with_spaces <- function(char){
  rep(" ", nchar(char)) |>
    paste0(collapse = "")

make_blank <- function(expr){
  res <- capture.output(eval(expr))
  res <- sapply(res, fill_with_spaces)
  names(res) <- sapply(names(res), fill_with_spaces)
conditional_display <- function(expr, soln){
  expr <- substitute(expr)
  } else{

## Slide with R Output

```{r cars, echo = TRUE}
conditional_display(summary(cars), soln)

cat("some text")
1 Like

That's a lot more progress than I made...

I want to be able to take an arbitrary part of an RMarkdown file and make it "hidden" or not, while leaving the proper spacing. Basically, I want to be able to give students a copy of slides that has some material blanked out that they can bring to class. Then I show the whole slide "unblanked out" and they can take notes on their version as they think best.

I don't know of any built-in solution, but maybe using advanced feature like Lua filters for Pandoc you can detect the part to hide, and replace them with blank content.

Do you have an example document to share ?

Also did you try that already ?

@cderv, thanks for the follow-up. hideanswer basically turns text white, rather than suppressing it. If you're printing, that's fine. If you're making pdfs available it's an improvement, but still lets an enterprising reader copy the "blank" text and paste it elsewhere in a visible font.

You can accomplish the same thing by adding


to header_includes: in the YAML and then using \transparent{0} to make text "invisible" and transparent{1} to restore visibility.

Not a perfect solution, but good enough for some purposes and pretty easy.

1 Like

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.