3 min read

flexdashboard 使用技巧

本文于2020-10-10更新。 如发现问题或者有建议,欢迎提交 Issue

部分展示可以参考 博客监控 。 主要参考 RStudio (2018)

1 包的受欢迎程度

library(tidyverse)
library(packagefinder)
library(dlstats)
library(cranly)
library(lubridate)
knitr::opts_chunk$set(message = FALSE, warning = FALSE, error = TRUE)
sem_pkg <- 
    bind_rows(
    'dashboard' %>% 
        findPackage() %>% 
        as_tibble()
    )
sem_pkg_download <- 
    sem_pkg %>% 
    rename_all(tolower) %>% 
    arrange(desc(score)) %>% 
    distinct(name) %>% 
    # head(100) %>% 
    .$name %>% 
    # 可以插入 vector,所以不需要map
    cran_stats()
sem_pkg_mostdownload <- 
    sem_pkg_download %>% 
    group_by(package) %>% 
    summarize(downloads = mean(downloads)) %>%
    arrange(desc(downloads))
sem_pkg_download %>% 
    filter(year(end) >= 2017) %>% 
    filter(package %in% sem_pkg_mostdownload[1:10,]$package) %>% 
    mutate(is_high = package %in% sem_pkg_mostdownload[1:5,]$package) %>% 
    ggplot(aes(x=end,y=downloads,col=package)) + 
    geom_line() + 
    facet_wrap(~is_high,scales = 'free_y')
  1. 目前flexdashboard包还处于上升期。

2 元素定义

每个元素定义,通过一个#来展示。

3 columns

Column {data-width=750}
-----------------------------------------------------------------------

data-width=750data-width=250把一个页面按照1:3分开。 (McVey 2018 * Column widths | R)

4 rows

output: 
  flexdashboard::flex_dashboard:
    orientation: rows

修改yaml参数 * (McVey 2018 Row orientation | R)

5

6 tabsets

Column {.tabset data-width=650}
-----------------------------------------------------------------------

写在开头即可。 参考 RStudio

不是对

Overview
=====================================

7 pages

Overview
=====================================

Add the text “Overview” followed by a line with 16 or more = to add the first page. (McVey 2018 * Pages | R)

9 Web-friendly plot

参考 ggplot2使用技巧 Web-friendly plot

10 leaflet map

leaflet包可以构建地图,只要给予经纬度 (McVey 2018 * Add a leaflet map | R * Color indicators | R) vertical_layout: fill的yaml参数可以让地图覆盖整个页面。

library(flexdashboard)
library(readr)
library(tidyverse)
library(leaflet)

stations_df <- read_csv('http://s3.amazonaws.com/assets.datacamp.com/production/course_6355/datasets/stations_data.csv')
leaflet() %>%
  addTiles() %>%
  addMarkers(lat = stations_df$latitude, lng = stations_df$longitude)
## Error in validateCoords(lng, lat, funcName): addMarkers requires non-NULL longitude/latitude values

data = stations_df参数也可以直接给data.frame (McVey 2018 * Add a leaflet map directly from dataframe | R) Assuming "longitude" and "latitude" are longitude and latitude, respectively自动识别了经纬度的变量。

leaflet(stations_df) %>%
    addTiles() %>%
    addMarkers()
## Error in guessLatLongCols(names(obj)): Couldn't infer longitude/latitude columns

11 value box

date_data %>% 
    filter(date > now() - ddays(90)) %>% 
    count %>% 
    pull %>% 
    valueBox(., caption = "近90天发文量", icon="fa-pencil"
             , color = ifelse(. < 30, "warning", "primary"))
  1. caption 显示的文字
  2. color 显示的颜色
  3. icon 显示的背景图案,更多参考 https://ionicons.com/

12 gauge

gauge函数展示指标 (McVey 2018 * Create gauge | R)

gauge(value = 60,
      min = 0,
      max = 100)

12.1 Color indicators

gauge(value = pct_short_trips,
      min = 0,
      max = 100,
      sectors = gaugeSectors(success = c(67, 100),
                             warning = c(33, 66),
                             danger = c(0, 32)))

13 table

  1. 使用kable函数做静态表格,可以使用滚轮上下滑动
  2. 使用DT::datatable函数做动态表格 [McVey (2018)

13.1 extensions to download

mtcars %>% 
    DT::datatable(
        rownames = FALSE,
        extensions = 'Buttons', options = list(
            dom = 'Bfrtip',
            buttons = c('copy', 'csv', 'excel', 'pdf', 'print')
      )
    )
  1. extensions = 'Buttons'可以选择下载的方式,更多见 https://datatables.net/extensions/index [McVey (2018)

14 Caption

比如可以加上数据来源等,如 > ... (McVey 2018 * Captions | R)

15 Storyboards

yaml中声明storyboard: true,然后每个标题### some text就是 Storyboard 了。 (McVey 2018 * Storyboards | R)

15.1 Commentary

在R Chunk后插入***,然后写入1.或者+即可。 (McVey 2018 * Storyboard Commentary | R)

Reference

McVey, Elaine. 2018. “Building Dashboards with Flexdashboard.” DataCamp. 2018. https://www.datacamp.com/courses/building-dashboards-with-flexdashboard.

RStudio. 2018. Using Flexdashboard. RStudio.