shiny 小部件提供了一個用戶給app傳送信息的方式
為什麼加上控制小工具上節已經學會在用戶界面放置一些簡單的元素,但顯示更複雜的內容需要用到小部件widgets
widgets 是可交互網頁元素,讓用戶用它們控制app
Shiny 小部件widgets 從用戶手裡收集值,當用戶改變小工具的時候,值也會變
控制小部件如圖有各種小部件,shiny有一系列的小部件,每個都可以用直白命名的R函數創建,例如函數actionButton 用來創建 動作按鈕 (Action Button),函數 sliderInput 創建 一個 滑塊 (slider bar)
下表是常見的小部件
其中的一些部件是用Twitter Bootstrap項目構建的,一個受歡迎的構建用戶界面的開源框架
加上小部件你可以像添加其他元素一樣添加widgets
放置一個widget 函數 在ui對象的sidebarPanel 或 mainPanel 函數中
每個widget 函數都要幾個參數,每個widget的前兩個參數一定是:
widget的名字name:用戶不會看到這個名字,但是可以用它獲取widget的值,應該用字符串一個label:這個標籤將和app中的小部件一起出現,應該是字符串,但是也可以是空的""此例中,name參數是"action", 標籤是 "Action"
actionButton("action", label = "Action")其他參數因小部件而異,具體要看小部件執行的工作所需的內容
他們包括初始值,範圍和增量
也可以通過查看函數的幫助頁面來獲取其他參數,如?selectInput
試試把下面的代碼運行一下吧
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)結果如下
可以每個都感受一下是做什麼的,或者改改代碼的值,看看有什麼變化
此app的布局方案可以參考application layout guide. 當然此篇只要了解他們的作用就行了,不必了解這種複雜的布局方案
練習嘗試寫個如圖所示的界面(答案很長放在後面,但是一定一定要先自己做一遍哦)
小節回顧每個小工具的函數都需要name和label這兩個參數更進一步Shiny Widgets Gallery 提供模版,供你快速加入到自己的app中
訪問這個網站,圖庫中展示了每個小部件,並演示了每個小部件的值根據你的輸入而變化
選擇一個小工具,並點擊See Code。圖庫會跳轉到一個描述這個小工具的示例app,只需要複製其中代碼到自己的app中即可。
我又做出來了哦,這是我的結果
我的代碼
library(shiny)
# Define UI ----
ui <- fluidPage(
titlePanel("censusVis"),
sidebarLayout(
sidebarPanel(
p("Create demographic maps with information from the 2010 US Census."),
br(),
selectInput("select", h3("Choose a variable to display"),
choices = list("Percent White" = 1, "Percent Black" = 2,
"Percent Hispanic" = 3, "Percent Asian" = 4), selected = 1),
sliderInput("slider", h3("Range of interest:"),
min = 0, max = 100, value = c(0,100))
),
mainPanel("")
)
)
# Define server logic ----
server <- function(input, output) {
}
# Run the app ----
shinyApp(ui = ui, server = server)參考答案
library(shiny)
# Define UI ----
ui <- fluidPage(
titlePanel("My Shiny App"),
sidebarLayout(
sidebarPanel(
h2("Installation"),
p("Shiny is available on CRAN, so you can install it in the usual way from your R console:"),
code('install.packages("shiny")'),
br(),
br(),
br(),
br(),
img(src = "rstudio.png", height = 70, width = 200),
br(),
"Shiny is a product of ",
span("RStudio", style = "color:blue")
),
mainPanel(
h1("Introducing Shiny"),
p("Shiny is a new package from RStudio that makes it ",
em("incredibly easy "),
"to build interactive web applications with R."),
br(),
p("For an introduction and live examples, visit the ",
a("Shiny homepage.",
href = "http://shiny.rstudio.com")),
br(),
h2("Features"),
p("- Build useful web applications with only a few lines of code—no JavaScript required."),
p("- Shiny applications are automatically 'live' in the same way that ",
strong("spreadsheets"),
" are live. Outputs change instantly as users modify inputs, without requiring a reload of the browser.")
)
)
)
# Define server logic ----
server <- function(input, output) {
}
# Run the app ----
shinyApp(ui = ui, server = server)Reference:
Shiny - Add control widgets