Search code examples
r-markdownpassword-protectionbookdown

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.


Solution

  • 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.