微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当我点击它时,闪亮的按钮不起作用 sb能帮我吗?

如何解决当我点击它时,闪亮的按钮不起作用 sb能帮我吗?

我想添加 3 个不同的按钮来显示不同的情节但使用相同的输入信息。

但现在我被困在了第一步。 当我单击链接到第一个图的第一个按钮时,它不起作用。

谁能帮我处理一下。非常感谢。

以下是我的示例代码

library(shiny)
library(ggplot2)
library(ggpubr)
library(dplyr)
library(tidyr)
#####
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_",matx <- matrix(runif(1000,5,10),nrow = 20)
)
names(sd_data)[-1] <- c(paste0("Gene_",1:50))

# Prepare dataset.
#   1. Bind mean and sd data
#   2. Reshape
data <- bind_rows(list(
  mean = mean_data,sd = sd_data
),.id = "stat")
data_mean_sd <- data %>%
  pivot_longer(-c(Name,stat),names_to = "Gene",values_to = "value") %>%
  pivot_wider(names_from = "stat",values_from = "value")
###
ui <- fluidPage(
  fluidRow(
    column(8,offset = 3,h2("Gene_FPKM Value Barplot")
    )
  ),fluidRow(
    column(8,selectInput(
             "selectGenesymbol","Select Gene Symbol:",choices = unique(data_mean_sd$Gene),multiple =F,width = 800,selected = "Igfbp7"
           ))
  ),actionButton(inputId = "FPKM",label = "FPKM"),actionButton(inputId = "logFC",label = "logFC"),actionButton(inputId = "logFC&FPKM",label = "logFC&FPKM")
    )
  ),fluidRow(
    column(3)
  ),fluidRow(
    column(12,align="center",plotOutput(outputId = "barplot1",height = 700,width = 1300)
    )
  ),plotOutput(outputId = "barplot2",width = 1300)
    )
  )
)

server <- function(input,output) {
  
  data_FPKM <- eventReactive(input$FPKM,{
    plot_data <- reactive({
      subset(data_mean_sd,Gene %in% input$selectGenesymbol)
    })
    ggplot(data = plot_data(),aes(x = Name,y = mean,fill=Name)) +
      geom_bar(stat = "identity",position = position_dodge(0.9),width = 0.9) +
      geom_errorbar(aes(ymin = mean - sd,ymax = mean + sd),width = .2,position = position_dodge(0.9)) +
      theme_classic2() +
      rotate_x_text(angle = 45) +
      theme(legend.position = "none") +
      labs(title = input$Genesymbol,x = NULL,y = "FPKM_value") +
      theme(plot.title = element_text(hjust = 0.5)) +
      theme(plot.margin = unit(c(20,1,5),"mm"))+
      theme(axis.text.x=element_text(vjust=1,size=12))
  })  ##  建立 按钮与 数据的关系
  
  output$barplot <- renderPlot(
    {
      barplot(data_FPKM())
    }) 
  
}

# Create Shiny app ----
shinyApp(ui = ui,server = server)

谁能帮我找出我的代码哪里错了。非常感谢

解决方法

这是一种通过单击第一个按钮获得第一个图的输出的方法。您可以对其他图使用相同的过程。

library(shiny)
library(ggplot2)

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_",matx <- matrix(runif(1000,5,10),nrow = 20)
)
names(sd_data)[-1] <- c(paste0("Gene_",1:50))

# Prepare dataset.
#   1. Bind mean and sd data
#   2. Reshape
data <- bind_rows(list(
  mean = mean_data,sd = sd_data
),.id = "stat")
data_mean_sd <- data %>%
  pivot_longer(-c(Name,stat),names_to = "Gene",values_to = "value") %>%
  pivot_wider(names_from = "stat",values_from = "value")
###
ui <- fluidPage(
  fluidRow(
    column(8,offset = 3,h2("Gene_FPKM Value Barplot")
    )
  ),fluidRow(
    column(8,selectInput(
             "selectGeneSymbol","Select Gene Symbol:",choices = unique(data_mean_sd$Gene),multiple =F,width = 800,selected = "Igfbp7"
           ))
  ),actionButton(inputId = "FPKM",label = "FPKM"),actionButton(inputId = "logFC",label = "logFC"),actionButton(inputId = "logFC&FPKM",label = "logFC&FPKM")
    )
  ),fluidRow(
    column(3)
  ),fluidRow(
    column(12,align="center",plotOutput(outputId = "barplot1",height = 700,width = 1300)
    )
  ),plotOutput(outputId = "barplot2",width = 1300)
    )
  )
)

server <- function(input,output) {
  
  
  plot_data <- reactive({
    subset(data_mean_sd,Gene %in% input$selectGeneSymbol)
  })
  
  v <- reactiveValues(barplot1 = NULL,barplot2 = NULL)
  
  observeEvent(input$FPKM,{
    ggplot(data = plot_data(),aes(x = Name,y = mean,fill=Name)) +
      geom_bar(stat = "identity",position = position_dodge(0.9),width = 0.9) +
      geom_errorbar(aes(ymin = mean - sd,ymax = mean + sd),width = .2,position = position_dodge(0.9)) +
      theme_classic2() +
      rotate_x_text(angle = 45) +
      theme(legend.position = "none") +
      labs(title = input$GeneSymbol,x = NULL,y = "FPKM_value") +
      theme(plot.title = element_text(hjust = 0.5)) +
      theme(plot.margin = unit(c(20,1,5),"mm"))+
      theme(axis.text.x=element_text(vjust=1,size=12)) -> v$barplot1
  })  
  
  output$barplot1 <- renderPlot({
    v$barplot1
  })
  
}

# Create Shiny app ----
shinyApp(ui = ui,server = server)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。