Incorporating shiny with learnr modules

I am interested in incorporating shiny elements in a learnr module, for example, I'd like to be able to have:

radioButton("input1", "This is an input", choices = c("Yes", "No"))

In a chunk above some learnr material - is this possible?

It should be -- I think you want radioButtons("input1", "This is an input", choices = c( "Yes", "No")), though?

Keep in mind that learnr::tutorial is a beefed up version of a rmarkdown::html_document (so you should be able to embed whatever shiny input/output/HTML you wish), and in many cases, it should be used with runtime: shiny_prerendered, for example:

---
output:
  learnr::tutorial:
    progressive: true
    allow_skip: true
runtime: shiny_prerendered
---

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.

You might want to read the execution section of this article on prerendered docs -- https://rmarkdown.rstudio.com/authoring_shiny_prerendered.html#Execution_Contexts

The default context is 'render', meaning that the code is run on "knit" time, which is before the shiny session initializes, so reactive expression(s) won't work in that context.

If you change the context to 'server' then the code is executed within a shiny session context, so this does what you want:

```{r, context='server'}
observe({
  d <- data.frame(test = input$input1)
  write.csv(d, "test.csv")
})
```

Lol, oops I had a typo in my post (fixed it!) - that part is working for me, but when I try to access input1 via input$input1 it says object input not found? This is Very Likely because I don't fully understand how shiny objects work in a R Markdown document. The task I am trying to do is take the input from a shiny input and save it to a data frame (and send that data frame to somewhere like DropBox).

In shiny I'd do this:

library(shiny)

ui <- fluidPage(
    titlePanel("Example"),

    sidebarLayout(
        sidebarPanel(
            radioButtons("input1", "This is an input", choices = c( "Yes", "No"))
        )
    )
)

server <- function(input, output) {
    observe({
        d <- data.frame(test = input$input1)
        write.csv(d, "test.csv")
    })
}

shinyApp(ui = ui, server = server)

Right now I have this (which doesn't work :frowning:)

```
---
output:
  learnr::tutorial:
    progressive: true
    allow_skip: true
runtime: shiny_prerendered
---

```{r}
library(shiny)
```

```{r}
radioButtons("input1", "This is an input", choices = c( "Yes", "No"))
```

```{r}
observe({
  d <- data.frame(test = input$input1)
  write.csv(d, "test.csv")
})
```

oh my goodness this is exactly what I need, thank you @cpsievert!