2016-08-14 78 views
2

我想在单击按钮时使用uiOutput()plotOutput()修改UI。如果它不等待事件并立即更改内容,则一切正常。当按钮被点击时,我应该改变什么使output$body.on.buttonoutput$body.immediately具有相同的输出?R Shiny:输出功能在eventReactive()内不起作用

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

ui.R:

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 

我也试着功能observeEvent(),但它并没有帮助。

回答

3

它不起作用,因为您不能有两个具有相同ID的输出。在你的例子中,“文本”的作品是因为它只是一个文本 - 而不是带有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)}) 

,然后从“文本2”中eventReactive改变IDs为“文本”(与类似的其他两个)看到,点击后的输出在按钮上不会显示。

如果您想要有两个相同的图,那么您应该创建两个带有唯一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) 
+1

谢谢!重复是故意创建的,以确保它能正常工作(但我不知道它会引起额外的问题)。该解决方案在函数'renderUI()'中。现在它工作正常 –