我正在创建一个动态创建一组输入框的闪亮应用程序,然后使用这些框的输入进一步创建另一组输入框。
首先,我从"问题数量得到输入"文本框并动态添加该数量的"问题面板"到ui(下面的例子)。
问题
问题是生成的对象总是附加到输出,我无法弄清楚如何从中获取新的用户输入。我怎么能得到这个输入,然后生成第二轮"回答"输入框?
shinyUI(fluidPage(
titlePanel("RSurvey"),
numericInput("questionCountText", label = h3("Number of Questions"), value = 1),
uiOutput("questionSet")
))
shinyServer(
function(input, output) {
output[['questionSet']] <- renderUI({
outputHtml = ""
count = input$questionCountText
if(count > 0) {
for(i in 1:input$questionCountText) {
outputHtml = paste0(outputHtml, questionPanel(i))
}
}
HTML(outputHtml)
})
}
)
questionPanel = function(i)
{
return(wellPanel(div(style="display:inline-block", textInput("questionText", label = h4(paste0("Question ", i)), "Enter your question")),
numericInput1("answerCountText", label = h4("Number of Answers"), value = 3, onchange="onTextChanged(this.value)")))
}
numericInput1 = function (inputId, label, value = "", ...)
{
div(style="display:inline-block",
tags$label(label, `for` = inputId),
tags$input(id = inputId, type = "numeric", value = value, ...))
}
答案 0 :(得分:1)
你好试试这个例子:
ui <- fluidPage(
titlePanel("RSurvey"),
numericInput("questionCountText", label = h3("Number of Questions"), value = 1),
uiOutput("questionSet"),
verbatimTextOutput(outputId = "answers")
)
server <- function(input, output) {
output[['questionSet']] <- renderUI({
outputHtml = ""
count = input$questionCountText
if(count > 0) {
for(i in 1:input$questionCountText) {
outputHtml = paste0(outputHtml, questionPanel(i))
}
}
HTML(outputHtml)
})
output$answers <- renderPrint({
invisible(
sapply(
X = seq_len(input$questionCountText),
FUN = function(i) {
cat(paste0("Question", i, "\n", input[[paste0("questionText", i)]], "\n", input[[paste0("answerCountText", i)]], "\n"))
}
)
)
})
}
shinyApp(ui = ui, server = server)
questionPanel = function(i) {
wellPanel(
div(
style="display:inline-block",
textInput2(inputId = paste0("questionText", i), label = h4(paste0("Question ", i)), placeholder = "Enter your question")
),
numericInput1(inputId = paste0("answerCountText", i), label = h4("Number of Answers"), value = 3, onchange="onTextChanged(this.value)")
)
}
numericInput1 = function (inputId, label, value = "", ...) {
div(style="display:inline-block", class = "form-group shiny-input-container",
tags$label(label, `for` = inputId),
tags$input(id = inputId, type = "number", value = value, ...))
}
`%AND%` <- shiny:::`%AND%`
textInput2 <- function (inputId, label, value = "", placeholder = NULL, width = NULL)
{
if (is.null(placeholder)) {
div(class = "form-group shiny-input-container", style = if (!is.null(width))
paste0("width: ", validateCssUnit(width), ";"), label %AND%
tags$label(label, `for` = inputId), tags$input(id = inputId,
type = "text", class = "form-control", value = value))
} else {
div(class = "form-group shiny-input-container", style = if (!is.null(width))
paste0("width: ", validateCssUnit(width), ";"), label %AND%
tags$label(label, `for` = inputId), tags$input(id = inputId, placeholder = placeholder,
type = "text", class = "form-control", value = value))
}