diff --git a/DESCRIPTION b/DESCRIPTION index 74818701..681d8ff2 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -53,7 +53,10 @@ Imports: shinyWidgets, utils, xml2, - yaml + yaml, + sortsurvey +URL: https://pkg.surveydown.org +BugReports: https://github.com/surveydown-dev/surveydown/issues Suggests: glue, knitr, diff --git a/NAMESPACE b/NAMESPACE index 450e6a51..6671478a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -36,6 +36,8 @@ export(sd_stop_if) export(sd_store_value) export(sd_ui) export(sd_version) +importFrom(sortsurvey,bucket_list_survey) +importFrom(sortsurvey,rank_list_survey) importFrom(miniUI,gadgetTitleBar) importFrom(miniUI,miniContentPanel) importFrom(miniUI,miniPage) diff --git a/R/config.R b/R/config.R index 0c08bc7a..0f6b2787 100644 --- a/R/config.R +++ b/R/config.R @@ -1398,7 +1398,7 @@ extract_question_structure_html <- function(html_content) { ) # Extract options for the question ( mc, *_multiple, *_buttons, and select) - if (length(type) > 0 && grepl("radio|checkbox|select|matrix", type)) { + if (length(type) > 0 && grepl("radio|checkbox|select|matrix|list", type)) { options <- question_node |> rvest::html_nodes( "input[type='radio'], input[type='checkbox'], option" diff --git a/R/ui.R b/R/ui.R index ecbf2762..f9b01334 100644 --- a/R/ui.R +++ b/R/ui.R @@ -645,6 +645,10 @@ to_snake_case <- function(text) { #' - `"mc_multiple_buttons"`: Multiple choice with button-style options (multiple selections allowed) #' - `"text"`: Single-line text question #' - `"textarea"`: Multi-line text question +#' - "rank_list": Multiple choice in which the respondent can reorder the +#' choices using drag and drop based on the sortsurvey rank_list_survey question (see [sortsurvey::rank_list_survey()]) +#' - "bucket_list": Multiple choice options which the respondent can put into two +#' different buckets based on the sortsurvey bucket_list_survey question (see [sortsurvey::bucket_list_survey()]) #' - `"numeric"`: Numeric question #' - `"slider"`: Slider question #' - `"slider_numeric"`: Extended numeric slider question @@ -694,7 +698,7 @@ to_snake_case <- function(text) { #' sd_create_survey(template = "default") #' # This creates survey.qmd and app.R - launch the survey using app.R #' } -#' +#' @importFrom sortsurvey rank_list_survey bucket_list_survey #' @export sd_question <- function( id, @@ -995,6 +999,35 @@ sd_question <- function( Shiny.setInputValue('%s', selectedValues, {priority: 'event'}); }, 50); }); + ", id, js_interaction)))) + + } else if(type == "rank_list") { + + output <- rank_list_survey( + input_id = id, + text = label, + labels = list_name_md_to_html(option), + options= sortsurvey::sortable_options(direction = direction) + ) + + } else if (type=="bucket_list") { + + bucket_list_survey( + header = label, + group_name = id, + add_rank_list( + text = "Drag from here", + labels = list_name_md_to_html(option) + ), + add_rank_list( + text = "to here", + labels = NULL + ),output_width="100%", + output_height="100%", + options= sortsurvey::sortable_options(direction = direction), + # NOTE: direction doesn't seem to work in bucket list questions + orientation=direction) + ", id, js_interaction, diff --git a/man/sd_question.Rd b/man/sd_question.Rd index 8da0157b..86a105c2 100644 --- a/man/sd_question.Rd +++ b/man/sd_question.Rd @@ -134,6 +134,10 @@ The function supports various question types: \item \code{"mc_multiple"}: Multiple choice (multiple selections allowed) \item \code{"mc_buttons"}: Multiple choice with button-style options (single selection) \item \code{"mc_multiple_buttons"}: Multiple choice with button-style options (multiple selections allowed) +\item "rank_list": Multiple choice in which the respondent can reorder the +choices using drag and drop based on the sortsurvey rank_list_survey question (see \code{\link[sortsurvey:rank_list_survey]{sortsurvey::rank_list_survey()}}) +\item "bucket_list": Multiple choice options which the respondent can put into two +different buckets based on the sortsurvey bucket_list_survey question (see \code{\link[sortsurvey:bucket_list_survey]{sortsurvey::bucket_list_survey()}}) \item \code{"text"}: Single-line text question \item \code{"textarea"}: Multi-line text question \item \code{"numeric"}: Numeric question @@ -183,5 +187,4 @@ if (interactive()) { sd_create_survey(template = "default") # This creates survey.qmd and app.R - launch the survey using app.R } - } diff --git a/surveydown.Rproj b/surveydown.Rproj index fd8dd284..28ddd7ba 100644 --- a/surveydown.Rproj +++ b/surveydown.Rproj @@ -1,4 +1,5 @@ Version: 1.0 +ProjectId: 3cd6a428-6761-4ab1-aa17-b65c2b0d5267 RestoreWorkspace: No SaveWorkspace: No diff --git a/test_rank_question/app.R b/test_rank_question/app.R new file mode 100644 index 00000000..bd515e9a --- /dev/null +++ b/test_rank_question/app.R @@ -0,0 +1,41 @@ +# remotes::install_github("surveydown-dev/surveydown", force = TRUE) +library(surveydown) + +# Database setup + +# surveydown stores data on a database that you define at https://supabase.com/ +# To connect to a database, update the sd_database() function with details +# from your supabase database. For this demo, we set ignore = TRUE, which will +# ignore the settings and won't attempt to connect to the database. This is +# helpful for local testing if you don't want to record testing data in the +# database table. See the documentation for details: +# https://surveydown.org/store-data + +db <- sd_database( + host = "", + dbname = "", + port = "", + user = "", + table = "", + ignore = TRUE +) + + +# Server setup +server <- function(input, output, session) { + + # Define any conditional skip logic here (skip to page if a condition is true) + sd_skip_if() + + # Define any conditional display logic here (show a question if a condition is true) + sd_show_if() + + # Database designation and other settings + sd_server( + db = db + ) + +} + +# shinyApp() initiates your app - don't change it +shiny::shinyApp(ui = sd_ui(), server = server) diff --git a/test_rank_question/survey.Rproj b/test_rank_question/survey.Rproj new file mode 100644 index 00000000..69fafd4b --- /dev/null +++ b/test_rank_question/survey.Rproj @@ -0,0 +1,22 @@ +Version: 1.0 + +RestoreWorkspace: No +SaveWorkspace: No +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX + +AutoAppendNewline: Yes +StripTrailingWhitespace: Yes +LineEndingConversion: Posix + +BuildType: Package +PackageUseDevtools: Yes +PackageInstallArgs: --no-multiarch --with-keep.source +PackageRoxygenize: rd,collate,namespace diff --git a/test_rank_question/survey.qmd b/test_rank_question/survey.qmd new file mode 100644 index 00000000..e9aea8b7 --- /dev/null +++ b/test_rank_question/survey.qmd @@ -0,0 +1,112 @@ +--- +echo: false +warning: false +--- + +```{r} +library(surveydown) +library(sortsurvey) +``` + +::: {#welcome .sd-page} + +# Welcome to our survey! + +This is a simple demonstration of a surveydown survey. It has two pages with one question on each page. + +Here is a basic "multiple choice" question, created using `type = 'mc'` inside the `sd_question()` function: + +```{r} +sd_question( + type = 'mc', + id = 'penguins', + label = "Which type of penguin do you like the best?", + option = c( + 'Adélie' = 'adelie', + 'Chinstrap' = 'chinstrap', + 'Gentoo' = 'gentoo' + ) +) + +sd_question( + type = 'rank_list', + id = 'penguins_drag', + label = "Which type of penguin do you like the best? Rank them from best to worst.", + option = c( + 'Adélie' = 'adelie', + 'Chinstrap' = 'chinstrap', + 'Gentoo' = 'gentoo' + ) +) + + + +``` + +# Bucket list style: + +```{r} + +# add in drag and drop question +# horizontal method doesn't seem to work regardless of what width/height is +# set to + +# DOES NOT WORK: input id is only set for bucket top class, +# need to set two other input IDs for rank_list objects + +# see function bucket_list_survey in sortsurvey https://github.com/saudiwin/sortable_survey/blob/main/R/bucket_list.R +# and add_rank_list in sortable: https://rstudio.github.io/sortable/reference/add_rank_list.html + +sd_question( + type = 'bucket_list', + id = 'penguins_bucket', + label = "Which type of penguin do you like the best? Drag those penguins to the second bucket.", + option = c( + 'Adélie' = 'adelie', + 'Chinstrap' = 'chinstrap', + 'Gentoo' = 'gentoo' + ) + +) + + +``` + + +You need to insert next buttons with `sd_next()` and set the `next_page` argument to the name of the page you want to go to next. + +```{r} +sd_next(next_page = 'page2') +``` + +::: + +::: {#page2 .sd-page} + +This is another page in your survey. + +{surveydown} supports many types of questions. For example, here is a simple `text` type question: + +```{r} +sd_question( + type = "text", + id = "silly_word", + label = "Write a silly word here:" +) + +sd_next(next_page = 'end') +``` + +::: + +::: {#end .sd-page} + +## End + +This it the last page in the survey. + +```{r} +sd_close("Exit Survey") +``` + +:::