簡體   English   中英

如何從輸出對象獲取用戶輸入

[英]How to get user input from an output object

我正在創建一個可以動態創建一組輸入框的閃亮應用程序,然后將這些框的輸入用於進一步創建另一組輸入框。

首先,我從“問題數量”文本框中輸入內容,並將該數量的“問題面板”動態添加到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, ...))
}

您好嘗試例如:

#ui

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"))
        }
      )
    )
  })
}

#app

shinyApp(ui = ui, server = server)

#utils

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))
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM