r-将反应性UI包裹在动作按钮rshiny中



i有一个选项卡式UI,每当用户在数据表中选择行时出现(在以下代码中,输出是随机的,在现实生活中,计算涉及(。

我想调节显示的选项卡UI,并单击按钮。当前,每次您选择一个额外的行时,它都会对已经选择的行进行一次计算。当用户完成他想看的所有行时,我想将其限制为一次性计算。

library(shiny)
library(DT)

UI:表,动作按钮和选项卡式部分。

ui <- fluidPage(
  mainPanel(
    fluidRow(
      column(12,DT::dataTableOutput(outputId = 'tableCurrencies'))
    ),
    actionButton(inputId = 'showSelectedButton',label = 'Show Selec'),
    fluidRow(
      uiOutput("myTabUI")
    )
  )
)

server函数:如果我删除output$myTabUI <- eventReactive(input$launchCalcButton, {零件,而直接执行output$myTabUI <- renderUI ({ ...,则可以按预期工作(当然单击按钮之后的计算(。

server <- function(input,output){
  output$tableCurrencies <- DT::renderDataTable({datatable(data.frame(a=rnorm(10),b=rnorm(10),c=rnorm(10)))})
  origTable_selected <- reactive({
    ids <- input$tableCurrencies_rows_selected
    return(ids)
  })
  output$myTabUI <- eventReactive(input$launchCalcButton, {
      selectedTabs <- renderUI({
        myTabs <- lapply(origTable_selected(),function(i) {

          tabName <- paste0("test",i)
          a <- renderPlot({
            hist(rnorm(50))
          })
          output[[paste0(tabName,"rates")]] <- a
          return(tabPanel(
            tabName,
            fluidRow(
              column(6,plotOutput(paste0(tabName,"rates")))
            )
          ))
        })
        return(do.call(tabsetPanel,myTabs))
      })
      selectedTabs
    })
}
app = shinyApp(ui,server)
runApp(app,port=3250,host='0.0.0.0')

不确定如何解决此问题。欢迎任何帮助。

您可以使用isolate()限制反应性依赖性

library(shiny)
library(DT)
ui <- fluidPage(
  mainPanel(
    fluidRow(
      column(12,DT::dataTableOutput(outputId = 'tableCurrencies'))
    ),
    actionButton(inputId = 'showSelectedButton',label = 'Show Selec'),
    fluidRow(uiOutput("myTabUI"))
  )
)
server <- function(input,output){
  output$tableCurrencies <- DT::renderDataTable({
    data.frame(a=rnorm(10),b=rnorm(10),c=rnorm(10))})     
  origTable_selected <- reactive({
    input$tableCurrencies_rows_selected        
  })      
  output$myTabUI <- renderUI({
    input$showSelectedButton
    myTabs <- lapply(isolate(origTable_selected()),function(i) {          
      tabName <- paste0("test",i)          
      a <- renderPlot({hist(rnorm(50))})
      output[[paste0(tabName,"rates")]] <- a
      return(tabPanel(
        tabName,
        fluidRow(column(6,plotOutput(paste0(tabName,"rates"))))
      ))
    })
    do.call(tabsetPanel,myTabs)
  })
}
shinyApp(ui,server)

最新更新