Hide part of the code while displaying full results

I'm writing a teaching document, in which I would like to hide some function and variable names, so the students have to rewrite themselves, but I still want to display the correct result. For example, this would be one code chunk:

# print 1+1
x <- 1 + ___
#> 2

One solution that works is to write two code chunks:

```{r eval=FALSE}
# print 1+1
x <- 1 + ___

```{r echo=FALSE}
# print 1+1
x <- 1 + 1

That way, the students see the content of first code chunk, and the results of the second one. This works, but is a bit unpractical with tens of code chunks.

I feel this should be a common thing to do, and there are probably better solutions available, but couldn't really find anything with the keywords I tried.

The easiest option is probably to take advantage of built-in features for collapsing code chunk. See the rmarkdown manual for help.

Another option is the functionality for a software carpentry lesson (see the 'episodes' directory for examples) but that is more complicated to implement.

My understanding (and experience) with collapsing is that the student then gets control and can choose to display nothing or everything. If I'm not mistaken, that is also what software carpentry uses (in their "Challenges").

Here I would like to keep control of what is displayed, and censor a few words, without giving the students the possibility to show them.

Your description of these resources is accurate. What you are asking for sounds like a lot of complicated programming that is not currently enabled in rmarkdown, but I could be wrong.

this might be closer to what you want: https://noamross.github.io/gams-in-r-course/chapter1

I see. I just felt that it would be something many instructors would have wanted to do, so there could be an easy solution out there.

Yes! That's what I have in mind.

1 Like

This topic was automatically closed 7 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.