前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >shiny入门课【1.简介】

shiny入门课【1.简介】

作者头像
用户2936342
发布2019-06-16 13:06:04
8240
发布2019-06-16 13:06:04
举报
文章被收录于专栏:nummynummy

shiny是一个R包,用于创建交互式web应用,首先需要安装这个包。

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

shiny包内置了11个已经写好的应用,我们可以使用runExample命令来运行。

代码语言:javascript
复制
library(shiny)
runExample("01_hello")

Shiny应用的结构

shiny应用一般单独存放在app.R代码文件中,如果app.R 放置于newdir/目录下面,那应用就可以用runExample("newdir")来运行。

app.R包括以下三个部分:

  • ui 对象
  • server函数
  • 对shinyApp函数的调用 ui对象控制了页面布局以及应用的外观,server用于控制交互,shinyApp函数用于创建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")

    )
  )
)

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

    })

}

整体结构

代码语言:javascript
复制
library(shiny)

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

server <- ...

shinyApp(ui = ui, server = server)

运行应用

将目录名传递给runApp即可。

代码语言:javascript
复制
library(shiny)
runApp("my_app")

Shiny内置应用

代码语言:javascript
复制
runExample("01_hello")      # a histogram
runExample("02_text")       # tables and data frames
runExample("03_reactivity") # a reactive expression
runExample("04_mpg")        # global variables
runExample("05_sliders")    # slider bars
runExample("06_tabsets")    # tabbed panels
runExample("07_widgets")    # help text and submit buttons
runExample("08_html")       # Shiny app built from HTML
runExample("09_upload")     # file upload wizard
runExample("10_download")   # file download wizard
runExample("11_timer")      # an automated timer
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.06.15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Shiny应用的结构
  • 运行应用
  • Shiny内置应用
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档