如何使用三个不同的数据(数据、数据2和数据3)通过三个动作按钮生成和刷新结果

how to use three different data(data,data2 and data3) to generate and refresh result by three actionbutton

这是我的代码:My last question here

options(encoding = "UTF-8")
library(shiny)
library(shinythemes)
library(psych)              ## corr.test
library(DT)
library(pheatmap)
########

    data<-structure(list(Name = c("A", "A", "A", "B", "B", "B", "C", "C", 
                                  "C", "D"), `0610005C13Rik` = c(0.42, 0.28, 0.16, 0.14, 0.23, 
                                                                 0.12, 0, 0.06, 0.09, 0.27), `0610006L08Rik` = c(0, 0, 0, 0, 0, 
                                                                                                                 0, 0, 0, 0, 0), `0610007P14Rik` = c(12.81, 11.44, 13.94, 14.26, 
                                                                                                                                                     14.95, 14.55, 6.61, 8.52, 7.68, 5.13), `0610009B22Rik` = c(7.53, 
                                                                                                                                                                                                                6.55, 7.32, 7.12, 6.33, 7.12, 5.87, 3.57, 3.95, 3.49), `0610009E02Rik` = c(0.19, 
                                                                                                                                                                                                                                                                                           0.25, 0.23, 0.18, 0.28, 0.3, 0.26, 0.14, 0.11, 0.11), `0610009L18Rik` = c(1.32, 
                                                                                                                                                                                                                                                                                                                                                                     1.61, 1.26, 0.78, 1.12, 0.95, 3.45, 1.36, 0.94, 1.31), `0610009O20Rik` = c(18.73, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                17.38, 18.56, 21.46, 22.64, 21.24, 20.85, 21.85, 17.9, 23.44), 
                         `0610010B08Rik` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `0610010F05Rik` = c(5.14, 
                                                                                                4.49, 4.68, 4.63, 4.5, 4.15, 1.74, 2.3, 2.03, 2.28), `0610010K14Rik` = c(34.97, 
                                                                                                                                                                         28.72, 32.6, 29.98, 29.93, 30.05, 38.07, 29.76, 28.63, 27.74
                                                                                                )), row.names = c(NA, 10L), class = "data.frame")
    data2<-structure(list(Name = c("A", "A", "A", "B", "B", "B", "C", "C", 
                                  "C", "D"), `0610005C13Rik` = c(0.42, 0.28, 0.16, 0.14, 0.23, 
                                                                 0.12, 0, 0.06, 0.09, 0.27), `0610006L08Rik` = c(0, 0, 0, 0, 0, 
                                                                                                                 0, 0, 0, 0, 0), `0610007P14Rik` = c(12.81, 11.44, 13.94, 14.26, 
                                                                                                                                                     14.95, 14.55, 6.61, 8.52, 7.68, 5.13), `0610009B22Rik` = c(7.53, 
                                                                                                                                                                                                                6.55, 7.32, 7.12, 6.33, 7.12, 5.87, 3.57, 3.95, 3.49), `0610009E02Rik` = c(0.19, 
                                                                                                                                                                                                                                                                                           0.25, 0.23, 0.18, 0.28, 0.3, 0.26, 0.14, 0.11, 0.11), `0610009L18Rik` = c(1.32, 
                                                                                                                                                                                                                                                                                                                                                                     1.61, 1.26, 0.78, 1.12, 0.95, 3.45, 1.36, 0.94, 1.31), `0610009O20Rik` = c(18.73, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                17.38, 18.56, 21.46, 22.64, 21.24, 20.85, 21.85, 17.9, 23.44), 
                         `0610010B08Rik` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `0610010F05Rik` = c(5.14, 
                                                                                                4.49, 4.68, 4.63, 4.5, 4.15, 1.74, 2.3, 2.03, 2.28), `0610010K14Rik` = c(34.97, 
                                                                                                                                                                         28.72, 32.6, 29.98, 29.93, 30.05, 38.07, 29.76, 28.63, 27.74
                                                                                                )), row.names = c(NA, 10L), class = "data.frame")
    data3<-structure(list(Name = c("A", "A", "A", "B", "B", "B", "C", "C", 
                                  "C", "D"), `0610005C13Rik` = c(0.42, 0.28, 0.16, 0.14, 0.23, 
                                                                 0.12, 0, 0.06, 0.09, 0.27), `0610006L08Rik` = c(0, 0, 0, 0, 0, 
                                                                                                                 0, 0, 0, 0, 0), `0610007P14Rik` = c(12.81, 11.44, 13.94, 14.26, 
                                                                                                                                                     14.95, 14.55, 6.61, 8.52, 7.68, 5.13), `0610009B22Rik` = c(7.53, 
                                                                                                                                                                                                                6.55, 7.32, 7.12, 6.33, 7.12, 5.87, 3.57, 3.95, 3.49), `0610009E02Rik` = c(0.19, 
                                                                                                                                                                                                                                                                                           0.25, 0.23, 0.18, 0.28, 0.3, 0.26, 0.14, 0.11, 0.11), `0610009L18Rik` = c(1.32, 
                                                                                                                                                                                                                                                                                                                                                                     1.61, 1.26, 0.78, 1.12, 0.95, 3.45, 1.36, 0.94, 1.31), `0610009O20Rik` = c(18.73, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                17.38, 18.56, 21.46, 22.64, 21.24, 20.85, 21.85, 17.9, 23.44), 
                         `0610010B08Rik` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `0610010F05Rik` = c(5.14, 
                                                                                                4.49, 4.68, 4.63, 4.5, 4.15, 1.74, 2.3, 2.03, 2.28), `0610010K14Rik` = c(34.97, 
                                                                                                                                                                         28.72, 32.6, 29.98, 29.93, 30.05, 38.07, 29.76, 28.63, 27.74
                                                                                                )), row.names = c(NA, 10L), class = "data.frame")
    
    ######
    
    
    ui <- fluidPage(
        mainPanel("666",
    #      width = 9,
                      tabPanel("123",
                               hr(),
                               typeaheadInput(
                                 "select", 
                                 "Select", 
                                 choices = NULL,
                                 minLength = 1,
                                 value = "0610005C13Rik"
                               ),
                               #####
                               actionButton(inputId = "plot_1", label = "123",width=80,class="btn btn-success"),
                               actionButton(inputId = "plot_2", label = "456",width=80,class="btn btn-light"),
                               actionButton(inputId = "plot_3", label = "789",width=80,class="btn btn-danger"),
                               hr(),
                               uiOutput("all"),
                               uiOutput("mydata"),
                               # dataTableOutput('myTable1'),
                               # dataTableOutput('myTable2'),
                               # dataTableOutput('myTable3')
                               #####
                      )
          ))
    server <- function(input, output, session) {
      
      updateTypeaheadInput(session, "select", choices = colnames(data[,-1]))
      observeEvent(list(input$plot_1,input$plot_2,input$plot_3), {
        updateTabsetPanel(session, "666",
                          selected = "123"
        )
      })
    
      #################
      global <- reactiveValues(out = NULL, 
                               data = NULL,
                               data2=NULL,
                               data3=NULL
      )
      
      ######
      output$all <- renderUI({                     
        global$out
      })
      
      output$mydata <- renderUI({                     
        global$data
        global$data2
        global$data3
      })
      
      #####
      observeEvent(input$plot_1,{
        global$out <- plotOutput("myPlot_1")
        global$data <- DTOutput("myTable1")
    #    myData_2(NULL)
    #    myData_3(NULL)
      })
      
      observeEvent(input$plot_2, {
        
        global$out <- plotOutput("myPlot_2")
        global$data2 <- DTOutput("myTable2")
        # myData_1(NULL)
        # myData_3(NULL)
      })
      
      observeEvent(input$plot_3, {
        global$outtt <- plotOutput("myPlot_3")
        global$data3 <- DTOutput("myTable3")
        # myData_1(NULL)
        # myData_2(NULL)
      })
     
      #########################################
      ##
      myPlot_1 = reactiveVal()
      myPlot_2 = reactiveVal()
      myPlot_3 = reactiveVal()
      #
      myData_1 = reactiveVal()
      myData_2 = reactiveVal()
      myData_3 = reactiveVal()
    
    
      ####################################  plot_1 plot_2
      observeEvent(input$plot_1, {
        validate(need(sum(unique(colnames(data[,-1])) %in% input$select)>0, "The gene is not found."))
        data_cor<-data[,-1]
        tm <- corr.test(data_cor[,input$select,drop=FALSE],
                        y = data_cor, use = "pairwise", "spearman", adjust="none", 
                        alpha=0.05, ci=F, minlength=5)
        res <-setNames(as.data.frame(t(do.call(rbind, tm[c("r", "p")]))), c("Correlation", "P_value"))
        res<-res[-which(rownames(res)== input$select),]
        res<-data.frame(Gene=rownames(res),res)
        res<-res[order(res$Correlation,decreasing = T),]
        rownames(res)<-NULL
        res<-na.omit(res)
        res
        ##############
        data_correlation=t(data[, -1])
        data_subset=data_correlation[c(input$select, as.vector(head(res$Gene, 9))), ]
        myPlot_1(
          
          if(nrow(data_subset)>1){
            pheatmap(log2(data_subset+1), show_colnames = F,fontsize_row =13,
    #                 labels_row = as.expression(lapply(rownames(data_subset), function(a) bquote(italic(.(a))))),
                     cluster_rows = F, cluster_cols = F, 
                     fontsize = 11,
                     cellwidth=4
            )
          }
        )
        myData_1(res)
      })        
      ######################################## 2
      observeEvent(input$plot_2, {
        validate(need(sum(unique(colnames(data2[,-1])) %in% input$select)>0, "The gene is not found."))
        data_cor<-data2[,-1]
        tm <- corr.test(data_cor[,input$select,drop=FALSE],
                        y = data_cor, use = "pairwise", "spearman", adjust="none", 
                        alpha=0.05, ci=F, minlength=5)
        res <-setNames(as.data.frame(t(do.call(rbind, tm[c("r", "p")]))), c("Correlation", "P_value"))
        res<-res[-which(rownames(res)== input$select),]
        res<-data.frame(Gene=rownames(res),res)
        res<-res[order(res$Correlation,decreasing = T),]
        rownames(res)<-NULL
        res<-na.omit(res)
        res
        ##############
        data_correlation=t(data2[, -1])
        data_subset=data_correlation[c(input$select, as.vector(head(res$Gene, 9))), ]
        myPlot_2(
          
          if(nrow(data_subset)>1){
            pheatmap(log2(data_subset+1), show_colnames = F,fontsize_row =13,
                     #                 labels_row = as.expression(lapply(rownames(data_subset), function(a) bquote(italic(.(a))))),
                     cluster_rows = F, cluster_cols = F, 
                     fontsize = 11,
                     cellwidth=4
            )
          }
        )
        myData_2(res)
      })     
      ########################################  3
      observeEvent(input$plot_3, {
        validate(need(sum(unique(colnames(data3[,-1])) %in% input$select)>0, "The gene is not found."))
        data_cor<-data3[,-1]
        tm <- corr.test(data_cor[,input$select,drop=FALSE],
                        y = data_cor, use = "pairwise", "spearman", adjust="none", 
                        alpha=0.05, ci=F, minlength=5)
        res <-setNames(as.data.frame(t(do.call(rbind, tm[c("r", "p")]))), c("Correlation", "P_value"))
        res<-res[-which(rownames(res)== input$select),]
        res<-data.frame(Gene=rownames(res),res)
        res<-res[order(res$Correlation,decreasing = T),]
        rownames(res)<-NULL
        res<-na.omit(res)
        res
        ##############
        data_correlation=t(data3[, -1])
        data_subset=data_correlation[c(input$select, as.vector(head(res$Gene, 9))), ]
        myPlot_3(
          
          if(nrow(data_subset)>1){
            pheatmap(log2(data_subset+1), show_colnames = F,fontsize_row =13,
                     #                 labels_row = as.expression(lapply(rownames(data_subset), function(a) bquote(italic(.(a))))),
                     cluster_rows = F, cluster_cols = F, 
                     fontsize = 11,
                     cellwidth=4
            )
          }
        )
        myData_3(res)
      })     
      ###################################  1
      output$myPlot_1 = renderPlot({
        req(myPlot_1())
        myPlot_1()
      })
      
      output$myTable1 = renderDataTable({
        req(myData_1())
    
        myData_1()
        
      })
      #################################  2
      output$myPlot_2 = renderPlot({
    
        req(myPlot_2())
        myPlot_2()
      })
      
      output$myTable2 = renderDataTable({
    
        req(myData_2())
        
        myData_2()
        
      })
      #################################  3
      output$myPlot_3 = renderPlot({
    
        req(myPlot_3())
        myPlot_3()
      })
      
      output$myTable3 = renderDataTable({
    
        req(myData_3())
        myData_3()
        
      })
      
    }
    
    ###################
    
    # Create Shiny app ----
    shinyApp(ui = ui, server = server)

