R语言做生信R shiny 开发web交互应用R语言

「R shiny练习」一个在线火山图的Shiny应用

2019-02-03  本文已影响87人  xuzhougeng

如果只学习而不练习,那么你就会一种自己好像会了的错觉。为了打破这种错觉,你就需要实际的去练习,在实践中应用你学习的知识。

先要明确自己的要干什么:

应用能够接受用户上传的差异表达分析结果,然后返回一个好看的火山图,后续用户可以提供几个基因使其在火山图上进行高亮显示。

第一步: 创建项目

在RStudio中创建Shiny应用的项目,具体操作见GIF动图

创建项目

构建网页布局

在PPT上绘制大致的布局,如下所示。

布局

根据布局开始写代码,代码如下

# Define UI for application that draws a histogram
ui <- fillPage(
  theme = "yeti",
  tags$title("online volcano"),
  
  div(
    tags$header(p("在线火山图", style="font-size:40px"),
                p("作者:徐洲更", style="font-size:30px")),
    align = "center", style="color:#ffffff; background-color: #4d728d") 
    ,
  
   # Sidebar with a slider input for number of bins 
  sidebarLayout(
     sidebarPanel(
       
       # uploading file
       div(
       fileInput("csvFile", "Choose defferential analysis result File",
                 accept = c(
                   "text/csv",
                   "text/comma-separated-values,text/plain",
                   ".csv")
       )
       ),
       
       # colname of your geneID, foldchange and pvalue
       fluidRow(
         column(4,
                textInput("geneID", "columna name of your gene ID",
                             value = "geneID")
                
         ),
         column(4,
                textInput("logFC", "columna name of your logFC",
                             value = "logFC")
         ),
         column(4,
                textInput("pvalue", "columna name of your pvalue",
                             value = "pvalue")
         )
         
       ),
       
       # select gene 
       textInput("selectGene", label = "gene name in your geneID column"),
       br(),
       
       numericInput("circlesize", "circle size", 
                    value = 2.0, min = 0.1, max = 5, step = 0.1),
       br(),
       
       # select fold change and pvalue 
       fluidRow(
         column(6,
                numericInput("logFC1", "log2 Fold Change threshod 1",
                             value = 1, min = 0, max = 2, step = 0.1)
                
                ),
         column(6,
                numericInput("logFC2", "log2 Fold Change threshod 2",
                             value = 2, min = 2, max =10, step = 0.2)
                )
         
       ),

       fluidRow(
         column(4,
                numericInput("pvalue1", "p value threshold 1",
                             value = 0.05, min = 0.01, max = 0.1, step = 0.01)
                
                ),
         column(4,
                numericInput("pvalue2", "p vlaue threshold 2",
                             value = 1e-4, min= 1e-5, max = 1e-3, step = 1e-4)
                ),
         column(4,
                numericInput("pvalue3", "p vlaue threshold 3",
                             value = 1e-5, min = 1e-7, max =1e-4, step = 1e-5)
                )
         
       )
       
     ),
      
      # Show a plot of the generated distribution
      mainPanel(
        tabsetPanel(
          tabPanel("volcano output",
                  plotOutput("volcanoImage") 
                   ),
          tabPanel("data table",
                   dataTableOutput("inputdata"))
        )
        
      )
   ),
  tags$footer(p("contact: xuzhougeng@163.com"), align="center")
)

左边是参数选择,右边用导航栏用于查看导入的数据以及最终的结果展示

布局需要的插件在http://shiny.rstudio.com/reference/shiny/1.2.0/查询对应的函数

效果图

交互输出

确定基本布局之后,就可以编写数据处理代码。

先以简单的数据展示为例,用户上传数据后,可以在data的部分查看自己上传的数据, server的代码如下:

# Define server logic required to draw a histogram
server <- function(input, output) {
  
  output$inputdata <- renderDataTable({
    # input$file1 will be NULL initially. After the user selects
    # and uploads a file, it will be a data frame with 'name',
    # 'size', 'type', and 'datapath' columns. The 'datapath'
    # column will contain the local filenames where the data can
    # be found.
    inFile <- input$csvFile
    
    if (is.null(inFile))
      return(NULL)
    
    read.csv(inFile$datapath)
  })
   
}

代码注解: 之前的fileInput控件在用户成功上传文件后,相应的input变量就被设置成数据框(这里也就是input$csvFile, 此处复制给inFIle), 每个数据框包含如下列

根据datapath就能够用read.csv进行文件读取。效果如下

结果展示

这里的读取数据代码放在renderDataTable中,在绘图的时候就还需要读取一次,因此为了避免不必要的操作,我们可以利用reactive函数.

最后的server代码如下

# Define server logic required to draw a histogram
server <- function(input, output) {
  
  inputdf <- reactive({
    inFile <- input$csvFile
    
    if (is.null(inFile))
      return(NULL)
    
    df <- read.csv(inFile$datapath)
    pos <- which(colnames(df) %in% c(input$geneID, input$logFC, input$pvalue))
    colnames(df)[pos] <- c("geneID","log2FoldChange","pvalue")
    df
  })

  
  output$inputdata <- renderDataTable({

    inputdf()
    
  })
  
  output$volcanoImage <- renderPlot({
    if (! is.null(inputdf())){
      volcano_plot(inputdf(),
                   selectgenes = input$selectGene,
                  log2FC1 = input$logFC1,
                  log2FC2 = input$logFC2,
                  pval1 = input$pvalue1,
                  pval2 = input$pvalue2,
                  pval3 = input$pvalue3
                   
                   )
    }
      
   
  })

: volcano_plot是外部脚本加载的函数,用于绘制火山图。

最终效果如下图

效果图

部署

参考这一篇「R shiny基础」使用shinyapp分享你的Shiny应用 对我的应用进行部署。

部署

问题

由于这是练手的作品,完成度其实很低,比如说对方要想下载PDF结果,或者PNG结果,这里并没有提供对应的选项,你只能右击保存为PNG图片。

另外就是各种报错信息都不是特别的完善,用户出错了不知道如何解决。

上一篇 下一篇

猜你喜欢

热点阅读