I want to add 3 different buttons to show different plot but use the same input information.
But now I was trapped in the first step. when I click the first button which is linked to the first plot but it didn't work.
Can somebody help me to deal with it. Thanks a lot.
Here is my sample code below:
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_", LETTERS[1:20])),
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,offset = 3,
selectInput(
"selectGeneSymbol",
"Select Gene Symbol:",
choices = unique(data_mean_sd$Gene),
multiple =F,
width = 800,
selected = "Igfbp7"
))
),
fluidRow(
column(8,offset = 3,
actionButton(inputId = "FPKM", label = "FPKM"),
actionButton(inputId = "logFC", label = "logFC"),
actionButton(inputId = "logFC&FPKM",label = "logFC&FPKM")
)
),
fluidRow(
column(3)
),
fluidRow(
column(3)
),
fluidRow(
column(12,align="center",
plotOutput(outputId = "barplot1",height = 700, width = 1300)
)
),
fluidRow(
column(12,align="center",
plotOutput(outputId = "barplot2",height = 700, 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, 5, 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)
Who can help me to find where the wrong with my code. Many thanks