Shiny app的許多功能都是通過一個小的部件進(jìn)行充實(shí)和調(diào)控的实幕。
Basic widgets
Shiny 有一套自定義的小工具拐邪,都是通過R函數(shù)實(shí)現(xiàn)。
例如钩蚊,Shiny提供了一個名為actionButton的函數(shù)來創(chuàng)建一個動作按鈕,以及一個名為sliderInput的函數(shù)來創(chuàng)建一個滑條蹈矮。
#標(biāo)準(zhǔn)的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。
#添加工具
添加工具鸣驱,就是將這些工具對應(yīng)的函數(shù)添加到in sidebarPanel
或mainPanel
泛鸟。添加工具與添加HTML 內(nèi)容類似,直接在ui.R中的面板插入就可以了踊东。
每一個工具函數(shù)的前兩個參數(shù)都是一致的:
- a name for the widget: 用戶看不到這個名稱北滥,開發(fā)者需要使用它來訪問這個小工具的值;一個字符串闸翅。
- a label: 會展示在你的App頁面再芋;可以為空。
舉個例子坚冀,名稱為“action”济赎,標(biāo)簽為“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)
widgets
#總結(jié)
- Shiny提供了一系列的函數(shù)來創(chuàng)建這些小部件。
- 每個函數(shù)都需要一個名稱和一個標(biāo)簽。
- 一些小部件需要特定的指令來完成它們的工作司训。
- 添加小部件和添加HTML 很類似构捡。
為方便尋找和學(xué)習(xí)如何添加需要的小部件到自己的Shiny App, 可以參考Shiny Widgets Gallery, 這個圖庫顯示了每個Shiny的小部件壳猜,并演示了這些小部件如何工作以及如何添加(點(diǎn)擊See code)勾徽。
Shiny Widgets Gallery
點(diǎn)擊See code,即可查看如何向Shiny app添加小部件统扳。
Individual widget
#原文
系列文章:
R shiny教程-1:一個 Shiny app的基本組成部分
R shiny教程-2:布局用戶界面
Shiny Server安裝