本文于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')
- 目前
flexdashboard
包还处于上升期。
2 元素定义
每个元素定义,通过一个#
来展示。
3 columns
Column {data-width=750}
-----------------------------------------------------------------------
data-width=750
和data-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"))
caption
显示的文字color
显示的颜色icon
显示的背景图案,更多参考 https://ionicons.com/
11.1 add link
valueBox(median_min,
caption = 'Median Trip Duration (Minutes)',
icon = 'fa-clock-o',
href = '#trip-duration'
)
href
参数可以接短链接或者长链接
(McVey 2018 * Linking | R)
- 如果标题是
trip duration
如果是英文可以直接用#trip-duration
引用。 - 如果标题用
#sometag
进行了标记,那么就可以用#sometag
引用。
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
- 使用
kable
函数做静态表格,可以使用滚轮上下滑动 - 使用
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')
)
)
extensions = 'Buttons'
可以选择下载的方式,更多见 https://datatables.net/extensions/index [McVey (2018)
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.