user15841231
user15841231

Reputation:

Execute observeEvent depending on radiobuttons input

I just started learning R to program an experiment for my thesis, so sorry in advance for asking what are probably super basic questions.

I want to build a survey (in German) consisting of multiple pages. Before the respondents can start they have to give their consent via a radio button ("Einwilligung"). I want them to only be able to advance to the next page if they consent ("Ich stimme zu").

So intuitively I would think of something like

if (input$Einwilligung == "Ich stimme zu") but I can´t figure out how to include it.

Pls find the code I came up with so far below. Thanks for your help.

library(shiny)
ui <- (htmlOutput("page"))

###intro
intro <- function(...) {
  div(class = 'container', id = "intro",
      div(class = 'col-sm-2'),
      div(class = 'col-sm-8',
          h1("Startseite"),
          p("Platzhalter"),
          br(),
          actionButton("Weiter1", "Weiter")
      ))
  
}

###declaration of consent
decl <- function(...) {
  div(class = 'container', id = "decl",
      div(class = 'col-sm-2'),
      div(class = 'col-sm-8',
          h1("Einwilligung zur Teilnahme"),
          p("Platzhalter"),
          br(),
  radioButtons("Einwilligung",label = NULL, choices = c("Ich stimme zu","Ich stimme nicht zu")),
  actionButton("Weiter2", "Weiter")
      ))
  
}

###explanation HSV
expl1 <- function(...) {
  div(class = 'container', id = "expl1",
      div(class = 'col-sm-2'),
      div(class = 'col-sm-8',
          h1("Einleitung Teil 1"),
          p("Platzhalter"),
          br(),
          actionButton("Weiter3", "Weiter")
      ))
  
}

###outro
outro <- function(...) {
  div(class = 'container', id = "outro",
      div(class = 'col-sm-2'),
      div(class = 'col-sm-8',
          h1("Abschluss"),
          p("Platzhalter"),
          br(),
          textInput("Email","Email"),
          actionButton("Senden", "Senden")
      ))

}

render_page <- function(...,f, title = "Test") {
  page <- f(...)
  renderUI({
    fluidPage(page, title = title)
  })
}

server <- function(input, output){
  
  output$page <- render_page(f = intro)
  
  observeEvent(input$Weiter1, {
    output$page <- render_page(f = decl)
  })
  
  observeEvent(input$Weiter2, {
    output$page <- render_page(f = expl1)
  })
  
  observeEvent(input$Weiter3, {
    output$page <- render_page(f = outro)
  })

}

Upvotes: 2

Views: 258

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389047

You can add a check condition in observeEvent of Weiter2 :

observeEvent(input$Weiter2, {
    if (input$Einwilligung == "Ich stimme zu") output$page <- render_page(f = expl1)
  })

Maybe add an else condition to do something else.

Upvotes: 1

Related Questions