R shiny教程-3:添加小部件到Shiny App
2020-10-01 本文已影响0人
JeremyL
Shiny app的许多功能都是通过一个小的部件进行充实和调控的。

Shiny 有一套自定义的小工具,都是通过R函数实现。
例如,Shiny提供了一个名为actionButton的函数来创建一个动作按钮,以及一个名为sliderInput的函数来创建一个滑条。
#标准的Shiny小部件
function | widget |
---|---|
actionButton |
Action Button |
checkboxGroupInput |
A group of check boxes |
checkboxInput |
A single check box |
dateInput |
A calendar to aid date selection |
dateRangeInput |
A pair of calendars for selecting a date range |
fileInput |
A file upload control wizard |
helpText |
Help text that can be added to an input form |
numericInput |
A field to enter numbers |
radioButtons |
A set of radio buttons |
selectInput |
A box with choices to select from |
sliderInput |
A slider bar |
submitButton |
A submit button |
textInput |
A field to enter text |
更多信息可以参看 Twitter Bootstrap。
#添加工具
添加工具,就是将这些工具对应的函数添加到in sidebarPanel
或mainPanel
。添加工具与添加HTML 内容类似,直接在ui.R中的面板插入就可以了。
每一个工具函数的前两个参数都是一致的:
- a name for the widget: 用户看不到这个名称,开发者需要使用它来访问这个小工具的值;一个字符串。
- a label: 会展示在你的App页面;可以为空。
举个例子,名称为“action”,标签为“action”:actionButton("action", label = "Action")
library(shiny)
# Define UI ----
ui <- fluidPage(
titlePanel("Basic widgets"),
fluidRow(
column(3,
h3("Buttons"),
actionButton("action", "Action"),
br(),
br(),
submitButton("Submit")),
column(3,
h3("Single checkbox"),
checkboxInput("checkbox", "Choice A", value = TRUE)),
column(3,
checkboxGroupInput("checkGroup",
h3("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2,
"Choice 3" = 3),
selected = 1)),
column(3,
dateInput("date",
h3("Date input"),
value = "2014-01-01"))
),
fluidRow(
column(3,
dateRangeInput("dates", h3("Date range"))),
column(3,
fileInput("file", h3("File input"))),
column(3,
h3("Help text"),
helpText("Note: help text isn't a true widget,",
"but it provides an easy way to add text to",
"accompany other widgets.")),
column(3,
numericInput("num",
h3("Numeric input"),
value = 1))
),
fluidRow(
column(3,
radioButtons("radio", h3("Radio buttons"),
choices = list("Choice 1" = 1, "Choice 2" = 2,
"Choice 3" = 3),selected = 1)),
column(3,
selectInput("select", h3("Select box"),
choices = list("Choice 1" = 1, "Choice 2" = 2,
"Choice 3" = 3), selected = 1)),
column(3,
sliderInput("slider1", h3("Sliders"),
min = 0, max = 100, value = 50),
sliderInput("slider2", "",
min = 0, max = 100, value = c(25, 75))
),
column(3,
textInput("text", h3("Text input"),
value = "Enter text..."))
)
)
# Define server logic ----
server <- function(input, output) {
}
# Run the app ----
shinyApp(ui = ui, server = server)

#总结
- Shiny提供了一系列的函数来创建这些小部件。
- 每个函数都需要一个名称和一个标签。
- 一些小部件需要特定的指令来完成它们的工作。
- 添加小部件和添加HTML 很类似。
为方便寻找和学习如何添加需要的小部件到自己的Shiny App, 可以参考Shiny Widgets Gallery, 这个图库显示了每个Shiny的小部件,并演示了这些小部件如何工作以及如何添加(点击See code)。

点击See code,即可查看如何向Shiny app添加小部件。

#原文
系列文章:
R shiny教程-1:一个 Shiny app的基本组成部分
R shiny教程-2:布局用户界面
Shiny Server安装