查看原文
其他

基于R语言的shiny网页工具开发基础系列-01

Editor's Note

跟着大佬学shiny😄

The following article is from 生信技能树 Author 生信技能树

shiny是一个直接用R来制作交互式网页应用 (interactive web applications (apps)) 的R包

一、欢迎使用shiny

如下就是一个简单朴素的shiny app界面

只需要两行代码就能画出来,记得安装和载入shiny包哦

# install.packages("shiny")
library(shiny)
runExample("01_hello")

 

可以拖动按钮改变柱子的数量

执行 runExample()可以看到内置的11个例子,github上有更多,可以体验和学习这些例子

rstudio/shiny-examples

runExample()
# Valid examples are "01_hello", "02_text", "03_reactivity", 
# "04_mpg", "05_sliders", "06_tabsets", "07_widgets", "08_html", "09_upload", 
# "10_download", "11_timer"

1.Shiny app的结构

一个shiny app分三个部分

  • 一个用户界面对象

    用户界面(ui)对象控制app的输出和外观

  • 一个server函数

    server 函数包含让计算机构建app的指令

  • 调用shinyApp函数

    最后,shinyApp 函数根据上面的那对uiserver创建一个shinyApp 项目

library(shiny)
# 一个用户界面对象
ui <- fluidPage()
# 一个服务函数
server <- function(input, output) {}
# 调用shinyApp函数
shinyApp(ui = ui, server = server)

可以看看上面 01_hello 的脚本内容

ui 部分

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

    )
  )
)

server 函数部分

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

    })

}

这个脚本相对简单且有详细的注释,做一些计算然后画成对应柱子数量的直方图

其中renderPlot 包含了大部分脚本,之后会详细解释,目前看不懂也没关系

试试把上面两块拼进一个shiny app的app.R脚本,记住要以library(shiny) 开头,调用shinyApp()结尾

library(shiny)

# See above for the definitions of ui and server
ui <- ...

server <- ...

shinyApp(ui = ui, server = server)

2.启动一个app

每个app的结构都一样,一个 app.R 文件包含 uiserver. 通过新建一个文件夹📁(例如(my_app)),并把app.R存入这个文件夹(通常一个app用一个专属的文件夹),然后把文件夹的名字作为参数给到runApp

library(shiny)
runApp("my_app")

runApp和其他R中的许多函数(read.csv, read.table)一样,第一个参数的是从工作目录到app的目录的文件路径,以上代码假设my_app文件夹包含在你的工作路径,因此,只需要输入文件夹的名字

当shiny app启动后,R的会话框会变成忙碌状态,不能运行任何其他代码,R会监控app并处理app的反应。会话框的右上角的红色stop可以停止进程


3. 改改代码练习一下

在工作目录创建一个新的文件夹App-1,用上面的代码创建一个app.R,看起来像这样

 
# 输入命令启动一下看看
runApp("App-1")

终止app并尝试做如下更改

1.把标题从 “Hello Shiny!” 改成 “Hello World!”.

2.设置滑块的最小值为5

3.把直方图的边框颜色从白色改成黄色

改好了看看能否出现下面的结果吧,我成功了哦

 

默认情况下,app以"normal"模式展示,就像上图一样。

内建的例子以"showcase mode"模式展示,会将app.R展示在app界面的一旁

你也可以用这种模式,通过这个命令,runApp("App-1", display.mode = "showcase")

结果是这样的

在Rstudio里面,会自动识别Shiny app 脚本,显示一个Run App 的按钮,如图,快捷键是Command+Shift+Enter (Windows: Control+Shift+Enter)

 

点击灰色的向下展开小箭头还有更多惊喜哦

一个个点点看

 

5.回顾一下怎么创建一个app

  1. 创建一个以myapp为名的路径(文件夹)
  2. 保存app.R到myapp文件夹中
  3. 使用runApp函数或者Rstudio的按钮启动app
  4. 通过点击叉叉或者stop退出app

下一节将开始了解app的每一个部分

Reference:

Shiny - Welcome to Shiny


既往专辑



您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存