加入輸入元件
輸入元件是 Shiny 讓使用者與應用程式互動的方式。例如,textInput() 用來讓使用者輸入文字,而 numericInput() 則讓使用者選擇數字。在下一章你會看到更多其他類型的輸入元件。
要在你的應用程式中加入輸入元件,只要把輸入函式放到 fluidPage() 裡即可。回想影片中提到,所有輸入函式都有相同的前兩個引數:inputId 和 label。
本練習屬於課程
案例研究:使用 R 的 Shiny 建置網頁應用程式
練習說明
- 定義這個 Shiny 應用程式的 UI。
- 建立一個 ID 為「age」且標籤為「How old are you?」的數值輸入。
- 建立一個 ID 為「name」且標籤為「What is your name?」的文字輸入。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
library(shiny)
# Define UI for the application
ui <- ___(
# Create a numeric input with ID "age" and label of
# "How old are you?"
numericInput(___, ___, value = 20),
# Create a text input with ID "name" and label of
# "What is your name?"
___(___, ___)
)
# Define the server logic
server <- function(input, output) {}
# Run the application
shinyApp(ui = ui, server = server)