建立 babynames 探索 Shiny 應用程式

使用 R 的 Shiny 建立網頁應用程式

Ramnath Vaidyanathan

VP of Product Research

先畫出你的 App 雛形

電腦手繪的雛形:顯示某個嬰兒名字隨時間變化的應用程式範例

使用 R 的 Shiny 建立網頁應用程式

加入輸入元件(UI)

ui <- fluidPage(

titlePanel("Baby Name Explorer"),
textInput('name', 'Enter Name', 'David')
)
server <- function(input, output, session){

}
shinyApp(ui = ui, server = server)

一個網頁應用,標題為「Baby name explorer」,輸入的名字是「David」

使用 R 的 Shiny 建立網頁應用程式

加入輸出元件(UI/server)

ui <- fluidPage(
  titlePanel("Baby Name Explorer"),
  textInput('name', 'Enter Name', 'David'),

plotOutput('trend')
)
server <- function(input, output, session){

output$trend <- renderPlot({ ggplot() })
}
shinyApp(ui = ui, server = server)
使用 R 的 Shiny 建立網頁應用程式

加入輸出元件(UI/server)

一個網頁應用,標題為「Baby names explorer」,輸入「David」,並顯示一個空白的圖表

使用 R 的 Shiny 建立網頁應用程式

調整版面配置(UI)

ui <- fluidPage(
  titlePanel("Baby Name Explorer"),

sidebarLayout(
sidebarPanel(
textInput('name', 'Enter Name', 'David')
),
mainPanel(
plotOutput('trend')
)
)
)
server <- function(input, output, session){
  output$trend <- renderPlot({ggplot()})
}
使用 R 的 Shiny 建立網頁應用程式

調整版面配置(UI)

一個網頁應用,標題為「Baby names explorer」,輸入「David」,左側為名字輸入區,右側為空白圖表

使用 R 的 Shiny 建立網頁應用程式

更新輸出(server)

ui <- fluidPage(
  ...
)
server <- function(input, output, session){
  output$trend <- renderPlot({

data_name <- subset( babynames, name == input$name )
ggplot(data_name) + geom_line( aes(x = year, y = prop, color = sex) )
}) }

一個網頁應用,標題為「Baby names explorer」,輸入「David」,並顯示該名字隨時間變化的頻率圖;左側為輸入,右側為圖表

使用 R 的 Shiny 建立網頁應用程式

一起來練習吧!

使用 R 的 Shiny 建立網頁應用程式

Preparing Video For Download...