2020-04-02 Rshiny 案例分享

  • 1 简介:
  • 1.1 定义
  • 1.2 优缺点
  • 1.3 架构
  • 2 ui对象包含的主要内容
    • 2.1 布局
    • 2.2 控件
    • 2.3 图片
    • 2.4 构建HTML元素
  • 3 交互式输出
    • 3.1 定义
    • 3.2 步骤
    • 3.3 ui与server的对应关系
  • 4 代码执行
  • 5 分享应用
  • 6 案列介绍
    • 6.1 案例功能
    • 6.2 案例代码分享
    • 6.3 案例缺点
  • 7 参考资料

1 简介:

1.1 定义

  • shiny 构建交互式网页 的R包,

1.2 优缺点

  • 优势
    • 不需要css、js等前端知识完成简单的交互式数据可视化
    • 一个人可以快速完成数据收集、处理、分析及数据可视化几项工作
  • 缺点
    • 展示内容有限,输出内容需要在shiny中存在相应函数
    • 数据量大或者图形复杂时,计算速度慢

1.3 架构

  • 需要一个app.R脚本,这个脚本需要三个部分:
    • 一个用户交互对象:负责网页布局,部件设置等,可称为 ui对象
    • 一个服务端对象:服务器端处理数据,可称为server对象,与普通R脚本类似,区别是:嵌套在render*函数中
    • 一个shinyApp函数

2 ui对象包含的主要内容

2.1 布局

  • 定义:网页中,不同内容摆放的位置。

  • 常用布局:
    pageWithSidebar:边栏布局
    sidebarPanel :边栏
    mainPanel:主体部分
    [图片上传失败...(image-2e74b6-1586837171656)]

    image.png

2.2 控件

  • 定义:用户能够与其互动的网页元素,shiny中自带控件:
    [图片上传失败...(image-4a10cd-1586837171656)]

    image.png

各种控件函数:
[图片上传失败...(image-50b26c-1586837171658)]

image.png
  • 插入控件两个重要参数:

    • 部件名称:ui与server是通过部件名称联系的
    • 部件标签

    2.3 图片

    • 图片位置:该脚本的www目录下
    • 函数:img eg: img(src="logo.png",height=100,width=250)

    2.4 构建HTML元素

[图片上传失败...(image-7c4b4b-1586837171658)]

image.png

3 交互式输出

3.1 定义

  • 根据用户要求输出特定内容

3.2 步骤

  • 在ui对象中使用函数创建输出类型

  • 在server对象中设置如何根据输入生成输出

  • 第一步: 在ui对象中增加输出内容选项
    shiny提供了一组函数,能够在ui中返回R对象。不同函数创建不同类型的输出,
    [图片上传失败...(image-bc33dc-1586837171654)]

    image.png
  • 在server中编写输出内容
    先前编写的ui只是确定了网页中哪个部分展示什么类型的输出信息,具体如何展现输出则是需要一类render函数。shiny可用render类函数如下:
    [图片上传失败...(image-d0b869-1586837171654)] *

    image.png

3.3 ui与server的对应关系

[图片上传失败...(image-336414-1586837174233)]

image.png

4 代码执行

  • 启动应用时,Shiny会运行一次所有的代码
  • 每有一个新的用户运行你的shiny应用,就会运行一次server函数,保证每个用户有不同的响应式对象(reactivate object)
  • 每次用户进行交互时,render部分的代码都会运行一次

代码优化:reactive函数,可将一部分在render*中代码移动至reactive中,提高运行速度。它只会在原始的控件(widgets)发生变化之后才会更新结果,
[图片上传失败...(image-27f294-1586837174233)]

image.png

5 分享应用

两种方法:

  • 提供源代码
  • 网页工具:将应用作为网页分享
    1 shiny服务器
    2 将shiny部署至shiny云服务中,eg:shinyapps.io

6 案列介绍

案例:
https://panyuzhang.shinyapps.io/heatmap-v2/

6.1 案例功能

  • 上传文件,依据用户需求显示文件、画热图。
  • 解决常规分析中常见的热图售后问题:
    • 用户更改差异条件:foldchange pvalue
    • 用户挑选基因
    • 用户挑选样本
    • 用户挑选热图选项,eg:聚类,gene_name or transcript_id

6.2 案例代码分享

