r - 使用 R 闪亮的应用程序创建箱线图的问题
问题描述
我对 Shiny 应用程序 R 非常陌生。我正在尝试在 Shiny R 应用程序中为某些数据集制作简单的箱线图。
在这里,我在一个文件中展示了一些示例数据df.csv
。数据如下所示。显示dput
以下数据:
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")
有了上述信息,我正在尝试制作一个闪亮的应用程序。我的代码如下所示:
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("thegene", "Gene", choices = c("A2M", "A1CF", "A2MP1"), selected = "A2M"),
radioButtons("colour","Colour of histogram",choices=c("red","green","blue"),selected="red"),
width = 3
),
mainPanel(
plotOutput("boxplot"),
width = 9
)
)
)
server <- function(input, output) {
df <- read.csv("df.csv")
library(reshape2)
library(ggplot2)
library(ggpubr)
library(EnvStats)
df.m <- melt(df, c("Samples", "Type"))
output$boxplot <- renderPlot({
ggplot(data=df.m, aes(x = Type, y = value, fill=variable)) +
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")) +
stat_compare_means(method = "t.test", size=5) + stat_n_text()
})
}
# Run the application
shinyApp(ui = ui, server = server)
所以,我重新塑造了信息,然后尝试制作一个应用程序来为肿瘤(6 个样本)和正常(4 个样本)之间的每个基因创建一个箱线图。
我没有看到任何错误,但我也没有得到想要的结果。我上面代码的输出如下所示:
1)每个Type下方箱线图中的样本数是错误的。
2)对于基因的选择,我在那里只能看到三个基因。我在那里看不到其他基因。如何检查其他基因?
3)直方图的颜色也不起作用。
任何帮助表示赞赏。谢谢你。
解决方案
尝试这个。
我做了一些更改,您可以保留一些并反转其他一些。
- 我没有
ggpubr
orEnvStats
,所以我删除了一些绘图摘要。 - 我定义了静态数据,您可能应该返回到您的
read.csv
解决方案。 - 如果您想以编程方式更新任何输入,我添加
session
到服务器声明中。 - 我有一个效率低下的反应块,它只返回所有原始数据;就目前而言,这是反惯用的,但添加只是为了演示
updateSelectInput
如果/当源数据发生更改时的正确使用。仅当您的数据动态更改(例如,用户上传数据或数据库查询)时才需要这样做,否则alldat()
实际上应该只是df.m
(并且您的输入应该静态定义)。 - 我更新了颜色单选按钮的使用。
library(shiny)
library(reshape2)
library(ggplot2)
library(ggpubr)
library(EnvStats)
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"))
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("thegene", "Gene", choices = c("A2M", "A1CF", "A2MP1"), selected = "A2M"),
radioButtons("colour","Colour of histogram",choices=c("red","green","blue"),selected="red"),
width = 3
),
mainPanel(
plotOutput("boxplot"),
width = 9
)
)
)
server <- function(input, output, session) {
alldat <- reactive({
# this is not an efficient use of a reactive block: since it does
# not depend on any dynamic data, it will fire only once, so if
# your data is static then this might be a touch overkill ... but
# the premise is that your `df.m` is data that can change based on
# updating it (e.g., DB query) or user-uploaded data (e.g., CSV
# upload)
choices <- unique(df.m$variable)
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$variable == input$thegene,,drop=FALSE]
})
output$boxplot <- renderPlot({
ggplot(data = dat(), aes(x = Type, y = value, fill = variable)) +
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)
})
}
# Run the application
shinyApp(ui = ui, server = server)
一些注释/意见:
- 当由于过滤或用户提供的修饰符而存在动态数据时,我发现有一个仅执行过滤/修改的反应块很好,这样修改后的数据可以在多个相关的反应块中使用,ergo my
dat <- reactive(...)
- 更重要的是,我发现许多不太好的闪亮应用程序试图在单个反应块中做太多事情;当我看到很多事情发生时,我倾向于认为(a)将反应块分成更小的块,尤其是当代码在多个块中重复时;和/或 (b) 编写完成大部分工作的外部函数,以便闪亮的应用程序本身看起来更紧凑。声明性函数名称可以使可读性/可维护性更容易(并且可以进行单元测试!)。
- 我没有为此添加任何保护措施;一种这样的保护措施(尽管此应用程序不会立即显示)将
req()
用于确保输入在启动期间“稳定”。对于较大的应用程序,人们可能会注意到一些反应块在(例如)input$thegene
具有有效值之前触发,这可能会导致一些绘图/表格闪烁。 - 当有一个选择输入会很快被覆盖/更新时,我通常会选择类似
choices="(initializing)"
或类似的东西;在这种情况下,只要这些选择很可能或肯定会出现在真实数据中,就有合理的默认选择是有意义的。
推荐阅读
- google-cast - 确定正在使用哪一代 Chromecast
- c++ - 我如何摆脱这些垃圾号码
- python - 将 Dropping Column 实例添加到管道中
- powershell - Powershell - Add an extra column to a Converto-HTML
- jquery - 单击时如何获取同一div中另一个元素的值?
- css - 为什么它对于foreginObject 仅适用于 ios 的剪辑路径
- excel - xlFormulas2 在专业加 2016 版中为空导致下标超出范围异常
- vue.js - 通过 v-on:change 获取输入值并点击 vuejs?
- python - 如何根据 Python 中的另一个数据框列填充列?
- google-apps-script - 发布 Google 附加组件的冲突答案