jdtrat / shinysurveys

Develop and deploy surveys in Shiny/R.
https://shinysurveys.jdtrat.com/
Other
97 stars 26 forks source link

Support Shiny Modules? #39

Open richardsprague opened 2 years ago

richardsprague commented 2 years ago

I'm unable to get shinysurveys::getSurveyData() to work properly when included in a shiny module. I'm not sure if this is an error on my part, or if it's a limitation on the current version.

Maybe this is because getSurveyData() doesn't allow the session id to be passed through? I'm not sure what's going on, but I see that surveyOutput() appears hard-coded to inputId = "submit", which would seem to preclude passing it as an ns() value as would be required in a shiny module.

Am I missing something? Is there a workaround? Before I fork and fix it myself I'm wondering if maybe shinysurveys already works under shiny modules and I just need to do something different.

jdtrat commented 2 years ago

Hi! That is a limitation of the current version, unfortunately. I'm planning to refactor much of the code to support best coding practices (e.g. shiny modules), but I don't know when that'll be public. If you fork and fix, and are interested in submitting a PR, I would love to incorporate your work!

In addition to the submit button being hard coded, you would also need to make sure that the data frame of questions supplied has the same namespace prefix. As the code is now, you could do that with something like:

modularize_survey_questions <- function(id, questions) {
  ns <- shiny::NS(id)
  questions$input_id <- ns(questions$input_id)
  return(questions)
}

modularize_survey_questions("mod", shinysurveys::teaching_r_questions)
#> # A tibble: 54 × 7
#>    question    option   input_type input_id dependence dependence_value required
#>    <chr>       <chr>    <chr>      <chr>    <chr>      <chr>            <lgl>   
#>  1 What's you… 25       numeric    mod-age  <NA>       <NA>             TRUE    
#>  2 Which best… Female   mc         mod-gen… <NA>       <NA>             TRUE    
#>  3 Which best… Male     mc         mod-gen… <NA>       <NA>             TRUE    
#>  4 Which best… Prefer … mc         mod-gen… <NA>       <NA>             TRUE    
#>  5 Which best… Prefer … mc         mod-gen… <NA>       <NA>             TRUE    
#>  6 Which best… <NA>     text       mod-sel… gender     Prefer to self … FALSE   
#>  7 What is th… Did not… select     mod-edu… <NA>       <NA>             FALSE   
#>  8 What is th… Some hi… select     mod-edu… <NA>       <NA>             FALSE   
#>  9 What is th… High sc… select     mod-edu… <NA>       <NA>             FALSE   
#> 10 What is th… Some co… select     mod-edu… <NA>       <NA>             FALSE   
#> # … with 44 more rows