如何在Shiny模态对话框中正常显示多页shinysurveys调查?
解决Shiny模态框中shinysurveys多页调查压缩问题
问题核心在于**surveyOutput参数顺序错误**,且未正确绑定renderSurvey到对应输出ID,导致分页逻辑失效,所有调查页面被强制挤在同一页显示。
修正方案
- 给
surveyOutput指定合法的outputId参数(该参数为surveyOutput的第一个必填项),调整参数顺序 - 让
renderSurvey明确绑定到该outputId,触发正确的分页渲染逻辑 - 可选:给模态框设置
size = "l"提升宽度,优化调查内容的显示空间
修正后的完整代码
library(shiny) library(shinysurveys) survey <- data.frame( question = c("Question 1", "Question 2"), option = c("Answer 1", "Answer 2"), input_type = c("text", "text"), input_id = c("answer_1", "answer_2"), dependence = c(NA,NA), dependence_value = c(NA,NA), required =c(F, F), page = c(1,2) ) ui <- fluidPage( div(actionButton("clickforsurvey", "Take Survey")), br() ) server <- function(input, output, session) { observeEvent(input$clickforsurvey, { showModal(modalDialog( shinysurveys::surveyOutput( outputId = "survey_modal", survey = survey, survey_title = "Analyst Survey" ), size = "l" # 可选:扩大模态框宽度,避免内容拥挤 )) renderSurvey(output$survey_modal, survey) }) } shinyApp(ui, server)
关键说明
surveyOutput的第一个参数必须是outputId,原代码错误地将调查数据框放在首位,导致渲染逻辑混乱,分页功能无法触发renderSurvey必须绑定到对应的output$outputId,这样shinysurveys才能识别你设置的page字段,生成多页调查的切换按钮和分页逻辑
内容的提问来源于stack exchange,提问作者AW23
相关产品推荐
相关产品推荐

