r - 如何通过在主面板中输出注意文本来避免错误消息
问题描述
我创建了一个闪亮的应用程序,只有一个选择输入框。还有两个重要的操作按钮。
第一个按钮使用data_mean_sd
,第二个按钮使用data_mean_sd2
。
但是我遇到了一个问题,当我选择一个存在 data_mean_sd
但不存在的基因data_mean_sd2
然后单击第二个按钮时,可能会出现错误消息。
所以我从@YBS 得到了更好的答案。此处: 选择输入启用或禁用操作按钮 - 有问题
##我的问题是:
现在我想使用另一种解决方案,当我选择一个只包含在其中的基因data_mean_sd
并单击第二个按钮时,我希望注意文本可以在主面板中输出,就像:您选择的基因不包含或未找到。
我发现一个解决方案来自https://shiny.rstudio.com/articles/validation.html使用 validate 为您的 UI 编写错误消息。
但我认为我的问题必须有其他更好的解决方案。
如果我只能修改下面的零件代码?
observeEvent(input$selectGeneSymbol, {
if(sum(unique(data_mean_sd2$Gene) %in% input$selectGeneSymbol)>0) {
shinyjs::enable("plot2")
shinyjs::enable("all")
}else{
shinyjs::disable("plot2")
shinyjs::disable("all")
}
})
我希望有人能给我一些建议。不胜感激。
这是我的可重现代码和数据:
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")
data_mean_sd2<-data_mean_sd[data_mean_sd$Gene==paste0("Gene_",1:25),]
###
ui <- fluidPage(
shinyjs::useShinyjs(),
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 = "123"
))
),
fluidRow(
column(8,offset = 3,
actionButton(inputId = "plot1", label = "FPKM",width=80),
actionButton(inputId = "plot2", label = "LogFc",width=80),
actionButton(inputId = "all",label = "FPKM&LogFc",width=120)
)
),
fluidRow(
column(3)
),
fluidRow(
column(3)
),
fluidRow(
column(12,align="center",
uiOutput("plots")
)
)
)
server <- function(input, output,session) {
observeEvent(input$selectGeneSymbol, {
if(sum(unique(data_mean_sd2$Gene) %in% input$selectGeneSymbol)>0) {
shinyjs::enable("plot2")
shinyjs::enable("all")
}else{
shinyjs::disable("plot2")
shinyjs::disable("all")
}
})
plot_data1 <- eventReactive(list(input$plot1,input$all), {
subset(data_mean_sd, Gene %in% input$selectGeneSymbol)
})
plot_data2 <- eventReactive(list(input$plot2,input$all), {
subset(data_mean_sd2, Gene %in% input$selectGeneSymbol)
})
global <- reactiveValues()
observeEvent(list(input$plot1,input$all), {
req(plot_data1())
#p1 <- eventReactive(list(input$plot1,
# input$all), {
global$p1 <- ggplot(data = plot_data1(), 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$selectGeneSymbol, 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))
})
observeEvent(list(input$plot2,input$all), {
req(plot_data2())
#p2 <- eventReactive(list(input$plot2,
# input$all), {
global$p2 <- ggplot(data = plot_data2(), 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$selectGeneSymbol, 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$plt1 <- renderPlot({ global$p1 })
output$plt2 <- renderPlot({ global$p2 })
observeEvent(input$plot1, {
global$out <- plotOutput("plt1", height=600)
})
observeEvent(input$plot2, {
global$out <- plotOutput("plt2", height=600)
})
output$plots <- renderUI({
global$out
})
}
# Create Shiny app ----
shinyApp(ui = ui, server = server)
解决方案
我很兴奋,我自己解决了这个问题。
我的解决方案的重要部分是从https://shiny.rstudio.com/articles/validation.html找到答案
所以我把这个validate()
函数放在我renderPlot()
这样的地方:
output$plt2 <- renderPlot({
validate(
need(sum(unique(colnames(data_mean_sd2[,-1])) %in% input$selectGeneSymbol)>0, "Error information you personal customized.")
)
p2()
})
而且效果很好。不要忘记删除下面的代码和相关信息。
observeEvent(input$selectGeneSymbol, {
if(sum(unique(data_mean_sd2$Gene) %in% input$selectGeneSymbol)>0) {
shinyjs::enable("plot2")
shinyjs::enable("all")
}else{
shinyjs::disable("plot2")
shinyjs::disable("all")
}
})
推荐阅读
- geospatial - Kepler.GL:支持栅格切片图层
- java - 想要手动反序列化我的 Hibernate 实体。有没有办法模仿@GeneratedValue?
- javascript - 你知道任何 SaaS 构建器框架(登录、用户管理、密码重置、数据库管理、CMS)
- c++ - 从方法返回指向 flatbuffer 的指针
- python - HTTP 服务器重定向不起作用(Python)
- java - Spring -@Recover - 找不到恢复方法
- c# - 如何在 OpenTK .NET Core 中设置 OpenGL API 版本?
- cytoscape.js - 单击 cytoscape 图上的节点调用方法
- python - 熊猫数据透视表返回字符串不是值
- android - Android Studio 小部件在相对布局中消失