【问题标题】:How to refresh the plot by actionbutton in different conitions in shinyapp如何在闪亮的应用程序中通过不同条件下的操作按钮刷新绘图
【发布时间】:2021-04-22 07:18:51
【问题描述】:

我创建了一个闪亮的应用程序,其中包含三个重要按钮。

三个按钮效果很好

而且click3可以同时输出一个plot和一个table。

现在在我的应用中它们只是相互刷新,但每次只有表格仍然存在。

我的问题是现在我想修改一些部分,我希望:

plot1 和 plot2 不会刷新 click3(plot3 和 table),click3 不会刷新 plot1 或 plot2。

######### 编辑:2021-04-22 21:09:43

抱歉,我没有澄清我的问题。

现在p1(),p2(), myPlot可以互相刷新了。

但我希望myPlotmyTable 可以一直待到新的click3 刷新自己。 p1() and p2()可以互相刷新但不影响myPlotmyTable 这样p1() or p2() 就可以在mainparnel 中与myPlotmyTable 保持在一起。

我的可重现代码和数据在这里:

library(shiny)
library(ggplot2)
##  load("04.21_3.RData")

mean_data <- data.frame(
  Name = c(paste0("Group_", LETTERS[1:20])),
  matx <- matrix(sample(1:1000, 1000, replace = T), nrow = 20)
)
names(mean_data)[-1] <- c(paste0("Gene_", 1:50))

sd_data <- data.frame(
  Name = c(paste0("Group_", LETTERS[1:20])),
  matx <- matrix(runif(1000, 5, 10), nrow = 20)
)
names(sd_data)[-1] <- c(paste0("Gene_", 1:50))


############
ui <- fluidPage(
  sidebarPanel(
    selectizeInput(
      "selectGeneSymbol", 
      "Select:", 
      choices = NULL,
      multiple =F,
      width = 400,
      selected = NULL,
      options = list(placeholder = 'e.g. gene here',create = F)
    ),
    actionButton("plot1", "click1"),
    actionButton("plot2", "click2"),
    actionButton("dataTable", "click3")
  ),
  
  mainPanel(
    uiOutput("all"),
#    plotOutput("myPlot"),
    tableOutput("myTable")
  )
)

server <- function(input, output, session) {
  
  updateSelectizeInput(session, "selectGeneSymbol", choices = colnames(mean_data[,-1]), server = TRUE)
  
  global <- reactiveValues(out = NULL,
                           p1 = NULL,
                           p2 = NULL)
  plotdata <- eventReactive(input$plot1,{ 
    df <- mean_data %>% mutate(sd = sd_data[,input$selectGeneSymbol])
  })

  output$all <- renderUI({                      ##
    global$out
  })
  
  observeEvent(input$plot1, {
    global$out <- plotOutput("plot1")

  })
  ##
  observeEvent(input$plot2, {
    global$out <- plotOutput("plot2")
    myData(NULL)
  })
  
  observeEvent(input$dataTable, {
    global$out <- plotOutput("myPlot")
    myData(NULL)
  })
  ####
  myPlot = reactiveVal()
  myData = reactiveVal()
  
  observeEvent(input$dataTable, {
    data_cor<-mean_data[,-1]
    tm <- corr.test(data_cor[,input$selectGeneSymbol,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$selectGeneSymbol),]
    res<-data.frame(Gene=rownames(res),res)
    res
    ##############
    data_correlation=t(mean_data[, -1])
    data_subset=data_correlation[c(input$selectGeneSymbol, as.vector(head(res$Gene, 10))), ]
    myPlot(
        pheatmap(log2(data_subset+1), show_colnames = F,fontsize_row =12,
                 cluster_rows = F, cluster_cols = F, gaps_row = 1)
    )
    myData(res)
  })
  
  output$myPlot = renderPlot({
    myPlot()
  })
  
  output$myTable = renderTable({
    myData()
  })
  
  ####
  p1 <- eventReactive(input$plot1,
                      {
                        ggplot(data =plotdata(), aes(x = Name, y = .data[[as.name(input$selectGeneSymbol)]])) +
                          geom_bar(stat = "identity", position = position_dodge(0.9), width = 0.9) +
                          theme(legend.position = "none") +
                          labs(title = paste(input$selectGeneSymbol), x = NULL, y = "666666")                      })
  
  p2 <- eventReactive(input$plot2,
                      {
                        ggplot(data = plotdata(), aes(x = Name, y = .data[[as.name(input$selectGeneSymbol)]], fill=Name)) +
                          geom_bar(stat = "identity", position = position_dodge(0.9), width = 0.9) +
                          theme(legend.position = "none") +
                          labs(title = paste(input$selectGeneSymbol), x = NULL, y = "777777")                      })                    
                      
  output$plot1 <- renderPlot({
    p1()})
  output$plot2 <- renderPlot({
    p2()})
    
}

shinyApp(ui, server)

