更新輸出(server)
就快完成了!最後一步是更新圖形輸出,顯示使用者輸入的 name 對應之 prop 與 year 的折線圖,並依 sex 著色。你可以用這個範本來建立你的圖:
ggplot(subset(babynames, name == "David")) +
geom_line(aes(x = year, y = prop, color = sex))
別忘了,在 server 中,名為 foo 的使用者輸入可透過 input$foo 來存取。我們已經預先載入 shiny 與 ggplot2 套件,以及 babynames 資料集。
本練習屬於課程
使用 R 的 Shiny 建立網頁應用程式
練習說明
- 在
renderPlot()中加入繪圖程式碼。記得將硬編碼的名稱(name == "David")替換為使用者輸入的名字。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
ui <- fluidPage(
titlePanel("Baby Name Explorer"),
sidebarLayout(
sidebarPanel(textInput('name', 'Enter Name', 'David')),
mainPanel(plotOutput('trend'))
)
)
server <- function(input, output, session) {
output$trend <- renderPlot({
# CODE BELOW: Update to display a line plot of the input name
ggplot()
})
}
shinyApp(ui = ui, server = server)