r - 当我单击它时,闪亮的按钮不起作用。某人可以帮助我吗?
问题描述
我想添加 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_", 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)
谁能帮我找出我的代码哪里出错了。非常感谢
解决方案
这是一种在单击第一个按钮时获取第一个图的输出的方法。您可以对其他绘图使用相同的过程。
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_", 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) {
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, 5, 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)
推荐阅读
- c# - 如何在 Asp.NET Core 中呈现不同的布局
- sql - soapUI JDBC 测试步骤中的多个 SQL 语句
- php - 如何在日期和时间之间添加类?
- javascript - 更新烧瓶而不刷新
- bash - Sed 内联动态替换表达式抛出:不匹配的 '|'
- javascript - Flask 没有看到 React 的 'bundle.js',只有 'index.html'
- javascript - 如何在 VueJS 2 中列出来自 firebase 的项目?
- python - 如何使用 azureml 中上传到 pandas 的数据集进行分析?
- spring-boot - 文本响应中的邮递员“意外令牌错误”
- c# - Unity:应用预制件时的空引用