Bắt đầu ngayBắt đầu miễn phí

shinydashboard đầu tiên của bạn với các phần tử tương tác

Bạn đã thấy cách thêm các phần tử tương tác vào một shinydashboard, và cũng biết cách thêm các bảng dữ liệu tương tác.

Trong bài tập này, dữ liệu Airbnb listings được lưu dưới dạng data frame tên là listings, và bản đồ không gian địa lý được lưu dưới dạng đối tượng leaflet tên m_london. Phần header và thanh bên đã được lưu lần lượt là headersidebar.

Các hàm trợ giúp tùy chỉnh:

  • make_plots: Vẽ box plot hoặc violin plot
  • num_listings: Tính số lượng listing dựa trên khoảng được chọn
  • num_private_rooms: Tính số phòng riêng tư dưới dạng phần trăm trên tổng số listing, dựa trên khoảng được chọn
  • median_price: Tính median của giá, dựa trên khoảng được chọn Để tìm hiểu thêm về một hàm trợ giúp, bạn có thể chạy nó (ví dụ make_plots) trong console.

Bài tập này là một phần của khóa học

Xây dựng Dashboard với shinydashboard

Xem khóa học

Hướng dẫn bài tập

  • Thêm box plot/violin plot plotly tên "plots".
  • Thêm một bảng dữ liệu tên "table".
  • Thêm một bản đồ leaflet tên "map".
  • Thiết lập các đối số đúng trong dashboardPage().

Bài tập tương tác thực hành trực tiếp

Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.

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)
Chỉnh sửa và Chạy Mã