R Shiny:输出函数在eventReactive()中不起作用

时间:2016-08-14 15:23:13

标签: r button shiny

我想在点击按钮时使用spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver spring.datasource.url=jdbc:oracle:thin:@g9u1769.houston.hpecorp.net:1525:ODSDBD spring.datasource.username=Solid_batch spring.datasource.password=solid_batch123 spring.datasource.initialize=true uiOutput()修改用户界面。如果它不等待事件并立即更改内容,一切正常。单击按钮时,我应该更改plotOutput()output$body.on.button具有相同的输出?

server.R:

output$body.immediately

ui.R:

shinyServer(function(input, output, session) {

  output$plot <- renderPlot({plot(1:5)})
  output$body.ui <- renderUI({tags$p('This works only within renderUI')})

  output$body.on.button <- eventReactive(input$goButton, {
    switch(
      input$select,
      "text" = 'This works within both functions',
      "ui" = uiOutput("body.ui"), # This doesn't work
      "plot" = plotOutput("plot") # This doesn't work
    )
  })

  output$body.immediately <- renderUI({
    switch(
      input$select,
      "text" = 'This works within both functions',
      "ui" = uiOutput("body.ui"), # This works
      "plot" = plotOutput("plot") # This works
    )
  })
})

我也试过函数library(markdown) shinyUI( navbarPage( "Title", tabPanel( "First element", fluidRow( column( 4, wellPanel( selectInput("select", "Select", c('text', 'ui', 'plot')), actionButton("goButton", "Go!") ) # end of wellPanel ), # end of column column( 4, uiOutput("body.immediately") ), # end of column column( 4, uiOutput("body.on.button") ) # end of column ) # end of fluidROw ) # end of tabPanel ) # end of navbarPage ) # end of shinyUI ,但没有用。

1 个答案:

答案 0 :(得分:3)

它不起作用,因为您不能有两个具有相同ID的输出。在您的示例中,“Text”起作用,因为它只是一个文本 - 而不是具有ID的输出。

在下面的示例中,我添加了三个具有唯一ID的新输出。 uiOutput body.on.button在单击按钮后也会动态接收特定输出。

您可以尝试删除这三行

output$text2 <- renderText({ 'This works within both functions' })
output$body.ui2 <- renderUI({tags$p('This works only within renderUI')})
output$plot2 <- renderPlot({plot(1:10)})

然后将eventReactive中的IDs从“text2”更改为“text”(与其他两个类似),以查看单击按钮后的输出不会显示。

如果你想拥有两个相同的图,那么你应该创建两个具有唯一ID的plotOutputs,然后使用两个生成相同图的renderPlot函数。

我希望这会有所帮助。

完整示例:

server <- shinyServer(function(input, output, session) {

  # Create outputs with unique ID

  output$text <- renderText({ 'This works within both functions' })
  output$body.ui <- renderUI({tags$p('This works only within renderUI')})
  output$plot <- renderPlot({plot(1:5)})

  output$text2 <- renderText({ 'This works within both functions' })
  output$body.ui2 <- renderUI({tags$p('This works only within renderUI')})
  output$plot2 <- renderPlot({plot(1:10)})


  # Save the selected type of the output and then pass it to renderUI via react()
  react <- eventReactive(input$goButton, {
    switch(
      input$select,
      "text" = textOutput("text2"),
      "ui" = uiOutput("body.ui2"), 
      "plot" = plotOutput("plot2") 
    )
  })

  output$body.on.button <- renderUI({
    react()
  })

  output$body.immediately <- renderUI({
    switch(
      input$select,
      "text" = textOutput("text"),
      "ui" = uiOutput("body.ui"), # This works
      "plot" = plotOutput("plot") # This works
    )
  })
})


  library(markdown)


ui <-shinyUI(
  navbarPage(
    "Title",

    tabPanel(
      "First element",
      fluidRow(

        column(
          4,
          wellPanel(
            selectInput("select", "Select", c('text', 'ui', 'plot')),
            actionButton("goButton", "Go!")
          ) # end of wellPanel
        ), # end of column

        column(
          4,
          uiOutput("body.immediately")
        ), # end of column

        column(
          4,
          uiOutput("body.on.button")
        ) # end of column


      ) # end of fluidROw
    ) # end of tabPanel
  ) # end of navbarPage
) # end of shinyUI
shinyApp(ui, server)