你的第一個含互動元素的 shinydashboard
你已經看過如何在 shinydashboard 中加入互動元素,也學過如何加入互動式資料表。
在此練習中,Airbnb 的房源已存成名為 listings 的資料框,而地理空間地圖已存成名為 m_london 的 leaflet 物件。頁首與側邊欄已分別存為 header 與 sidebar。
自訂輔助函式:
make_plots:繪製盒狀圖或小提琴圖num_listings:根據所選範圍計算房源數量num_private_rooms:根據所選範圍計算「獨立房間」佔所有房源的百分比median_price:根據所選範圍計算價格中位數 若想進一步了解某個輔助函式,你可以在主控台執行它(例如make_plots)。
本練習屬於課程
使用 shinydashboard 建立儀表板
練習說明
- 加入名為
"plots"的plotly盒狀圖/小提琴圖。 - 加入名為
"table"的資料表。 - 加入名為
"map"的leaflet地圖。 - 在
dashboardPage()中設定正確的參數。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
body <- dashboardBody(
tabItems(
tabItem(tabName = "charts",
fluidRow(
valueBoxOutput(outputId = "count"), valueBoxOutput(outputId = "prop"),valueBoxOutput(outputId = "med") ),
fluidRow(
tabBox(side = "left", id = "tabset", height = "500px",
tabPanel("Charts",
# Place plotly object here
fluidRow(box(___("plots", height = 500, width = 600)) ) ),
# Place dataTable object here
tabPanel("Data table", height = "500px", ___("table")) ),
box(side = "right", height = "200px", title = "Welcome to London!",),
box(side = "right", height = "385px", title = "Controls",
sliderInput(inputId = "range", label = "Select price range:",
min = 0, max = 25000,value = c(0,2500)),
selectInput(inputId = "select", label = "Select group:",
choices = c("Box plots", "Violin plots")) ) ) ),
tabItem(tabName = "map",
# Place leaflet object here
fluidRow(box(title = "Map of listings in London", ___("map", height = 600, width = 700))) ) ) )
# Set the correct arguments for dashboardPage()
ui <- dashboardPage(___, ___, ___)
server <- function(input, output) {
output$count <- renderValueBox(valueBox("Number of listings", num_listings(input$range),
icon = icon("house-user") ))
output$prop <- renderValueBox(valueBox("Private rooms", paste0(num_private_rooms(input$range), "% of all listings"),
icon = icon("eye"), color = "orange") )
output$med <- renderValueBox(valueBox("Median price", paste0(median_price(input$range), "£"),
icon = icon("money-bill-alt"), color = "olive") )
output$plots <- renderPlotly(make_plots(input$range, input$select))
output$table <- renderDataTable(filter(listings, price >= input$range[1], price <= input$range[2]) %>% select(c(name, neighbourhood, room_type, price)),
options = list(lengthMenu = c(5, 30, 50)))
output$map <- renderLeaflet(m_london)
}
shinyApp(ui, server)