将selectInput选项从R列表中指定为闪亮



我有问题!我有一个包含文件列表的目录(所有文件的扩展名都相同.hist(。我想为所有这些文件绘制直方图(我可以完成这一部分(,但我希望能够从闪亮的下拉菜单中选择任何直方图,而无需显式地写";选择";。也就是说,我希望选择是动态的,并且基于在…中读取的.hist文件

假设我有5个样本及其相应的.hist文件:

A.hist
B.hist
C.hist
D.hist
E.hist

首先,我加载所有的.hist文件:

library(ggplot2)
library(shiny)
temp = list.files(pattern="*.hist")
for (i in 1:length(temp)) assign(temp[i], read.table(temp[i]))
myfiles = lapply(temp, read.table)

然后我想输出每个.hist文件的直方图:

plot_histogram <- function(x) {

x <- ggplot(x, aes(V2, V5)) + geom_col() + xlim(0,500) + ylim(0,0.01)+
ylab("Proportion") + xlab("Read Depth")

}
lapply(myfiles, plot_histogram)

但是,我想要一个闪亮的应用程序,我可以从我的所有样本(a、B、C、D或E(中进行选择,它会生成绘图。我不想明确声明,A, B, C, D, E作为选项,因为.hist文件的数量可能会更改。该应用程序每次批处理运行都会下载.hist文件,所以我需要它来遍历目录中的文件。

这是我的。。。

ui <- fluidPage(
titlePanel("Histogram QC"),
sidebarLayout(
sidebarPanel(
##ISSUE IS HERE, I do not want to use choices = c("A.hist", "B.hist" etc)
selectInput("histo", "Select sample",
choices = c()
),
mainPanel(
plotOutput("histograms")
)
))
)
server <- function(input, output) {
output$histograms <- renderPlot({ 
ggplot(input$histo, aes(V2, V5)) + geom_col() + xlim(0,500) + ylim(0,0.01)+
ylab("Proportion") + xlab("Read Depth") 
})
}
shinyApp(ui = ui, server = server)

我希望selectInput接受myfiles的所有选项。。。在没有明确命名的情况下等同于(A.hist、B.hist等(。这可能吗?!

我有一个解决方案。。。

library(ggplot2)
library(shiny)
#load in coverage.hist files
coverage_hist = list.files(pattern="*_coverage.hist")
#load as separate df objects
for (i in 1:length(coverage_hist)) assign(coverage_hist[i], read.table(coverage_hist[i]))
#load as list of dfs
hist_files = lapply(coverage_hist, read.table)
#Add names to list
names(hist_files) = coverage_hist
ui <- fluidPage(
titlePanel("Histogram QC"),
sidebarLayout(
sidebarPanel(
selectInput("histo", "Select sample",
choices = coverage_hist
)),
mainPanel(
plotOutput("histograms")
)
))
server <- function(input, output) {
output$histograms <- renderPlot({ 
ggplot(get(input$histo), aes(V2, V5)) + geom_col() + xlim(0,500) + ylim(0,0.01)+
ylab("Proportion") + xlab("Read Depth") 
})
#or using list
#output$histograms <- renderPlot({ 
# ggplot(hist_files[[input$histo]], aes(V2, V5)) + geom_col() + xlim(0,500) + ylim(0,0.01)+
# ylab("Proportion") + xlab("Read Depth") 
#  })
}
shinyApp(ui = ui, server = server)

最新更新