前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >shiny学习(一)

shiny学习(一)

作者头像
生信编程日常
发布2020-05-18 15:46:57
7880
发布2020-05-18 15:46:57
举报

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

首先是安装Shiny软件包

代码语言:javascript
复制
install.packages("shiny")

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

代码语言:javascript
复制
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对象)
代码语言:javascript
复制
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功能)
代码语言:javascript
复制
# 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结合
代码语言:javascript
复制
shinyApp(ui = ui, server = server)
Shiny App的保存

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

代码语言:javascript
复制
runApp("/test/App/testApp.R")

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

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Shiny apps的构成
    • app.R 包括三部分:
    • 1.ui:(Hello Shiny示例的ui对象)
    • 2.server:(Hello Shiny示例的server功能)
    • 3.shinyApp:最后运行时用shinyApp将ui和server结合
    • Shiny App的保存
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档