Last updated: 2023-05-19
Checks: 7 0
Knit directory: muse/
This reproducible R Markdown analysis was created with workflowr (version 1.7.0). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.
Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.
Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.
The command set.seed(20200712)
was run prior to running
the code in the R Markdown file. Setting a seed ensures that any results
that rely on randomness, e.g. subsampling or permutations, are
reproducible.
Great job! Recording the operating system, R version, and package versions is critical for reproducibility.
Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.
Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.
Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.
The results in this page were generated with repository version 50c92d7. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.
Note that you need to be careful to ensure that all relevant files for
the analysis have been committed to Git prior to generating the results
(you can use wflow_publish
or
wflow_git_commit
). workflowr only checks the R Markdown
file, but you know if there are other scripts or data files that it
depends on. Below is the status of the Git repository when the results
were generated:
Ignored files:
Ignored: .Rhistory
Ignored: .Rproj.user/
Ignored: r_packages_4.1.2/
Ignored: r_packages_4.2.0/
Ignored: r_packages_4.2.2/
Ignored: r_packages_4.3.0/
Untracked files:
Untracked: analysis/cell_ranger.Rmd
Untracked: analysis/tss_xgboost.Rmd
Untracked: data/HG00702_SH089_CHSTrio.chr1.vcf.gz
Untracked: data/HG00702_SH089_CHSTrio.chr1.vcf.gz.tbi
Untracked: data/ncrna_NONCODE[v3.0].fasta.tar.gz
Untracked: data/ncrna_noncode_v3.fa
Unstaged changes:
Modified: analysis/graph.Rmd
Modified: script/run_rstudio.sh
Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.
These are the previous versions of the repository in which changes were
made to the R Markdown (analysis/json_vs_yaml.Rmd
) and HTML
(docs/json_vs_yaml.html
) files. If you’ve configured a
remote Git repository (see ?wflow_git_remote
), click on the
hyperlinks in the table below to view the files as they were in that
past version.
File | Version | Author | Date | Message |
---|---|---|---|---|
Rmd | 50c92d7 | Dave Tang | 2023-05-19 | JSON and YAML formats |
JSON and YAML are popular serialisation formats.
In computing, serialization (or serialisation) is the process of translating a data structure or object state into a format that can be stored (e.g. files in secondary storage devices, data buffers in primary storage devices) or transmitted (e.g. data streams over computer networks) and reconstructed later (possibly in a different computer environment).
Install the jsonlite and yaml packages, so that we can generate JSON and YAML.
install.packages(c("jsonlite", "yaml"))
Installing packages into '/packages'
(as 'lib' is unspecified)
Load libraries.
library(jsonlite)
library(yaml)
As a first example, we will convert the women
data set,
which is a small data set with 15 observations for 2 variables.
women
height weight
1 58 115
2 59 117
3 60 120
4 61 123
5 62 126
6 63 129
7 64 132
8 65 135
9 66 139
10 67 142
11 68 146
12 69 150
13 70 154
14 71 159
15 72 164
Convert women
to JSON.
women_json <- toJSON(women, pretty = TRUE)
women_json
[
{
"height": 58,
"weight": 115
},
{
"height": 59,
"weight": 117
},
{
"height": 60,
"weight": 120
},
{
"height": 61,
"weight": 123
},
{
"height": 62,
"weight": 126
},
{
"height": 63,
"weight": 129
},
{
"height": 64,
"weight": 132
},
{
"height": 65,
"weight": 135
},
{
"height": 66,
"weight": 139
},
{
"height": 67,
"weight": 142
},
{
"height": 68,
"weight": 146
},
{
"height": 69,
"weight": 150
},
{
"height": 70,
"weight": 154
},
{
"height": 71,
"weight": 159
},
{
"height": 72,
"weight": 164
}
]
Convert women
to YAML.
women_yaml <- as.yaml(women, indent = 3)
writeLines(women_yaml)
height:
- 58.0
- 59.0
- 60.0
- 61.0
- 62.0
- 63.0
- 64.0
- 65.0
- 66.0
- 67.0
- 68.0
- 69.0
- 70.0
- 71.0
- 72.0
weight:
- 115.0
- 117.0
- 120.0
- 123.0
- 126.0
- 129.0
- 132.0
- 135.0
- 139.0
- 142.0
- 146.0
- 150.0
- 154.0
- 159.0
- 164.0
JSON to data frame.
fromJSON(women_json)
height weight
1 58 115
2 59 117
3 60 120
4 61 123
5 62 126
6 63 129
7 64 132
8 65 135
9 66 139
10 67 142
11 68 146
12 69 150
13 70 154
14 71 159
15 72 164
YAML to data frame. This does not work for more complex data structures (see below).
yaml.load(women_yaml, handlers = list(map = function(x) as.data.frame(x) ))
height weight
1 58 115
2 59 117
3 60 120
4 61 123
5 62 126
6 63 129
7 64 132
8 65 135
9 66 139
10 67 142
11 68 146
12 69 150
13 70 154
14 71 159
15 72 164
A data frame containing lists.
my_df <- data.frame(
id = 1:3,
title = letters[1:3]
)
my_df$keywords = list(
c('aa', 'aaa', 'aaaa'),
c('bb', 'bbb'),
c('cc', 'ccc', 'cccc', 'ccccc')
)
my_df
id title keywords
1 1 a aa, aaa, aaaa
2 2 b bb, bbb
3 3 c cc, ccc, cccc, ccccc
Convert my_df
to JSON.
my_df_json <- toJSON(my_df, pretty = TRUE)
my_df_json
[
{
"id": 1,
"title": "a",
"keywords": ["aa", "aaa", "aaaa"]
},
{
"id": 2,
"title": "b",
"keywords": ["bb", "bbb"]
},
{
"id": 3,
"title": "c",
"keywords": ["cc", "ccc", "cccc", "ccccc"]
}
]
Convert my_df
to YAML.
my_df_yaml <- as.yaml(my_df, indent = 3)
writeLines(my_df_yaml)
id:
- 1
- 2
- 3
title:
- a
- b
- c
keywords:
- - aa
- aaa
- aaaa
- - bb
- bbb
- - cc
- ccc
- cccc
- ccccc
Converting from JSON to YAML is easy.
identical(writeLines(as.yaml(fromJSON(my_df_json))), writeLines(my_df_yaml))
id:
- 1
- 2
- 3
title:
- a
- b
- c
keywords:
- - aa
- aaa
- aaaa
- - bb
- bbb
- - cc
- ccc
- cccc
- ccccc
id:
- 1
- 2
- 3
title:
- a
- b
- c
keywords:
- - aa
- aaa
- aaaa
- - bb
- bbb
- - cc
- ccc
- cccc
- ccccc
[1] TRUE
Converting from YAML to JSON for my_df
is not as
straight-forward because of the different number of keywords.
my_df_list <- yaml.load(my_df_yaml)
my_df_list
$id
[1] 1 2 3
$title
[1] "a" "b" "c"
$keywords
$keywords[[1]]
[1] "aa" "aaa" "aaaa"
$keywords[[2]]
[1] "bb" "bbb"
$keywords[[3]]
[1] "cc" "ccc" "cccc" "ccccc"
This conversion is different from the original data frame to JSON conversion because this creates a single object, where as the original conversion creates an array with three objects.
toJSON(my_df_list, pretty = TRUE)
{
"id": [1, 2, 3],
"title": ["a", "b", "c"],
"keywords": [
["aa", "aaa", "aaaa"],
["bb", "bbb"],
["cc", "ccc", "cccc", "ccccc"]
]
}
my_df_json
[
{
"id": 1,
"title": "a",
"keywords": ["aa", "aaa", "aaaa"]
},
{
"id": 2,
"title": "b",
"keywords": ["bb", "bbb"]
},
{
"id": 3,
"title": "c",
"keywords": ["cc", "ccc", "cccc", "ccccc"]
}
]
I could probably write a hacky function to make the conversion but I won’t.
sessionInfo()
R version 4.3.0 (2023-04-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.2 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so; LAPACK version 3.10.0
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
time zone: Etc/UTC
tzcode source: system (glibc)
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] yaml_2.3.7 jsonlite_1.8.4 workflowr_1.7.0
loaded via a namespace (and not attached):
[1] vctrs_0.6.2 httr_1.4.5 cli_3.6.1 knitr_1.42
[5] rlang_1.1.0 xfun_0.39 stringi_1.7.12 processx_3.8.1
[9] promises_1.2.0.1 glue_1.6.2 rprojroot_2.0.3 git2r_0.32.0
[13] htmltools_0.5.5 httpuv_1.6.9 ps_1.7.5 sass_0.4.5
[17] fansi_1.0.4 rmarkdown_2.21 jquerylib_0.1.4 tibble_3.2.1
[21] evaluate_0.20 fastmap_1.1.1 lifecycle_1.0.3 whisker_0.4.1
[25] stringr_1.5.0 compiler_4.3.0 fs_1.6.2 pkgconfig_2.0.3
[29] Rcpp_1.0.10 rstudioapi_0.14 later_1.3.0 digest_0.6.31
[33] R6_2.5.1 utf8_1.2.3 pillar_1.9.0 callr_3.7.3
[37] magrittr_2.0.3 bslib_0.4.2 tools_4.3.0 cachem_1.0.7
[41] getPass_0.2-2