Skip to contents

Create R6-based server module to generate and hold the state of your questionnaire

It is recommended to use handler functions to use the Quetzio class most efficiently, though if you have experience using R6 - using it that way is also valid.

See also

Quetzio_UI

Public fields

source_list

List containing the data for all the inputs

description

List containing survey instruction and description

div_id

ID of the div containing the survey UI

module_id

ID of the shiny module

is_done

logical reactiveVal indicating if the survey has been completed

message

reactiveVal catching any messages from object

answers

reactiveVal object containing list with answers to questions

order

Indices of questions in order of their appearance, if you wished to randomize their order. Otherwise NULL

module_ui_id

character string used to generate UI. It needs to be modified when linking the questionnaires

Methods


Method toggle_ui()

method to change the state of the UI

Usage

Quetzio$toggle_ui(x)

Arguments

x

logical indicating what should be the state of the UI. If TRUE, then the UI will be rendered.


Method new()

Initializing the 'Quetzio' object

Usage

Quetzio$new(
  source_method,
  source_yaml = NULL,
  source_yaml_default = NULL,
  source_gsheet_id = NULL,
  source_gsheet_sheetname = NULL,
  source_object = NULL,
  source_object_default = NULL,
  desc_yaml = NULL,
  desc_gsheet_id = NULL,
  desc_gsheet_sheetname = NULL,
  desc_object = NULL,
  randomize_order = FALSE,
  output_gsheet = FALSE,
  output_gsheet_id = NULL,
  output_gsheet_sheetname = NULL,
  questionee_id = NULL,
  module_id = NULL,
  div_id = NULL,
  custom_css = NULL,
  lang = "en",
  custom_txts = NULL,
  use_modal = TRUE,
  render_ui = TRUE,
  link_id = NULL
)

Arguments

source_method

character string specifying in what form the source config file will be provided. Can be either 'gsheet', 'yaml' or 'raw'. Necessity of other arguments is dependent on this choice. For more info see 'details'

source_yaml

path to the source yaml file

source_yaml_default

path to the optional default options for items generated with source list. Only when source_method == 'yaml' or source_method == 'raw' and source object of class list is povided..

source_gsheet_id

id of the source googlesheet file

source_gsheet_sheetname

name of the source spreadsheet

source_object

object of class list (similiar in structure to 'yaml' source) or data.frame (similiar in structure to 'googlesheet' source) to be the source of questions. You can create a sample data.frame with create_survey_source(). Needed when source_method == 'raw'

source_object_default

list containing optional default options for items generated with source list. Only when source_method == 'yaml' or source_method == 'raw' and source object of class list is povided.

desc_yaml

path to the optional instruction and item descriptions.

desc_gsheet_id

id of the googlesheet to provide optional instruction and item descriptions. Defaults to 'source_gsheet_id', if not provided.

desc_gsheet_sheetname

name of source for optional instruction and item descriptions.

desc_object

object of class list or data.frame to be the source of optional instruction and item descriptions.

randomize_order

logical: do you wish to randomize order in which the items will appear? Defaults to FALSE

output_gsheet

logical: do you wish to save the answers automatically to the googlesheet. If TRUE, the 'output_gsheet_id' and 'output_gsheet_sheetname' arguments need to be specified. Defaults to FALSE

output_gsheet_id

id of the output googlesheet file. If not specified, the same googlesheet as for 'source' will be used

output_gsheet_sheetname

name of the output spreadsheet

questionee_id

reactive object containing ID of the questionee to append to the answers list during its retrieval with Quetzio_get_df or sending to googlesheets. Optional.

module_id

character string with unique id for the module. If not specified, it will be automatically generated

div_id

character string with unique id for the created div. If not specified, it will be set to 'form'

custom_css

custom css for classes 'mandatory star' and 'invalid_input'. If not specified, default look will be used:

  • invalid_input = "outline: red; outline-style: dashed; outline-offset: 10px;"

  • mandatory_star = "color: red;"

  • quetzio_submit = "color: #fff; background-color: #337ab7; border-color: #2e6da4; width: 200px;"

  • quetzio_description = "font-size: 0.9em;"

You can also add styles for different classes contained within the div of the questionnaire - styles will be affecting only elements within this specific questionnaire.

lang

language to use. For now only 'en' and 'pl' are supported.

custom_txts

named list with custom labels for specified language. For more information look upon documentation for 'quetzio_txt'

use_modal

logical indicating if modalDialog for invalid inputs should be triggered. Defaults to TRUE

render_ui

logical indicating if the UI for questionnaire should be rendered

link_id

character specifying the 'link_id' of the 'quetzio_link_server' object, modifying its namespace. Only used internally, if the questionnaire is part of linked server. Don't set it manually!

Details

Currently, there are multiple methods both for source, which will generate the inputs, and for output. Mandatory arguments change depending of your choices:

  • for source:

    • source_method == 'yaml': 'source_yaml'

    • source_method == 'gsheet': 'source_gsheet_id,' 'source_gsheet_sheetname'

    • source_method == 'raw': 'source_object'

  • for output:

    • output_gsheet == TRUE: 'output_gsheet_id' (if other than 'source_gsheet_id') and 'output_gsheet_sheetname'

