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

Tạo Sidebar Input Toàn cục

Hãy tạo một sidebar toàn cục cho dashboard nhiều trang.

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

Xây dựng Dashboard với flexdashboard

Xem khóa học

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

  • Biến sidebar trên dashboard này thành toàn cục để nó xuất hiện trên cả hai trang.

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.

{"my_document.Rmd":"---\ntitle: \"Bike Shares Daily\"\noutput: \n  flexdashboard::flex_dashboard:\n    orientation: columns\n    vertical_layout: fill\nruntime: shiny\n---\n\n```{r global, include=FALSE}\nlibrary(flexdashboard)\nlibrary(readr)\nlibrary(leaflet)\nlibrary(DT)\nlibrary(tidyverse)\nlibrary(lubridate)\nlibrary(plotly)\n\ntrips_df <- read_csv('https://assets.datacamp.com/production/repositories/1448/datasets/1f12031000b09ad096880bceb61f6ca2fd95e2eb/sanfran_bikeshare_joined_oneday.csv') %>%\n  mutate(duration_min = duration_sec / 60)\n\n```\n\nOverview\n====================\n\nColumn {data-width=200 .sidebar}\n-----------------------------------------------------------------------\n\n```{r}\n\nsliderInput(\"duration_slider\", label = \"Select maximum trip duration to display (in minutes):\",\n            min = 0, max = 120, value = 15, step = 5, dragRange = TRUE)\n\nsliderInput(\"duration_bin\", label = \"Select # of minutes to bin trip durations:\",\n           min = 1, max = 15, value = 1, step = 1)\n\nshow_trips_df <- reactive({\n\n  trips_df %>%\n    filter(duration_sec <= input$duration_slider * 60)\n\n})\n```\n\nColumn {data-width=450}\n-----------------------------------------------------------------------\n\n### Origins\n\n```{r}\n\nrenderLeaflet({\n  show_trips_df() %>%\n    rename(latitude = start_latitude,\n           longitude = start_longitude) %>%\n    group_by(start_station_id, latitude, longitude) %>%\n    count() %>%\n    leaflet() %>%\n    addTiles() %>%\n    addCircles(radius = ~n)\n})\n\n```\n\n\nColumn {data-width=350}\n-----------------------------------------------------------------------\n\n### Total Trips\n\n```{r}\n\nrenderValueBox({\n  valueBox(prettyNum(show_trips_df() %>%\n                       nrow(), big.mark = ','), \n           icon = 'fa-bicycle')\n})\n\n```\n\n### Trips by Start Time\n\n```{r}\n\nrenderPlot({show_trips_df() %>%\n    mutate(hour = hour(start_date)) %>%\n    group_by(hour) %>%\n    summarize(`Trips Started` = n()) %>%\n    ggplot(aes(x = hour, y = `Trips Started`)) +\n    theme_bw() +\n    ylab('Trips Started \\n') +\n    geom_bar(stat = 'identity') \n})\n\n```\n\nDuration\n====================\n\n### Trip Durations\n\n```{r}\n\nrenderPlot({show_trips_df() %>%\n  mutate(`Trip Duration (min)` = duration_sec / 60) %>%\n  ggplot(aes(x = `Trip Duration (min)`)) +\n  theme_bw() +\n  geom_histogram(binwidth = input$duration_bin) +\n  ylab('# Trips')\n})\n\n```\n\n\n"}
Chỉnh sửa và Chạy Mã