11generate_md_episodes <- function () {
22
3- library(" methods" )
4-
5- if (! require(" remotes" , quietly = TRUE )) {
6- install.packages(" remotes" , repos = c(CRAN = " https://cloud.r-project.org/" ))
3+ if (! requireNamespace(" renv" , quietly = TRUE )) {
4+ install.packages(" renv" , repos = c(CRAN = " https://cloud.r-project.org/" ))
75 }
86
9- if (! require( " requirements " , quietly = TRUE )) {
10- remotes :: install_github( " hadley/requirements " )
7+ if (! requireNamespace( " rprojroot " , quietly = TRUE )) {
8+ install.packages( " rprojroot " , repos = c( CRAN = " https://cloud.r-project.org/ " ) )
119 }
1210
11+ cfg <- rprojroot :: has_file_pattern(" ^_config.y*ml$" )
12+ root <- rprojroot :: find_root(cfg )
13+
1314 required_pkgs <- unique(c(
1415 # # Packages for episodes
15- requirements ::: req_dir( " _episodes_rmd" ),
16+ renv :: dependencies(file.path( root , " _episodes_rmd" ), progress = FALSE , error = " ignore " ) $ Package ,
1617 # # Pacakges for tools
17- requirements ::: req_dir( " bin" )
18+ renv :: dependencies(file.path( root , " bin" ), progress = FALSE , error = " ignore " ) $ Package
1819 ))
1920
2021 missing_pkgs <- setdiff(required_pkgs , rownames(installed.packages()))
@@ -28,7 +29,8 @@ generate_md_episodes <- function() {
2829 if (require(" knitr" ) && packageVersion(" knitr" ) < ' 1.9.19' )
2930 stop(" knitr must be version 1.9.20 or higher" )
3031
31- # # get the Rmd file to process from the command line, and generate the path for their respective outputs
32+ # # get the Rmd file to process from the command line, and generate the path
33+ # # for their respective outputs
3234 args <- commandArgs(trailingOnly = TRUE )
3335 if (! identical(length(args ), 2L )) {
3436 stop(" input and output file must be passed to the script" )
@@ -40,20 +42,28 @@ generate_md_episodes <- function() {
4042 # # knit the Rmd into markdown
4143 knitr :: knit(src_rmd , output = dest_md )
4244
43- # Read the generated md files and add comments advising not to edit them
44- vapply(dest_md , function (y ) {
45- con <- file(y )
46- mdfile <- readLines(con )
47- if (mdfile [1 ] != " ---" )
48- stop(" Input file does not have a valid header" )
49- mdfile <- append(mdfile , " # Please do not edit this file directly; it is auto generated." , after = 1 )
50- mdfile <- append(mdfile , paste(" # Instead, please edit" ,
51- basename(y ), " in _episodes_rmd/" ), after = 2 )
52- writeLines(mdfile , con )
53- close(con )
54- return (paste(" Warning added to YAML header of" , y ))
55- },
56- character (1 ))
45+ # Read the generated md files and add comments advising not to edit them
46+ add_no_edit_comment <- function (y ) {
47+ con <- file(y )
48+ mdfile <- readLines(con )
49+ if (mdfile [1 ] != " ---" )
50+ stop(" Input file does not have a valid header" )
51+ mdfile <- append(
52+ mdfile ,
53+ " # Please do not edit this file directly; it is auto generated." ,
54+ after = 1
55+ )
56+ mdfile <- append(
57+ mdfile ,
58+ paste(" # Instead, please edit" , basename(y ), " in _episodes_rmd/" ),
59+ after = 2
60+ )
61+ writeLines(mdfile , con )
62+ close(con )
63+ return (paste(" Warning added to YAML header of" , y ))
64+ }
65+
66+ vapply(dest_md , add_no_edit_comment , character (1 ))
5767}
5868
5969generate_md_episodes()
0 commit comments