网页开发,尤为是交互式动态网页的开发,是有必定门槛的,若是你有必定的R基础,又不想过深的接触PHP和MySQL,那R的shiny就是一个不错的选择。html
如今R shiny配合R在统计分析上的优点,能够作出很是优秀的科学网站,但我见过的shiny仍是多用于本地网站搭建,由于不是每一个实验室都能拿出大量的财力来构建公共的网站的,并且很容易形成计算资源的浪费,R shiny则充分利用了我的计算机的优点,只要安装了基本包,就能够运行shiny网站。java
如今个人需求是:react
一个填表网站,属性固定,我须要一行一行的输入。git
还要有查重的功能,已经录入的提示不须要再录入。github
提示我那些尚未录入,我再一个一个的填入(覆盖功能)。express
最后作一个基本的统计图表。macos
过滤查找功能,提取感兴趣的数据。服务器
这确定能够用Excel作,但缺点是:app
1. 不易更新;ide
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网站都包含三个部分: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)
小技巧:
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
高级语法
「R shiny基础」reactive让shiny应用运行速度变快
用内存换速度,原来每有一点变更就从新计算全部结果,如今把部分结果放在内存中,除非指定的发生变化,不然不会从新计算。
isolate隔离
正常状况下,一旦参数发生变化,就必然会更新结果,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分析;