R:输出函数don';不能在事件范围内工作

R:输出函数don';不能在事件范围内工作,r,button,shiny,R,Button,Shiny,单击按钮时,我想使用uiOutput()和plotOutput()修改UI。如果不等待事件并立即更改内容,则一切正常。单击按钮时,我应该更改什么以使output$body.on.按钮具有与output$body.相同的输出 server.R: shinyServer(function(input, output, session) { output$plot <- renderPlot({plot(1:5)}) output$body.ui <- renderUI({tag

单击按钮时,我想使用
uiOutput()
plotOutput()
修改UI。如果不等待事件并立即更改内容,则一切正常。单击按钮时,我应该更改什么以使
output$body.on.按钮
具有与
output$body.相同的输出

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
    )
  })
})
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()
,但没有效果。

它不起作用,因为不能有两个具有相同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)})

谢谢这种口是心非的行为是为了确保它能够正常工作(但我不知道它会引起额外的问题)。解决方案位于函数
renderUI()
中。现在很好用
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)