There are also some optional functionalities, that can be used with sources.

  • optional instructions and item descriptions - they are generated only if one of the following is provided:

    • desc_yaml: rendering from YAML file

    • desc_gsheet_sheetname: rendering from googlesheet. If the source_method isn't gsheet or the 'googlesheet_id' containing description is different from source, the desc_gsheet_id need to be provided too

    • desc_object: rendering from R object of classes 'data.frame' or 'list'

  • optional default configuration - it is used per shinyInput type. Need to provide either source_yaml_default or source_object_default.

Returns

the 'Quetzio' serverModule

Examples

## Only run example in interactive R sessions

if (interactive()) {

 # load libraries
 library(shiny)
 library(shiny.quetzio)

 # create ui
 ui <- fluidPage(
   column(6, align = "center",
          # bind the UI with correct module_id
          Quetzio_UI("my_quetzio")
   ),
   column(6,
          h2("State of", tags$i("Quetzio")),
          h3("Is it done?"),
          verbatimTextOutput("quetzio_is_done"),
          h3("Error messages?"),
          verbatimTextOutput("quetzio_message"),
          h3("Answers"),
          verbatimTextOutput("quetzio_answers")
   )
 )

 server <- function(input, output, session) {

   # initialize new quetzio
   questionnaire <- Quetzio$new(
     # load questions from R object
     source_method = "raw",
     source_object = quetzio_examples$questions_lists$simple_quetzio,
     # optionally add descriptions
     desc_object = quetzio_examples$description_lists$simple_quetzio,
     # use the same module_id as in UI binding
     module_id = "my_quetzio",
     # custom_css to give margin but not center options explicitly
     # it will affect only elements within the form div
     custom_css = list(
       "shiny-options-group" = "text-align: left; margin-left: 45%"
     ),
     # you can also optionally give div unique id - useful for external styling
     div_id = "my_questio_div_id"
   )

   # render objects to show your questionnaire status
 output$quetzio_is_done <-
     renderPrint(questionnaire$is_done())
   output$quetzio_message <-
     renderPrint(questionnaire$message())
   output$quetzio_answers <-
     renderPrint(questionnaire$answers())
 }

 shinyApp(ui, server)

}


Method get_answers_df()

method to get preprocessed answers in the form of dataframe (only if all of the questionnaires are done)

Usage

Quetzio$get_answers_df()

Returns

data.frame


Method update_labels()

Method to update labels on the change in reactive

Usage

Quetzio$update_labels(
  trigger,
  source_method,
  source_yaml = NULL,
  source_gsheet_id = NULL,
  source_gsheet_sheetname = NULL,
  source_object = NULL
)

Arguments

trigger

reactive which will trigger the update. It needs to take values linked to the changes in the source

source_method

character string specifying in what form the source config file will be provided. Can be either 'gsheet', 'yaml' or 'raw'. Necessity of other arguments is dependent on this choice

source_yaml

path to the source yaml file

source_gsheet_id

id of the source googlesheet file

source_gsheet_sheetname

name of the source spreadsheet

source_object

object of class list (similiar in structure to 'yaml' source) or data.frame (similiar in structure to 'googlesheet' source) to be the source of questions. You can create a sample data.frame with create_survey_source(). Needed when source_method == 'raw'

Examples

## only run examples in interactive environment

if (interactive()) {

 library(shiny)
 library(shiny.quetzio)

 ui <- fluidPage(
   # some input to trigger label update
   selectizeInput("gender", "What is your gender?",
                  choices = c("Male" = "M",
                              "Female" = "F",
                              "I identify as neither of above" = "O",
                              "Prefer not to say" = "NI"),
                  selected = "NI"),
   tags$hr(),
   # quetzio to update labels
   Quetzio_UI("updating_labels")
 )

 server <- function(input, output, session) {

   quetzio <- Quetzio$new(
     source_method = "raw",
     source_object = quetzio_examples$questions_lists$gender_update,
     module_id = "updating_labels"
   )

   # trigger need to be reactive
   gender_react <- reactive(input$gender)

   # update labels method call
   quetzio$update_labels(
     trigger = gender_react,
     source_method = "raw",
     source_object = quetzio_examples$label_update$gender_update
   )
 }
 shinyApp(ui, server)
}


Method update_values()

Method to update selected values on the change in reactive

Usage

Quetzio$update_values(values)

Arguments

values

list of values to update questionnaire with. List needs to be named, as the names are going to be used to identify which inputId to update

Examples

## only run examples in interactive environment