### 功能:上传规定格式表达量表格,根据fc,pvalue,gene_name等画图
options(encoding = "UTF-8")
rm(list = ls())
library(pheatmap)
library(shiny)
library(dplyr)

ui<-pageWithSidebar(
  headerPanel("heatmap"),
  sidebarPanel(
    ##上传数据
    h3("data_upload"),
    fileInput('file1', 'Choose File',
              accept=c('text/csv')),
    tags$hr(),#加入水平线,HTML的一些标签语法可以用tags来调用
    ##表格
    h3("table"),
    checkboxInput('header', 'Header', TRUE),
    numericInput("obs","Number for obervations to view of the table:",10),
    downloadButton('downloadData', 'Download'),
    br(),
    br(),
    #热图
    h3("heatmap"),
    selectInput("name","name",c("transcript_id","gene_id","gene_name"),selected = "transcript_id"),
    sliderInput("pvalue","pvalue",min = 0,max = 1,value = 0.05),
    sliderInput("FC","log2(fc)",min = 0,max = 5,value = 1),
    selectInput("cluster","cluster",c("yes","no"),selected = "yes"),
    selectInput("exp_numbers","Exp",c("yes","no"),selected = "yes"),
    sliderInput("fontisize","size",min=1,max=100,value = 8),
    textInput("sample","sample",value="Enter sample,sep/,/"),
    #插入图片
    img(src="logo.png",height=100,width=250)
  ),
  mainPanel(
    tabsetPanel(
      tabPanel("Table",tableOutput('contents')),#表格输出
      tabPanel("fig",plotOutput("plot"))#图片输出
    )
  )
)

server<-function(input, output) {
  ##读入上传数据
  datasetInput <- reactive({
    inFile <- input$file1
    if (is.null(inFile))
      return(NULL)
    datax<-read.table(inFile$datapath, header=T,sep="\t") 
  })
  ##处理上传数据
  my_data <- reactive({
    if (is.null(data()))
      return(NULL)
    name<-switch(input$name,
               "transcript_id"=1,
               "gene_id"=2,
               "gene_name"=3)
    tmp_dim<-dim(datasetInput())
    data<-datasetInput()[,c(name,4:tmp_dim[2])]
    data<-subset(data,data$pvalue < input$pvalue)
    data<-subset(data,data$FC>input$FC)
  })
  ##输出table
  output$contents <- renderTable({
    if (is.null(my_data()))
      return(NULL)
    data<-my_data()
    head(data,input$obs)
  })
  ##输出热图
  output$plot<-renderPlot({
    if (is.null(my_data()))
      return(NULL)
    clu<-switch(input$cluster,
                "yes"=T,
                "no"=F)
    ex<-switch(input$exp_numbers,
               "yes"=T,
               "no"=F)
    data<-my_data()
    rownames(data)<-data[,1]
    data<-data[,-1]
    if(input$sample=="Enter sample,sep/,/"){
      col<-dim(data)[2]
      data<-data[,-c(col,col-1,col-2)]
      pheatmap(as.matrix(data),scale="row",fontsize=input$fontisize,cluster_rows = clu,display_numbers = ex)
    }else{
      col<-dim(data)[2]
      data<-data[,-c(col,col-1,col-2)]
      dat<-data[,unlist(strsplit(input$sample,split=","))]
      rownames(dat)<-rownames(data)
      pheatmap(as.matrix(dat),fontsize=input$fontisize,cluster_rows = clu,display_numbers = ex)
    }
  })
  ##下载table
  output$downloadData<-downloadHandler(
    filename = function(){paste("table",".csv",sep="")},
    content = function(file){
      write.table(datasetInput(),file)
    }
  )
}
shinyApp(ui,server)

6.3 案例缺点

  • 提前整理好数据,数据需要符合一定格式,对每列内容及列名有要求
  • 样本顺序
  • 问题:两个转录本对应同一个gene_id

7 参考资料

https://www.jianshu.com/p/a86ed8cbd970

https://wenku.baidu.com/view/9aa408ed4bfe04a1b0717fd5360cba1aa8118cbb.html

https://shiny.rstudio.com/tutorial/written-tutorial/lesson2/

http://yanping.me/shiny-tutorial/#hello-shiny

你可能感兴趣的:(2020-04-02 Rshiny 案例分享)