r - 使用显示不同图的两个不同数据集创建单个闪亮的应用程序
问题描述
我有两种类型的数据集df.m
和df2
. 使用数据集df.m
,df2
我为每个数据集分别制作了一个应用程序,输出给出了不同的图。下面我将展示数据集以及server
每个应用程序的侧面。两个应用程序的用户界面ui
相同,为了复制,我只是在这里展示。
这两个应用程序都运行良好,没有任何问题。
library(reshape2)
library(ggplot2)
library(ggpubr)
library(EnvStats)
library(shinyjs)
library(shiny)
library(shinyjs)
library(tidyverse)
library(survminer)
library(survival)
df <- structure(list(Samples = structure(1:10, .Label = c("Sample1",
"Sample10", "Sample2", "Sample3", "Sample4", "Sample5", "Sample6",
"Sample7", "Sample8", "Sample9"), class = "factor"), Type = structure(c(2L,
1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L), .Label = c("Normal", "Tumor"
), class = "factor"), A1BG = c(0, 0.01869105, 0.026705782, 0.016576987,
0, 0.007636787, 0.015756547, 0.00609601, 0.115575528, 0.04717536
), A1BG.AS1 = c(0, 0.096652515, 0.086710002, 0.04683499, 0.188283185,
0.104318353, 0.102735593, 0.100064808, 0.04717536, 0.159745808
), A1CF = c(1.616942802, 1.367084444, 1.101855892, 1.3823884,
0.631627098, 2.407159505, 1.687449785, 1.229844138, 0.87989414,
0.642785868), A2M = c(3.357654845, 3.149165846, 3.654774122,
2.851143092, 2.952601867, 4.002335454, 4.123949457, 3.691343955,
3.553064673, 3.425443559), A2M.AS1 = c(0.217308191, 0.08268571,
0.297320544, 0.101579093, 0.020102613, 0.35578965, 0.288014115,
0.145352771, 0.043808388, 0.104677012), A2ML1 = c(0, 0.017949113,
0.00984907, 0.002289616, 0, 0.002100359, 0.032146138, 0.052275569,
0.537892142, 0), A2ML1.AS1 = c(0.631627098, 0.04717536, 1.229844138,
0, 4.002335454, 0, 1.229844138, 1.229844138, 0.04717536, 0)), row.names = c(NA,
-10L), class = "data.frame")
df.m <- reshape2::melt(df, c("Samples", "Type"))
colnames(df.m)[3] <- "GENE"
df2 <- structure(list(Samples = structure(c(1L, 12L, 23L, 34L, 45L,
46L, 47L, 48L, 49L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 24L, 25L, 26L,
27L, 28L, 29L, 30L, 31L, 32L, 33L, 35L, 36L, 37L, 38L, 39L, 40L,
41L, 42L, 43L, 44L), .Label = c("Sample1", "Sample10", "Sample11",
"Sample12", "Sample13", "Sample14", "Sample15", "Sample16", "Sample17",
"Sample18", "Sample19", "Sample2", "Sample20", "Sample21", "Sample22",
"Sample23", "Sample24", "Sample25", "Sample26", "Sample27", "Sample28",
"Sample29", "Sample3", "Sample30", "Sample31", "Sample32", "Sample33",
"Sample34", "Sample35", "Sample36", "Sample37", "Sample38", "Sample39",
"Sample4", "Sample40", "Sample41", "Sample42", "Sample43", "Sample44",
"Sample45", "Sample46", "Sample47", "Sample48", "Sample49", "Sample5",
"Sample6", "Sample7", "Sample8", "Sample9"), class = "factor"),
years = c(1.301369863, 0.4, 1.054794521, 0.134246575, 0.794520548,
3.287671233, 3.646575342, 3.887671233, 3.646575342, 3.619178082,
3.575342466, 2.02739726, 3.523287671, 3.742465753, 2.926027397,
3.8, 1.161643836, 1.380821918, 3.087671233, 3.104109589,
3.084931507, 2.887671233, 2.778082192, 2.728767123, 3.043835616,
1.210958904, 2.704109589, 2.742465753, 2.635616438, 2.536986301,
2.432876712, 2.794520548, 1.967123288, 1.84109589, 1.838356164,
2.726027397, 2.430136986, 2.257534247, 1.876712329, 2.010958904,
0.698630137, 2.090410959, 2.098630137, 2.01369863, 1.717808219,
1.81369863, 2.057534247, 2.032876712, 1.989041096), patient.vital_status = c(0L,
0L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L,
1L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L), A1BG = structure(c(1L, 1L, 2L, 2L, 1L, 2L, 2L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L,
2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L,
1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L), .Label = c("high",
"low"), class = "factor"), A1CF = structure(c(1L, 1L, 1L,
1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L,
2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L), .Label = c("high", "low"), class = "factor")), class = "data.frame", row.names = c(NA,
-49L))
ui <- fluidPage(
titlePanel("plots"),
sidebarLayout(
sidebarPanel(
selectInput(inputId = "thegene", label = "Gene", choices = c("FAP", "ADSL", "SIX2"), selected = "SIX2"),
radioButtons(inputId = "FileType", label = "Select the file type", choices = list("png", "pdf"), selected = "pdf"),
width = 3
),
mainPanel(
plotOutput("plot"),
downloadButton(outputId = "downloadPlot", label = "Download the plot"),
width = 9
)
)
)
server <- function(input, output, session) {
vals <- reactiveValues()
alldat <- reactive({
choices <- unique(df.m$GENE)
selected <- isolate(input$thegene)
if (!selected %in% choices) selected <- choices[1]
updateSelectInput(session, "thegene", choices = choices, selected = selected)
df.m
})
dat <- reactive({
x <- alldat()
x[ x$GENE == input$thegene,,drop=FALSE]
})
output$plot <- renderPlot({
gg <- ggplot(data = dat(), aes(x = Type, y = value, fill=GENE)) +
geom_boxplot() +
theme_bw(base_size = 14) + xlab("") + ylab("Expression logFPKM") +
theme(axis.text=element_text(size=15, face = "bold", color = "black"),
axis.title=element_text(size=15, face = "bold", color = "black"),
strip.text = element_text(size=15, face = "bold", color = "black")) +
scale_fill_manual(values = input$colour) +
stat_compare_means(method = "t.test", size=5) +
stat_n_text()
vals$gg <- gg
print(gg)
})
output$downloadPlot <- downloadHandler(
filename = function() {
paste(input$thegene, input$FileType,sep=".")
},
# content is a function with argument file. content writes the plot to the device
content = function(file){
if(input$FileType=="png")
png(file, units="in", width=5, height=5, res=300)
else
pdf(file, width = 5, height = 5)
print(vals$gg)
dev.off()
}
)
}
# Run the application
shinyApp(ui = ui, server = server)
#############################################################################################
server <- function(input, output, session) {
genes <- as.factor(names(df2[c(4:5)]))
env <- parent.frame()
vals <- reactiveValues()
alldat <- reactive({
choices <- genes
selected <- isolate(input$thegene)
if (!selected %in% choices) selected <- choices[1]
updateSelectInput(session, "thegene", choices = choices, selected = selected)
df2
})
dat <- reactive({
x <- alldat()
x[ x$variable == input$thegene,,drop=FALSE]
})
output$plot <- renderPlot({
fit <- eval(parse(text = paste0("survfit(Surv(years, patient.vital_status) ~ ", input$thegene, ", data = final)")))
gg <-ggsurvplot(fit,
data = final,
pval = TRUE, conf.int = FALSE,
risk.table = TRUE, # Add risk table
risk.table.col = "strata", # Change risk table color by groups
linetype = "strata", # Change line type by groups
surv.median.line = "hv", # Specify median survival
ggtheme = theme_bw(), # Change ggplot2 theme
palette = c("#FF0027", "#060606"),
xlim = c(0,10),
break.x.by = 3,
xlab="Time in years",
risk.table.y.text.col = T, # colour risk table text annotations.
risk.table.y.text = FALSE)
vals$gg <- gg
print(gg)
})
output$downloadPlot <- downloadHandler(
filename = function() {
paste(input$thegene, input$FileType,sep=".")
},
# content is a function with argument file. content writes the plot to the device
content = function(file){
if(input$FileType=="png")
png(file, units="in", width=5, height=5, res=300)
else
pdf(file, width = 5, height = 5, onefile = FALSE)
print(vals$gg)
dev.off()
}
)
}
# Run the application
shinyApp(ui = ui, server = server)
现在,我想使用这两个数据集并制作一个应用程序,而不是每个数据集的每个应用程序,我想在其中显示两个图(输出)。我还希望 Shiny 应用程序允许用户选择要显示的绘图输出。不知道该怎么做。任何帮助表示赞赏。谢谢。
解决方案
推荐阅读
- javascript - 相邻兄弟选择器 element.querySelector("+element") 无效
- python - 查找分组列的唯一值?
- excel - 需要帮助简化或改进 excel 中的加权分布公式(数学/excel/编程菜鸟)
- c# - 在 C# Winform 中通过动作创建后调用对象
- html - 如何使文本在按钮中间居中?
- reactjs - React-Redux 中的减速器
- c# - asp.net 中的服务器端自定义验证器,当折扣适用下拉菜单为“是”时,文本框应该是强制性的,否则文本框不是强制性的
- java - 有什么方法可以获取 ENC 图层的所有功能并在 java 或 .net ArcGIS SDK 中切换它们的可见性?
- javascript - 在位掩码中打包和解包整数
- c++ - 如何提高BST的搜索操作效率?