shiny学习(一)

作者: 生信编程日常 | 来源:发表于2020-05-09 22:10 被阅读0次

Shiny是一个R软件包,可很方便的从R直接构建交互式Web应用程序。

首先是安装Shiny软件包

install.packages("shiny")

Shiny有11个内置的演示例子来讲解Shiny的工作流程,如01_hello:

library(shiny)
#直接展示内置的实例
runExample("01_hello")

这个直方图在左侧有一个可以调整bins个数的滑条,当用户滑动选择bins的数目时,图表也随即产生变化,这样实现了一个交互式的过程。

Shiny apps的构成

Shiny apps包含一个R script即app.R,位于某个目录下如(newdir/),app可以通过函数runApp("newdir/app.R")运行。

app.R 包括三部分:

1.a user interface object
2.a server function
3.a call to the shinyApp function
即:1.用户界面(ui)object 控制应用程序的布局和外观。2.server function包含构建应用程序所需的说明。3.最后,shinyApp function通过ui和server创建Shiny应用程序对象。

以Hello Shiny为例

1.ui:(Hello Shiny示例的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")

    )
  )
)

2.server:(Hello Shiny示例的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")

    })

}

3.shinyApp:最后运行时用shinyApp将ui和server结合

shinyApp(ui = ui, server = server)

Shiny App的保存

每个Shiny应用程序都具有相同的结构:app.R包含ui和的文件server。可以通过创建新目录并在其中保存app.R文件来创建Shiny应用程序。
如:将ui,sever,runApp这三部分代码保存test/App目录下的testApp.R里。
为了与之前的代码区分,我改了一下颜色和title,保存后,重新运行

runApp("/test/App/testApp.R")

欢迎关注微信公众号:生信编程日常


公众号二维码

参考:
https://shiny.rstudio.com/gallery/

相关文章

  • Shiny学习(二)

    前面介绍了Shiny的基本构成Shiny学习(一)下面接着学习如何构建用户界面。 首先,创建Shiny应用程序所需...

  • shiny学习(一)

    Shiny是一个R软件包,可很方便的从R直接构建交互式Web应用程序。 首先是安装Shiny软件包 Shiny有1...

  • LearningR-shiny

    1. shiny 1.1 About shiny 1.2 shiny examples 2. rsconnect ...

  • 「R shiny 基础」初识Shiny

    传送门 Shiny基础教程: 「R shiny 基础」初识Shiny 「R shiny 基础」如何进行网页布局 「...

  • IRscope代码拆解一

    IRScope 是用用来可视化 叶绿体基因组边界扩张收缩的一个shiny应用。正好自己最近在学习R语言里shiny...

  • Shiny 学习资源

    一本简单的网络书:https://laderast.github.io/gradual_shiny/where-n...

  • Shiny 教程1

    简介shiny 什么是shiny: Shiny 是一个开源的 R 包,它为使用 R 构建 Web 应用提供了一个优...

  • 【大数据部落】R语言用Shiny生态快速搭建交互网页应用

    原文链接:http://tecdat.cn/用shiny生态快速搭建交互网页应用/ 什么是Shiny? Shiny...

  • Shiny 初步了解

    想了解Shiny app运行原理,可以查看先前的文章一个 Shiny app的基本组成部分 Shiny app框架...

  • shiny的介绍及使用

    shiny cheatsheet:https://shiny.rstudio.com/articles/cheat...

网友评论

    本文标题:shiny学习(一)

    本文链接:https://www.haomeiwen.com/subject/slwqnhtx.html