CharlotteS.
CharlotteS.

Reputation: 447

R markdown with interactive option of the Chunk

I try to have a Markdown document with a verbose option (i.e. show or not the code of the page, to avoid to afraid people :D ) depending on the choice of the user (basically a radio button widget).

I therefore executed the following code:

---
title: "Hello"
author: "Charlotte S."
date: "8 février 2018"
output: html_document
runtime: shiny
---


```{r}
 radioButtons("verbose", label = "",
    choices = c("Yes", "No"), 
    selected = "No")

verboseAction <- reactive({
  if(!is.null(input$verbose)){
    if(input$verbose == "Yes"){
      TRUE
    } else {
      FALSE
    }
  } else {
    FALSE
  }
})

output$print <- renderText({
  verboseAction()
})
```
Here `r textOutput("print")`

```{r echo = verboseAction()} 

2+2

```

the answer of R: "operation not allowed without a reactive context

Ok no problem, I did:

---
title: "Hello"
author: "Charlotte S."
date: "8 février 2018"
output: html_document
runtime: shiny
---


```{r}
 radioButtons("verbose", label = "",
    choices = c("Yes", "No"), 
    selected = "No")

verboseAction <- reactive({
  if(!is.null(input$verbose)){
    if(input$verbose == "Yes"){
      TRUE
    } else {
      FALSE
    }
  } else {
    FALSE
  }
})

output$print <- renderText({
  verboseAction()
})
```
Here `r textOutput("print")`

```{r echo = observe(verboseAction())} 

2+2

```

not working neither

---
title: "Hello"
author: "Charlotte S."
date: "8 février 2018"
output: html_document
runtime: shiny
---


```{r}
 radioButtons("verbose", label = "",
    choices = c("Yes", "No"), 
    selected = "No")

verboseAction <- reactive({
  if(!is.null(input$verbose)){
    if(input$verbose == "Yes"){
      TRUE
    } else {
      FALSE
    }
  } else {
    FALSE
  }
})

output$print <- renderText({
  verboseAction()
})
```
Here `r textOutput("print")`


```{r global_options, include=FALSE}

option <- reactive({
  if(!is.null(verboseAction())){

opts_chunk$set(fig.width=12, fig.height=8, fig.path='Figs/',
               echo=verboseAction(), warning=FALSE, message=FALSE)
  }
})
```

```{r} 

2+2

```

Nope nope nope

Do you have an idea how to do that? I could not find any example with a reactive values in the {r}, I am not even sure its possible ... (I also tried with a tcltk but it's worse because no reactivity)...

Thanks in advance and have a good day !

Cha

Upvotes: 2

Views: 1091

Answers (1)

Cristian E. Nuno
Cristian E. Nuno

Reputation: 2920

Overview

You want to declare the code_folding option in your Yet Another Markup Language (YAML) header.

You can set the default code folding to automatically hide (code_folding: hide) or show (code_folding: show) the code chunks from the user.

---
title: "Hello"
author: "Charlotte S."
date: "8 février 2018"
output: 
  html_document:
    code_folding: hide
runtime: shiny
---


```{r}
 radioButtons("verbose", label = "",
    choices = c("Yes", "No"), 
    selected = "No")

verboseAction <- reactive({
  if(!is.null(input$verbose)){
    if(input$verbose == "Yes"){
      TRUE
    } else {
      FALSE
    }
  } else {
    FALSE
  }
})

output$print <- renderText({
  verboseAction()
})
```
Here `r textOutput("print")`


```{r global_options, include=FALSE}

option <- reactive({
  if(!is.null(verboseAction())){

opts_chunk$set(fig.width=12, fig.height=8, fig.path='Figs/',
               echo=verboseAction(), warning=FALSE, message=FALSE)
  }
})
```

```{r} 

2+2

```

Upvotes: 3

Related Questions