我使用了三个不同的数据并生成了相似的结果(一张图片和 table 在一起)。

但它只有在点击第三个按钮时才有效。我不知道为什么前两个动作按钮不生成类似的 tables.

here是@YBS给出的答案,但只有相同的数据。

我的问题是如何修改我的代码以使用三种不同的数据。 我下面的新代码不起作用。我希望有人能帮助我。

下面的代码对我来说工作正常。我没有更改其余代码。请注意,myTable1、myTable2、myTable3是根据data、data2、data3呈现不同的数据表。

ui <- fluidPage(
  mainPanel("666",
            #      width = 9,
            tabPanel("123",
                     hr(),
                     selectInput(
                       "select", 
                       "Select", 
                       choices = NULL#,
                       #minLength = 1,
                       #value = "0610005C13Rik"
                     ),
                     #####
                     actionButton(inputId = "plot_1", label = "123",width=80,class="btn btn-success"),
                     actionButton(inputId = "plot_2", label = "456",width=80,class="btn btn-light"),
                     actionButton(inputId = "plot_3", label = "789",width=80,class="btn btn-danger"),
                     hr(),
                     uiOutput("all"),
                     uiOutput("mydata"),
                     # dataTableOutput('myTable1'),
                     # dataTableOutput('myTable2'),
                     # dataTableOutput('myTable3')
                     #####
            )
  ))
server <- function(input, output, session) {
  
  updateSelectInput(session, "select", choices = colnames(data[,-1]))
  observeEvent(list(input$plot_1,input$plot_2,input$plot_3), {
    updateTabsetPanel(session, "666",
                      selected = "123"
    )
  })
  
  #################
  global <- reactiveValues(out = NULL, 
                           data = NULL
  )
  
  ######
  output$all <- renderUI({                     
    global$out
  })
  
  output$mydata <- renderUI({                     
    global$data
  })
  
  #####
  observeEvent(input$plot_1,{
    global$out <- plotOutput("myPlot_1")
    global$data <- DTOutput("myTable1")
  })
  
  observeEvent(input$plot_2, {
    
    global$out <- plotOutput("myPlot_2")
    global$data <- DTOutput("myTable2")
  })
  
  observeEvent(input$plot_3, {
    global$outtt <- plotOutput("myPlot_3")
    global$data <- DTOutput("myTable3")
  })