【问题讨论】:

  • @YBS 先生,您能给我一些建议吗?
  • 您的消息没有显示给我。也许你需要在@YBS 后面加一个逗号,。我不确定我是否理解您的问题。在这种情况下,它应该按照您期望的方式工作。请注意,几分钟前我已经回答了您的其他问题 - 看起来那里也给出了相同的代码。由于您没有包含 corr.test() 函数,因此这不是 MRE。以后请包括你使用的函数或使用公开可用的数据集并绘制它们,例如plot(cars)plot(pressure)或使用你的其他绘图p1()p2()
  • @YBS,谢谢,先生。我明白了。
  • @YBS,我的意思是如果我可以修改我的代码,以便 plot1 和 plot2 可以相互刷新,但它们不会影响 dataTable 输出(myPlot 和 myData)。而当dataTable输出(myPlot和myData)只有click3可以刷新它?当 click1 或 click2 运行时,dataTable 输出(myPlot 和 myData)仍然留在主面板中
  • 请注意,我仍然不清楚您的需求。也许您需要在上述问题中阐明您的需求或提出一个新问题。 cmets 中的信息过多不是一个好主意。由于对象global$outplot1plot2myPlot,您只能显示一个对象。您可以显示 myTable - 连续显示或仅在单击 click3 时显示。当您单击每个操作按钮时,请说明您希望在主面板中显示的内容。理想情况下,应在问题中而不是在 cmets 中显示此类说明。

标签: r shiny uioutput


【解决方案1】:

也许这是你的期望

library(shiny)
library(ggplot2)
##  load("04.21_3.RData")

mean_data <- data.frame(
  Name = c(paste0("Group_", LETTERS[1:20])),
  matx <- matrix(sample(1:1000, 1000, replace = T), nrow = 20)
)
names(mean_data)[-1] <- c(paste0("Gene_", 1:50))

sd_data <- data.frame(
  Name = c(paste0("Group_", LETTERS[1:20])),
  matx <- matrix(runif(1000, 5, 10), nrow = 20)
)
names(sd_data)[-1] <- c(paste0("Gene_", 1:50))


############
ui <- fluidPage(
  sidebarPanel(
    selectizeInput(
      "selectGeneSymbol", 
      "Select:", 
      choices = NULL,
      multiple =F,
      width = 400,
      selected = NULL,
      options = list(placeholder = 'e.g. gene here',create = F)
    ),
    actionButton("plot1", "click1"),
    actionButton("plot2", "click2"),
    actionButton("dataTable", "click3")
  ),
  
  mainPanel(
    uiOutput("all"),
    plotOutput("myPlot"),
    tableOutput("myTable")
  )
)

server <- function(input, output, session) {
  
  updateSelectizeInput(session, "selectGeneSymbol", choices = colnames(mean_data[,-1]), server = TRUE)
  
  global <- reactiveValues(out = NULL,
                           p1 = NULL,
                           p2 = NULL)
  plotdata <- eventReactive(input$plot1,{ 
    df <- mean_data %>% mutate(sd = sd_data[,input$selectGeneSymbol])
  })
  
  output$all <- renderUI({                      ##
    global$out
  })
  
  observeEvent(input$plot1, {
    global$out <- plotOutput("plot1")
    #myData(NULL)
  })
  ##
  observeEvent(input$plot2, {
    global$out <- plotOutput("plot2")
    #myData(NULL)
  })
  
  # observeEvent(input$dataTable, {
  #   global$out <- plotOutput("myPlot")
  #   
  # })
  ####
  myPlot = reactiveVal()
  myData = reactiveVal()
  
  observeEvent(input$dataTable, {
    # data_cor<-mean_data[,-1]
    # tm <- corr.test(data_cor[,input$selectGeneSymbol,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$selectGeneSymbol),]
    # res<-data.frame(Gene=rownames(res),res)
    # res
    # ##############
    # data_correlation=t(mean_data[, -1])
    # data_subset=data_correlation[c(input$selectGeneSymbol, as.vector(head(res$Gene, 10))), ]
    # myPlot(
    #   pheatmap(log2(data_subset+1), show_colnames = F,fontsize_row =12,
    #            cluster_rows = F, cluster_cols = F, gaps_row = 1)
    # )
    # myData(res)
    
    myData(mtcars)
  })
  
  p3 <- eventReactive(input$dataTable, {
    hist(runif(500))
  })
  
  output$myPlot = renderPlot({
    p3()
    #myPlot()
  })
  
  output$myTable = renderTable({
    myData()
  })
  
  ####
  p1 <- eventReactive(input$plot1,
                      {
                        ggplot(data =plotdata(), aes(x = Name, y = .data[[as.name(input$selectGeneSymbol)]])) +
                          geom_bar(stat = "identity", position = position_dodge(0.9), width = 0.9) +
                          theme(legend.position = "none") +
                          labs(title = paste(input$selectGeneSymbol), x = NULL, y = "666666")                      })
  
  p2 <- eventReactive(input$plot2,
                      {
                        ggplot(data = plotdata(), aes(x = Name, y = .data[[as.name(input$selectGeneSymbol)]], fill=Name)) +
                          geom_bar(stat = "identity", position = position_dodge(0.9), width = 0.9) +
                          theme(legend.position = "none") +
                          labs(title = paste(input$selectGeneSymbol), x = NULL, y = "777777")                      })                    
  
  output$plot1 <- renderPlot({
    p1()})
  output$plot2 <- renderPlot({
    p2()})
  
}

shinyApp(ui, server)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    • 1970-01-01
    • 2019-05-02
    • 1970-01-01
    • 2020-09-05
    • 2021-06-01
    • 1970-01-01
    相关资源
    最近更新 更多