เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

shinydashboard แรกที่มี interactive elements

ได้เรียนรู้แล้วว่า interactive elements สามารถเพิ่มลงใน shinydashboard ได้อย่างไร รวมถึงวิธีเพิ่ม interactive data table

ในแบบฝึกหัดนี้ ข้อมูลรายการ Airbnb ถูกเก็บไว้ในรูปแบบ data frame ชื่อ listings และแผนที่ geospatial ถูกเก็บไว้เป็น object ของ leaflet ชื่อ m_london ส่วน header และ sidebar ถูกเก็บไว้เป็น header และ sidebar ตามลำดับ

ฟังก์ชัน helper ที่กำหนดไว้:

  • make_plots: สร้าง box plot หรือ violin plot
  • num_listings: คำนวณจำนวนรายการตามช่วงที่เลือก
  • num_private_rooms: คำนวณจำนวนห้องส่วนตัวในรูปแบบเปอร์เซ็นต์จากรายการทั้งหมด ตามช่วงที่เลือก
  • median_price: คำนวณราคามัธยฐานตามช่วงที่เลือก หากต้องการทราบข้อมูลเพิ่มเติมเกี่ยวกับฟังก์ชัน helper สามารถรันฟังก์ชันนั้น (เช่น make_plots) ใน console ได้

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

การสร้างแดชบอร์ดด้วย shinydashboard

ดูคอร์ส

คำแนะนำการฝึกหัด

  • เพิ่ม plotly box plot/violin plot โดยตั้งชื่อว่า "plots"
  • เพิ่ม data table โดยตั้งชื่อว่า "table"
  • เพิ่มแผนที่ leaflet โดยตั้งชื่อว่า "map"
  • กำหนด argument ที่ถูกต้องใน 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)
แก้ไขและรันโค้ด