【问题标题】:radioMatrixInput from shinySurvey doesn't work with incomplete responses来自 shinySurvey 的 radioMatrixInput 不适用于不完整的响应
【发布时间】:2022-12-02 23:05:20
【问题描述】:

我正在使用这个包 (shinySurveys),我可以很容易地使用它。我面临一个关于 radioMatrixInput 对象的问题。除非所有选项都已完成,否则我似乎无法从对象中得到响应,这是我的挑战,用户可以留下一些未回答的选项。因此,如果问题有 4 个选项,他可以回答 2 个并留下 2 个,而不为他们选择一个选项。

我正在提供我正在描述的行为的示例。我试过 observe 和 observeEvent 但都没有用。

if (interactive()) {
  
  df <- data.frame(
    question = c(rep("I love sushi.", 3), rep("I love chocolate.",3), rep("I love vegetables.",3)),
    option = c(rep(c("Disagree", "Neutral", "Agree"), 3)),
    input_type = c(rep("matrix", 9)),
    # For matrix questions, the IDs should be the same for each question
    # but different for each matrix input unit
    input_id = c(rep("matId", 9)),
    dependence = NA,
    dependence_value = NA,
    required = FALSE
  )
  
  library(shiny)
  library(shinySurveys)
  
  ui <- fluidPage(
    surveyOutput(df),
    verbatimTextOutput('debug01')
  )
  
  server <- function(input, output, session) {
    renderSurvey()

    observe({
      print(input$matId)
    })
      
    output$debug01 <- renderPrint({input$matId})
    
    observeEvent(input$submit, {
      print(input$matId)
      showModal(modalDialog(
        title = "Congrats, you completed your first shinysurvey!",
        "You can customize what actions happen when a user finishes a survey using input$submit."
      ))
    })
  }
  
  shinyApp(ui, server)
  
}

只要我提供所有选项的答案,这就可以正常工作。当我只选择一个选项时,该对象不会将任何答案返回给我在服务器中的打印功能。

你们中有人碰巧知道如何从闪亮的服务器部分访问该数据吗?

【问题讨论】:

    标签: r shiny shiny-reactivity


    【解决方案1】:

    经过一段时间的思考,我已经解决了自己的挑战。我所做的是使用对象创建了一个扩展输入类型无线电矩阵输入从包裹shinyRadioMatrix.矩阵无线电输入已经存在于 shinysurveys 中,但正如我在上面的问题中所解释的那样,它要求用户始终回答所有问题,然后才能使用函数 getSurveyData 提取答案,在我的情况下,用户应该只回答每个问题 4 个选项中的 2 个.相反,这个新的 radioMatrixInput 让我只能从用户那里得到 2 个答案,留下其他 2 个空白。

    为此,我必须将每个问题的选项重新安排到一行中,以便可以将它们分配给我的新扩展输入类型,我们称之为“matx”。这个新类型的定义如下:

    extendInputType(input_type = "matx", {
      shinyRadioMatrix::radioMatrixInput(
        inputId = surveyID(),
        rowIDs = unlist(strsplit(surveyLabel(), ",")),
        selected = NULL,
        rowLLabels = c("","","",""),
        choices = unlist(strsplit(surveyOptions(), ",")),
        rowIDsName = paste("Question",substr(surveyID(), 11,12))
      )
    })
    

    要使此扩展正常工作,必须使用辅助函数:surveyID()、surveyLabel() 和 surveyOptions()。最后两个从包含问题的数据框中带回问题和选项。

    至于我的数据框,每个问题都包含四个用逗号分隔的问题,与选项相同。在我的例子中,选项总是两个(最多和最少)。见下图

    enter image description here

    所以考虑到辅助函数将返回一个由我使用的逗号分隔的字符串向量拆分取消列表将问题分开并将它们转换为 radioMatrixInput 可以使用和读取的向量。

    这样做之后,我的问题看起来很闪亮:

    enter image description here

    好了,希望这能帮助任何面临与我类似挑战的人。

    一切顺利!

    【讨论】:

      猜你喜欢
      • 2015-10-14
      • 1970-01-01
      • 1970-01-01
      • 2015-05-28
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 2019-05-05
      相关资源
      最近更新 更多