簡體   English   中英

閃亮的動態UI:無法從使用renderUI創建的uiOutput打印結果

[英]Dynamic UI in shiny: Can't print results from uiOutput created with renderUI

我正在使用Shiny中的renderUI根據用戶的選擇創建文本字段。 然后,用戶填寫字段,並在下面的簡化版本中,目標是顯示結果。 如何從uiOutput獲取數據?

我認為該問題的答案將有所幫助: 如何在ui.R中的uioutput中獲取值並將其發送回server.R? 但不適用於我的應用。

library(shiny)
ui <- fluidPage(
  tabsetPanel(
     tabPanel("data", fluid = TRUE,
         sidebarLayout(                                                                                     
           sidebarPanel(selectInput(inputId = "age2", label = "Select", choices = c("young", "old")), 
                        actionButton("goButton", "Go!"), 
                        uiOutput("variables")),
           mainPanel(verbatimTextOutput("print"))))
    )
  )
server <- function(input, output, session) {
  output$variables <- renderUI({
    switch(input$age2, 
           "young" = numericInput("this", "This", value = NULL),
           "old" = numericInput("that", "That", value = NULL)
    ) 
  })
  x <- eventReactive(input$goButton, {
  input$variables
  })
  output$print <- renderText({
    x()
  })
} 
shinyApp(ui = ui, server = server)

我在下面修改了您的代碼,似乎符合您的情況。 所做的更改是,numericInput()對象分別命名為“ this”和“ that”,並且您的eventReactve()對象正在偵聽名為“ variables”的對象。 因為當值增加時您的numericInputs()會發生變化,因此這是React需要監聽的內容。 我同時更改了名為“ vars”的條件numericInput()對象和eventReactive中的內容,以類似地偵聽input $ vars

library(shiny)
ui <- fluidPage(
  tabsetPanel(
    tabPanel("data", fluid = TRUE,
             sidebarLayout(                                                                                     
               sidebarPanel(selectInput(inputId = "age2", label = "Select", choices = c("young", "old")), 
                            actionButton("goButton", "Go!"), 
                            uiOutput("variables")),
               mainPanel(verbatimTextOutput("print"))))
  )
)
server <- function(input, output, session) {
  output$variables <- renderUI({
    switch(input$age2, 
           "young" = numericInput("vars", "This", value = NULL),
           "old" = numericInput("vars", "That", value = NULL)
    ) 
  })
  x <- eventReactive(input$goButton, {
    input$vars

  })
  output$print <- renderText({
    x()
  })
} 
shinyApp(ui = ui, server = server)

暫無
暫無

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

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