Moritz Primus
Moritz Primus

Reputation: 11

Dependent survey questions in R shinysurveys: matrix questions

I am currently trying to create a survey in R using the shinysurveys package. My goal is to create dependent matrix questions (e.g. Likert scale). As long as I don't use matrix questions everything is fine. However, by using the code below, R returns an error message and the second, dependent question doesn't appear.

To clarify, I want the second question to show up, only if the first question is answered with "1".

Would be great, if someone could help me out :)

df_1 <- data.frame(question = rep("A", 7),
                   option = rep(c("1", "2", "3", "4", "5", "6", "I don't know"), 1),
                   input_type = rep("matrix", 7),
                   input_id = rep("ID1", 7),
                   dependence = NA,
                   dependence_value = NA,
                   required = TRUE
)


df_2 <- data.frame(question = c(rep("B", 7)),
                   option = rep(c("1", "2", "3", "4", "5", "6", "I don't know"), 1),
                   input_type = rep("matrix", 7),
                   input_id = rep("ID2", 7),
                   dependence = rep("ID1", 7),
                   dependence_value = rep("1", 7),
                   required = TRUE
)

df_merged <- rbind(df_1, df_2)

#create user interface
ui <- fluidPage(
  surveyOutput(df = df_merged,
               survey_title = "Title",
               survey_description = "Description"))


#specify server function
server <- function(input, output, session) {
  renderSurvey()

  observeEvent(input$submit, {
    showModal(modalDialog(
      title = "Survey End!"
    ))
  })
}


shinyApp(ui, server)

Upvotes: 1

Views: 183

Answers (1)

user28876813
user28876813

Reputation: 1

You could try and register an 'extendInpuTtype'. This will help achieve your goal. However, there is some limitations with using the shiny widgets extension input for instance when displaying the choices in the ui. see code example below. You can play around with the various shiny inputs to see what will be aesthetically pleasing and consistent with your ui desires.

library(shiny)
library(shinysurveys)

df_1 <- data.frame(question = "what is your favorite food",
                   option = NA,
                   input_type = "slider",
                   input_id = rep("ID1", 7),
                   dependence = NA,
                   dependence_value = NA,
                   required = TRUE
)


df_2 <- data.frame(question = "Why is this your favorite food?",
                   option = NA,
                   input_type = "textSlider",
                   input_id = rep("ID2", 7),
                   dependence = rep("ID1", 7),
                   dependence_value = rep("1", 7),
                   required = TRUE
)

  extendInputType(input_type = "slider", {
  shiny::sliderInput(
    inputId = surveyID(),
    label = surveyLabel(),
    min = 0,
    max = 5,
    value = 0
  ) 
})


  extendInputType("textSlider", {
    shinyWidgets::sliderTextInput(
      inputId = surveyID(),
      label = surveyLabel(),
      force_edges = TRUE,
      choices = c("I Love it",  "It's all I can afford")
    )
  })

df_merged <- rbind(df_1, df_2)

#create user interface
ui <- fluidPage(
  surveyOutput(df = df_merged,
               survey_title = "Dependant Qustionaire",
               survey_description = "This is a two part survey that only shows the next question when the correct answer is provided to the first"))



#specify server function
server <- function(input, output, session) {
  renderSurvey()
  
  observeEvent(input$submit, {
    showModal(modalDialog(
      title = "Survey End!"
    ))
  })
}


shinyApp(ui, server) 

Upvotes: 0

Related Questions