RMarkdown collapsible panel

后端 未结 3 1235
梦谈多话
梦谈多话 2021-02-03 12:52

As I am preparing tutorials for students, I need a way to hide content in collapsible panels which can be revealed by clicking on a button. I have got this to work using the cod

3条回答
  •  南旧
    南旧 (楼主)
    2021-02-03 13:22

    You can use multiple tabs (add {.tabset} after the header). It's very simple to generate them using r-markdown and they look almost the same as collapsible panel (of course you need to have more than one option).
    Not to paste same code multiple times specify code argument in chunk options (code = readLines("code.R")). Or you can have only one panel for code and answer so you wouldn't need external document.

    ---
    title: Collapsible Panel
    output:
      html_document:
        theme: flatly
        highlight: tango
    ---
    
    # Question 1 {.tabset .tabset-fade .tabset-pills}
    
    ## Question
    
    How does uniform distribution look like?
    
    ## Code 
    
    ```{r, echo = TRUE, eval = FALSE, code = readLines("Q1.R")}
    ```
    
    ## Answer
    
    ```{r, echo = FALSE, eval = TRUE, code = readLines("Q1.R")}
    ```
    

    Code file (Q1.R):

    hist(1:10)
    


    To not have any content and then show answer you can make first tab completely empty with:

    # Question 1 {.tabset}
    
    ##  
    
    ## Answer
    
    ```{r, echo = FALSE, eval = TRUE, code = readLines("Q1.R")}
    ```
    

提交回复
热议问题