在學完基礎的網頁布局后,我們就可以學學如何在網頁中加一些控件,讓網頁顯得可以交互(要想真的能夠交互,還得學下一節的內容)。
那什么叫做控件呢?所謂的控件就是用戶能夠與其互動的網頁元素。Shiny自帶的一些控制插件如下:
函數 | 插件功能 |
---|---|
actionButton | 操作按鈕 |
checkboxGroupInput | 一組復選框 |
checkboxInput | 單個復選框 |
dateInput | 單個日期選擇 |
dateRangeInput | 一組日期選擇 |
fileInput | 文件上傳A file upload control wizard |
helpText | 可添加到輸入窗體的幫助文本 |
numericInput | 數字輸入 |
radioButtons | 單選按鈕 |
selectInput | 一個可供選擇的框 |
sliderInput | 滑動條 |
submitButton | 提交按鈕 |
textInput | 輸入文本的字段 |
具體這些函數在網頁展示是什么效果呢? 其實Shiny也想到了,所以給了一個控件全景圖
展覽
增加控件
讓我們繼續從一個非常j簡單的布局開始,嘗試添加幾個控件。
ui <- fluidPage(
titlePanel("Hello Widgets"),
sidebarLayout(
sidebarPanel(),
mainPanel()
)
)
以文本輸入控件textInput
為例,
textInput(inputId, label, value = "", width = NULL,
placeholder = NULL)
前兩個參數分別是該控件的唯一ID和在網頁中顯示的名字,所有控件的前兩個都是這兩個參數。第一個參數在所有插件中必須唯一,后續的數據交互時shiny需要根據這個ID獲取輸入信息。
加入該控件的代碼如下
ui <- fluidPage(
titlePanel("Hello Widgets"),
sidebarLayout(
sidebarPanel(
h1("Please input your name"),
textInput("input1", label = "", value = "",
placeholder = "your name")
),
mainPanel(
h1("Showing results")
)
)
)
輸入對應的是HTML中的<input>
元素,里面的屬性type="text"
表明它的輸入數據是文本。
檢查元素
練習
這部分的內容主要是了解,在后續需要時挑選對應的工具,如下 是練習題
練習題
如下是我寫的代碼
library(shiny)
ui <- fluidPage(
titlePanel("censusVis"),
sidebarLayout(
sidebarPanel(
helpText("Create demographic maps with information from the 2010 US census"),
selectInput("checkBox1",
label = "choose a variable to display",
choices = list("A"=1,
"B"=2)
),
sliderInput("sliderInput",
label = "Range of interest",
min = 0, max=100, value = c(0,100), step=10)
),
mainPanel(
)
)
)
server <- function(input, output){
}
shinyApp(ui = ui, server = server)
那么如何根據輸入調整輸出呢?這就是下一節的內容了
參考資料
傳送門
Shiny基礎教程: