zoukankan      html  css  js  c++  java
  • R Shiny app | 交互式网页开发

    网页开发,尤其是交互式动态网页的开发,是有一定门槛的,如果你有一定的R基础,又不想过深的接触PHP和MySQL,那R的shiny就是一个不错的选择。

    现在R shiny配合R在统计分析上的优势,可以做出非常优秀的科学网站,但我见过的shiny还是多用于本地网站搭建,因为不是每个实验室都能拿出大量的财力来构建公共的网站的,而且很容易造成计算资源的浪费,R shiny则充分利用了个人计算机的优势,只要安装了基本包,就可以运行shiny网站。

    现在我的需求是:

    一个填表网站,属性固定,我需要一行一行的输入。

    还要有查重的功能,已经录入的提示不需要再录入。

    提示我那些还没有录入,我再一个一个的填入(覆盖功能)。

    最后做一个基本的统计图表。

    过滤查找功能,提取感兴趣的数据。

    这肯定可以用Excel做,但缺点是:

    1. 不易更新;

    2. 不易去重;

    3. 不易过滤;

    4. 不易发表;

    4. 时间越久表格越混乱;

    参考文章:

    Learn Shiny - 官网教程

    Mimicking a Google Form with a Shiny app

    我的 Shiny 入门学习笔记 - 生信菜鸟团

    网站结构设计:

    1. 输入一个需要定期更新的table1,里面包含我们现在所有的信息,把“run”作为table的key;

    2. 初始化一个输出table2,我们填写的值需要不断地填入,留一个overwrite的功能;

    3. 输出一个提示table3,哪些数据我们还没有整理,与table2同步更新;

    4. 部署到网上,www.shinyapps.io

    注:

    显然table3要放在主页,因为我要用它来填表,输入放在底下;

    table2放在分页,底下再显示基本的统计信息,基本的柱状图就行;

    Shiny的建站逻辑

    任何一个shiny网站都包含三个部分:ui, server, shinyApp.

    library(shiny)
    
    # See above for the definitions of ui and server
    ui <- ...
    
    server <- ...
    
    shinyApp(ui = ui, server = server)
    

    The user interface (ui) object controls the layout and appearance of your app.

    The server function contains the instructions that your computer needs to build your app.

    Finally the shinyApp function creates Shiny app objects from an explicit UI/server pair.  

    交互式的网页逻辑很清楚:

    在网页上,我需要输入数据,其实我们输入的是一个个的键值对;

    当我们填好后,请求就会到达服务器,服务器会根据输入的键值对来做各种处理,最终会返回数据和图表;

    ui里就包含了输入的键值对信息,以及服务器的输出过来了如何显示在网页上;

    server就无所谓了,因为ui已经把输入输出固定死了,你可以做任何花式处理,但你必须返回指定的输出才能在网页上显示;

    shinyApp就是把两者连接在一起;

    一个简单的案例:

    ui里定义的输入就是bins;输出就是disPlot;

    server里就是根据现有的数据集和bins,画了个hist;

    # setwd("/Users/zxli/Dropbox/PureZ/project/singleCell/tea/shiny")
    
    library(shiny)
    
    # Define UI for app that draws a histogram ----
    ui <- fluidPage(
    
      # App title ----
      titlePanel("Hello Shiny!"),
    
      # Sidebar layout with input and output definitions ----
      sidebarLayout(
    
        # Sidebar panel for inputs ----
        sidebarPanel(
    
          # Input: Slider for the number of bins ----
          sliderInput(inputId = "bins",
                      label = "Number of bins:",
                      min = 1,
                      max = 50,
                      value = 30)
    
        ),
    
        # Main panel for displaying outputs ----
        mainPanel(
    
          # Output: Histogram ----
          plotOutput(outputId = "distPlot")
    
        )
      )
    )
    
    # Define server logic required to draw a histogram ----
    server <- function(input, output) {
    
      # Histogram of the Old Faithful Geyser Data ----
      # with requested number of bins
      # This expression that generates a histogram is wrapped in a call
      # to renderPlot to indicate that:
      #
      # 1. It is "reactive" and therefore should be automatically
      #    re-executed when inputs (input$bins) change
      # 2. Its output type is a plot
      output$distPlot <- renderPlot({
    
        x    <- faithful$waiting
        bins <- seq(min(x), max(x), length.out = input$bins + 1)
    
        hist(x, breaks = bins, col = "#75AADB", border = "white",
             xlab = "Waiting time to next eruption (in mins)",
             main = "Histogram of waiting times")
    
      })
    
    }
    
    shinyApp(ui = ui, server = server)
    

      

    深入ui

    小技巧:

    1. 如何输入文件?

    fileInput

    2. 如何显示数据?

    默认的会把整个table都显示,显然DT的格式更美观,还可以排序过滤。

    DT::dataTableOutput
    DT::renderDataTable

    3. 如何改变布局?

    sidebarLayout很实用,但可以改变;常见布局并不多,参见官网:Application layout guide

    4. 如何提交数据?

    textInput

    5. 有哪些好看的主题?

    shinydashboard这个布局比较高大上。

    library(devtools)
    install_github("nik01010/dashboardthemes")
    

    6. 如何解决rJave问题?

    先安装java8,然后sudo R CMD javareconf。参考:rJava fails to load #2254  

    macos去/Library/Java/JavaVirtualMachines/里面把之前的版本给卸载了。

    stdio.h的问题就得去/Library/Developer/CommandLineTools/Packages/把包再装一遍。

    怎么搞都搞不好,最后发现默认的rJava是依赖jdk11.0.1的,装了对应版本,世界瞬间清净了。

    7. 如何下载table?

    downloadHandler

    深入server

    高级语法

    Reactivity - An overview

    reactive重新激活

    「R shiny基础」reactive让shiny应用运行速度变快

    用内存换速度,原来每有一点变动就重新计算所有结果,现在把部分结果放在内存中,除非指定的发生变化,否则不会重新计算。

    isolate隔离

    正常情况下,一旦参数发生变化,就必然会更新结果,isolate则只在激活按钮时才生效。

    Stop reactions with isolate()

    Use isolate() to avoid dependency on input$obs

    observe

    类似reactive,但是没有输出值。

    renderUI


    第二个R shiny的网站app

    主要功能:

    1. 用词云展示核心的信息,table呈现meta信息;

    2. 用户搜寻自己感兴趣的样本,导出样本信息;

    3. 用户查找感兴趣的基因,导出感兴趣的基因在特定组织、处理中的表达;

    4. 精确查找meta信息,整合做meta RNA-seq分析,DEG获取;

    5. 常规pipeline分析;

  • 相关阅读:
    手把手教你使用UICollectionView写公司的项目
    深入研究Block捕获外部变量和__block实现原理
    聊聊 iOS 开发中的协议
    如何快速的开发一个完整的iOS直播app(原理篇)
    萌货猫头鹰登录界面动画iOS实现分析
    浅谈 block(1) – clang 改写后的 block 结构
    iOS 开发中你是否遇到这些经验问题(二)
    iOS 开发中你是否遇到这些经验问题(一)
    iOS 本地自动打包工具
    Storyboards vs NIB vs Code 大辩论
  • 原文地址:https://www.cnblogs.com/leezx/p/10922712.html
Copyright © 2011-2022 走看看