if (interactive()) {

 library(shiny)
 library(shiny.quetzio)

 ui <- fluidPage(
   # first questionnaire to get values from
   column(6,
          h1("Finish first questionnaire"),
          Quetzio_UI("first_questionnaire")
   ),
   # second questionnaire to update values
   column(6,
          h1("Update values of second questionnaire!"),
          actionButton("update_vals", "Update values"),
          tags$hr(),
          Quetzio_UI("second_questionnaire")
   )
 )

 server <- function(input, output, session) {

   quetzio_1st <- Quetzio$new(
     source_method = "raw",
     source_object = quetzio_examples$questions_lists$simple_quetzio,
     module_id = "first_questionnaire"
   )
   quetzio_2nd <- Quetzio$new(
     source_method = "raw",
     source_object = quetzio_examples$questions_lists$simple_quetzio,
     module_id = "second_questionnaire"
   )

   # update values on button press
   observeEvent(input$update_vals, {
     # you can use answers from one questionnaire to update another, though
     # the used values can be any other static named list
     quetzio_2nd$update_values(quetzio_1st$answers())
   })
 }
 shinyApp(ui, server)
}


Method clone()

The objects of this class are cloneable with this method.

Usage

Quetzio$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples


## ------------------------------------------------
## Method `Quetzio$new`
## ------------------------------------------------


## Only run example in interactive R sessions

if (interactive()) {

 # load libraries
 library(shiny)
 library(shiny.quetzio)

 # create ui
 ui <- fluidPage(
   column(6, align = "center",
          # bind the UI with correct module_id
          Quetzio_UI("my_quetzio")
   ),
   column(6,
          h2("State of", tags$i("Quetzio")),
          h3("Is it done?"),
          verbatimTextOutput("quetzio_is_done"),
          h3("Error messages?"),
          verbatimTextOutput("quetzio_message"),
          h3("Answers"),
          verbatimTextOutput("quetzio_answers")
   )
 )

 server <- function(input, output, session) {

   # initialize new quetzio
   questionnaire <- Quetzio$new(
     # load questions from R object
     source_method = "raw",
     source_object = quetzio_examples$questions_lists$simple_quetzio,
     # optionally add descriptions
     desc_object = quetzio_examples$description_lists$simple_quetzio,
     # use the same module_id as in UI binding
     module_id = "my_quetzio",
     # custom_css to give margin but not center options explicitly
     # it will affect only elements within the form div
     custom_css = list(
       "shiny-options-group" = "text-align: left; margin-left: 45%"
     ),
     # you can also optionally give div unique id - useful for external styling
     div_id = "my_questio_div_id"
   )

   # render objects to show your questionnaire status
 output$quetzio_is_done <-
     renderPrint(questionnaire$is_done())
   output$quetzio_message <-
     renderPrint(questionnaire$message())
   output$quetzio_answers <-
     renderPrint(questionnaire$answers())
 }

 shinyApp(ui, server)

}


## ------------------------------------------------
## Method `Quetzio$update_labels`
## ------------------------------------------------

## only run examples in interactive environment

if (interactive()) {

 library(shiny)
 library(shiny.quetzio)

 ui <- fluidPage(
   # some input to trigger label update
   selectizeInput("gender", "What is your gender?",
                  choices = c("Male" = "M",
                              "Female" = "F",
                              "I identify as neither of above" = "O",
                              "Prefer not to say" = "NI"),
                  selected = "NI"),
   tags$hr(),
   # quetzio to update labels
   Quetzio_UI("updating_labels")
 )

 server <- function(input, output, session) {

   quetzio <- Quetzio$new(
     source_method = "raw",
     source_object = quetzio_examples$questions_lists$gender_update,
     module_id = "updating_labels"
   )

   # trigger need to be reactive
   gender_react <- reactive(input$gender)

   # update labels method call
   quetzio$update_labels(
     trigger = gender_react,
     source_method = "raw",
     source_object = quetzio_examples$label_update$gender_update
   )
 }
 shinyApp(ui, server)
}


## ------------------------------------------------
## Method `Quetzio$update_values`
## ------------------------------------------------

## only run examples in interactive environment

if (interactive()) {

 library(shiny)
 library(shiny.quetzio)

 ui <- fluidPage(
   # first questionnaire to get values from
   column(6,
          h1("Finish first questionnaire"),
          Quetzio_UI("first_questionnaire")
   ),
   # second questionnaire to update values
   column(6,
          h1("Update values of second questionnaire!"),
          actionButton("update_vals", "Update values"),
          tags$hr(),
          Quetzio_UI("second_questionnaire")
   )
 )

 server <- function(input, output, session) {

   quetzio_1st <- Quetzio$new(
     source_method = "raw",
     source_object = quetzio_examples$questions_lists$simple_quetzio,
     module_id = "first_questionnaire"
   )
   quetzio_2nd <- Quetzio$new(
     source_method = "raw",
     source_object = quetzio_examples$questions_lists$simple_quetzio,
     module_id = "second_questionnaire"
   )

   # update values on button press
   observeEvent(input$update_vals, {
     # you can use answers from one questionnaire to update another, though
     # the used values can be any other static named list
     quetzio_2nd$update_values(quetzio_1st$answers())
   })
 }
 shinyApp(ui, server)
}