{r setup, include=FALSE} knitr::opts_chunk$set(eval = FALSE) 如发现问题或者有建议,欢迎提交 Issue
rlist包是任坤开发,用于处理非表列数据(non-tabular data),常见数据类型为JSON和YAML字段。
<!-- renkun tutorial 的例子。 -->
<!-- ``` -->
<!-- library(rlist) -->
<!-- library(pipeR) -->
<!-- library(tidyverse) -->
<!-- url <- "https://renkun-ken.github.io/rlist-tutorial/data/people.json" -->
<!-- people <- list.load(url) -->
<!-- people %>% -->
<!-- list.filter(Expertise$R >= 1 & Expertise$Python >= 1) %>% -->
<!-- list.class(Interests) %>% -->
<!-- list.sort(-length(.)) %>% -->
<!-- list.take(3) %>% -->
<!-- list.map(. %>>% list.table(Age)) -->
<!-- ``` -->
pipeline的选择
%>%also works with rlist functions. However, in some cases, the operator may impose conflicting interpretation on.symbol to cause unexpected error.[@Ren2016]
这里会考虑使用pipeR:%>>%:。
JSON格式
In the coming tutorial pages, we will mainly use JSON data to demonstrate the features and examples of rlist package.
[]creates a unnamed node array.{}creates a named node list."key" : valuecreates a key-value pair wherevaluecan be a number, a string, a[]array, or a{}list.
rlist package imports jsonlite package to read/write JSON data.
YAML格式
rlistalso imports yaml package to read/writeYAMLdata.
- Name: Ken
Age: 24
Interests:
- reading
- music
- movies
Expertise:
R: 2
CSharp: 4
Python: 3
- Name: James
Age: 25
Interests:
- sports
- music
Expertise:
R: 3
Java: 2
Cpp: 5
- Name: Penny
Age: 24
Interests:
- movies
- reading
Expertise:
R: 1
Cpp: 4
Python: 2
数据来源于 @Ren2016 https://renkun-ken.github.io/rlist-tutorial/data/sample.yaml 常见于RMarkdown文档的开头申明。
博客可以使用这个来提取yaml数据
举例
{r} library(tidyverse) library(rlist) people <- list.load("data/sample.json")
{r} str(people)
NOTE:
str()previews the structure of an object. We may use this function more often to avoid verbose representation of list objects.
这个函数展示很不错。
{r} people %>% map( ~ list( name = .$Name ,age = .$Age ,range = .$Expertise %>% as.numeric %>% range %>% as.character %>% str_flatten('-') ) ) %>% map(~glue::glue("{.$name}: {.$age} and has {.$range} year experience"))
这个可以用来出tutorial的题目。
filter
{r} people %>% list.filter(Age >= 25 & "music" %in% Interests) %>% map(~.$Name)
这个还没找好很好的替代方式。
list.findlist.findilist.firstlist.lastlist.takelist.skip
太多了,没什么好选择的。
是list.filter 的替代品,只反馈一部分的字段,因为json格式很大时,反馈时间很长。