「R shiny練習(xí)」一個(gè)在線火山圖的Shiny應(yīng)用

如果只學(xué)習(xí)而不練習(xí),那么你就會(huì)一種自己好像會(huì)了的錯(cuò)覺。為了打破這種錯(cuò)覺,你就需要實(shí)際的去練習(xí),在實(shí)踐中應(yīng)用你學(xué)習(xí)的知識(shí)。

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

應(yīng)用能夠接受用戶上傳的差異表達(dá)分析結(jié)果,然后返回一個(gè)好看的火山圖,后續(xù)用戶可以提供幾個(gè)基因使其在火山圖上進(jìn)行高亮顯示。

第一步: 創(chuàng)建項(xiàng)目

在RStudio中創(chuàng)建Shiny應(yīng)用的項(xiàng)目,具體操作見GIF動(dòng)圖

創(chuàng)建項(xiàng)目

構(gòu)建網(wǎng)頁(yè)布局

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

布局

根據(jù)布局開始寫代碼,代碼如下

# 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")
)

左邊是參數(shù)選擇,右邊用導(dǎo)航欄用于查看導(dǎo)入的數(shù)據(jù)以及最終的結(jié)果展示

布局需要的插件在http://shiny.rstudio.com/reference/shiny/1.2.0/查詢對(duì)應(yīng)的函數(shù)

效果圖

交互輸出

確定基本布局之后,就可以編寫數(shù)據(jù)處理代碼。

先以簡(jiǎn)單的數(shù)據(jù)展示為例,用戶上傳數(shù)據(jù)后,可以在data的部分查看自己上傳的數(shù)據(jù), 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控件在用戶成功上傳文件后,相應(yīng)的input變量就被設(shè)置成數(shù)據(jù)框(這里也就是input$csvFile, 此處復(fù)制給inFIle), 每個(gè)數(shù)據(jù)框包含如下列

  • name: 文件名(并非文件路徑)
  • size: 文件大小
  • type: 瀏覽器返回的MIME格式報(bào)告
  • datapath,表示文件的存放路徑

根據(jù)datapath就能夠用read.csv進(jìn)行文件讀取。效果如下

結(jié)果展示

這里的讀取數(shù)據(jù)代碼放在renderDataTable中,在繪圖的時(shí)候就還需要讀取一次,因此為了避免不必要的操作,我們可以利用reactive函數(shù).

最后的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是外部腳本加載的函數(shù),用于繪制火山圖。

最終效果如下圖

效果圖

部署

參考這一篇「R shiny基礎(chǔ)」使用shinyapp分享你的Shiny應(yīng)用 對(duì)我的應(yīng)用進(jìn)行部署。

部署

問題

由于這是練手的作品,完成度其實(shí)很低,比如說(shuō)對(duì)方要想下載PDF結(jié)果,或者PNG結(jié)果,這里并沒有提供對(duì)應(yīng)的選項(xiàng),你只能右擊保存為PNG圖片。

另外就是各種報(bào)錯(cuò)信息都不是特別的完善,用戶出錯(cuò)了不知道如何解決。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容