将R脚本连接到闪亮以输出图像

时间:2019-02-14 19:25:15

标签: r shiny

我正在构建一个闪亮的应用程序GUI,以允许将四个数据文件“轻松”输入到我的R脚本中。我希望能够上传四个文件,从两组单选按钮中选择选项,然后单击“提交”以运行所选的分析。最终输出(在我的分析代码中生成)应为.PNG文件,该文件将(a)自动下载到工作目录,并(b)显示在GUI的MainPanel上。为了了解我要做什么,我提供了应用程序UI的代码。

library(shiny)
  ui <- fluidPage(
  titlePanel("Pando Nutrition Microbiome Analysis"),
  sidebarLayout(
     sidebarPanel(
      fileInput(inputId="n1","Shared Table Upload", multiple = FALSE),
      fileInput(inputId="n2", "Taxonomy File Upload", multiple=FALSE),
      fileInput(inputId="n3", "Meta Table Upload", multiple=FALSE),
      fileInput(inputId='ZHtree', "Phylogenetic Tree"),
      radioButtons(inputId="Selection1", "Select One",
               c("Fungi" = "ITS",
                 "Bacteria" = "16S")),
  radioButtons(inputId="Selection2", "Select Analysis",
               c("Shannon Diversity (Timecourse)" = "shannon1",
                 "Shannon Diversity (CTL vs EXP)"="shannon2",
                 "Top 20 Genera"="Top20Genera",
                 "Top 10 Genera"="Top10Genera",
                 "Top 5 Genera"="Top5Genera",
                 "Top 2 Genera"="Top2Genera"
                 )),
  actionButton(inputId="Submit", "Submit"),
  hr()
),
mainPanel(
  imageOutput("figure_1.png")
)
  )
)
server <- function(input, output) {
}
shinyApp(ui = ui, server = server)

这是在R Studio中生成16s“ shannon1”图的R代码:

n1<-readline(prompt="Enter .Shared File Name:")
n2<-readline(prompt="Enter .Taxonomy File Name:")
n3<-readline(prompt="Enter Meta Table File Name:")

source("https://bioconductor.org/biocLite.R")
biocLite("phyloseq")
biocLite("DESeq2")
library(ape)
library(DESeq2)
library(dplyr)
library(ggplot2)
library(gplots)
library(lme4)
library(phangorn)
library(phyloseq)
library(plotly)
library(tidyr)
library(vegan)
library(VennDiagram)
library(shiny)


OTU = read.table(n1, header=TRUE, sep="\t")
tax = read.table(n2, header=TRUE, sep="\t")
meta=read.table(n3, header=TRUE, row.names=1, sep="\t")

row.names(OTU) = OTU$Group
OTU.clean = OTU[,-which(names(OTU) %in% c("label", "numOtus", "Group"))]
row.names(tax) = tax$OTU
tax.clean = tax[row.names(tax) %in% colnames(OTU.clean),]
tax.clean = separate(tax.clean, Taxonomy, into = c("Domain", "Phylum", 
"Class", "Order", "Family", "Genus", "Species", "Strain"), sep=";")
tax.clean = tax.clean[,-which(names(tax.clean) %in% c("Size", "Strain", "OTU"))]
OTU.clean = OTU.clean[order(row.names(OTU.clean)),]
meta = meta[order(row.names(meta)),]
meta.controls <- meta[which(meta$group=='Control'),]
meta.controls = meta.controls[order(row.names(meta)),]
meta.experimental <- meta[which(meta$group=='Experimental'),]
meta.experimental = meta.experimental[order(row.names(meta)),]
meta.day0 <- meta[which(meta$collected_date=="0"),]
meta.day28 <- meta[which(meta$collected_date=="28"),]
OTU.clean.day0 <- OTU.clean[which(meta$collection_date=="0"),]
OTU.clean.day28 <- OTU.clean[which(meta$collection_date=="28"),]
meta.day0 <- meta[which(meta$collection_date=="0"),]
meta.day28 <- meta[which(meta$collection_date=="28"),]
OTU.clean.relabund <- sweep(OTU.clean,1,rowSums(OTU.clean),"/")
rowSums(OTU.clean)
rowSums(OTU.clean.relabund)
set.seed(8765)
OTU.physeq = otu_table(as.matrix(OTU.clean), taxa_are_rows=FALSE)
tax.physeq = tax_table(as.matrix(tax.clean))
meta.physeq = sample_data(meta)
meta.physeq.control = sample_data(meta.controls)
meta.physeq.experimental= sample_data(meta.experimental)
physeq.alpha = phyloseq(OTU.physeq, tax.physeq, meta.physeq)
physeq.alpha.controls =phyloseq(OTU.physeq, tax.physeq, 
meta.physeq.control)
physeq.alpha.experimental=phyloseq(OTU.physeq, tax.physeq, 
meta.physeq.experimental)
sample_data(physeq.alpha)$shannon.physeq <- 
estimate_richness(physeq.alpha, measures="Shannon")
sample_data(physeq.alpha.controls)$shannon.physeq <- 
estimate_richness(physeq.alpha.controls, measures="Shannon")
sample_data(physeq.alpha.experimental)$shannon.physeq <- 
estimate_richness(physeq.alpha.experimental, measures="Shannon")

title = "Shannon Diversity Over Study Timecourse"
plot_richness(physeq.alpha, measures="Shannon", title = title) #FIGURE 1
dev.copy(png,'figure_1.png')
dev.off()

我可以使UI呈现得很漂亮,但是将代码(例如生成“ shannon1”图形的16s.R脚本)链接到按钮上却很难。我知道服务器功能必须对“提交”功能起反应,但是它还需要依赖于(a)四个文件的输入,(b)选定的16S或ITS单选按钮,(c)选定的分析。

0 个答案:

没有答案