Skip to content

Commit a38ed4b

Browse files
maellestephlocke
authored andcommitted
Improving 📦 quality (#4)
1 parent 4669959 commit a38ed4b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1397
-460
lines changed

.Rbuildignore

+12
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
1+
^appveyor\.yml$
2+
^codecov\.yml$
3+
^\.travis\.yml$
4+
^LICENSE\.md$
5+
^README\.Rmd$
6+
^_redirects$
7+
^config\.toml$
8+
^content$
9+
^index\.Rmd$
10+
^layouts$
11+
^netlify\.toml$
12+
^static$
113
^.*\.Rproj$
214
^\.Rproj\.user$

.travis.yml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# R for travis: see documentation at https://docs.travis-ci.com/user/languages/r
2+
3+
language: R
4+
sudo: false
5+
cache: packages
6+
7+
after_success:
8+
- Rscript -e 'covr::codecov()'

DESCRIPTION

+29-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,38 @@
11
Package: learn
22
Version: 0.0.0.9000
33
Title: RECON LEARN content
4-
Description: Contains recon learn content
5-
Authors@R: person("Steph", "Locke", email = "steph@itsalocke.com", role = c("ctr"))
6-
License: CC-BY-4.0
4+
Description: Contains RECON learn content and infrastructure to build it.
5+
Authors@R: c(person("Steph",
6+
"Locke",
7+
email = "steph@itsalocke.com",
8+
role = c("ctr", "aut", "cre")),
9+
person("R Epidemics Consortium (RECON)",
10+
role = c("cph", "fnd"),
11+
email = "thibautjombart@gmail.com",
12+
comment = "https://www.repidemicsconsortium.org/"))
713
Encoding: UTF-8
814
LazyData: true
915
ByteCompile: true
10-
Imports: knitr,
16+
Imports:
17+
knitr,
1118
rmarkdown,
1219
automagic,
1320
htmltools,
14-
htmlwidgets
21+
htmlwidgets,
22+
snakecase,
23+
yaml,
24+
usethis (>= 1.3.0.9000),
25+
stringr,
26+
glue,
27+
jsonlite,
28+
fs,
29+
magrittr,
30+
dplyr,
31+
rlang
32+
RoxygenNote: 6.1.0
33+
License: MIT + file LICENSE
34+
Suggests:
35+
testthat,
36+
covr
37+
Remotes:
38+
r-lib/usethis

LICENSE

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
YEAR: 2018
2+
COPYRIGHT HOLDER: R Epidemics Consortium (RECON)

LICENSE.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# MIT License
2+
3+
Copyright (c) 2018 R Epidemics Consortium (RECON)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

NAMESPACE

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Generated by roxygen2: do not edit by hand
2+
3+
export("%>%")
4+
export(create_post)
5+
export(get_and_update_dependencies)
6+
export(render_new_rmds_to_md)
7+
export(save_and_use_widget)
8+
importFrom(magrittr,"%>%")
9+
importFrom(rlang,.data)

R/create_post.R

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#' Create a post from the template
2+
#'
3+
#' @details create a R Markdown file and open it.
4+
#'
5+
#' @param title Title for the post
6+
#' @param slug Short informative slug with words separated by "-"
7+
#' @param category Either "tutorials", "practicals" or "case studies"
8+
#' @param author Author as a vector
9+
#' @param date Date for the post
10+
#' @param licence_name Name of the license
11+
#' @param licence_url URL to the license
12+
#'
13+
#' @return Used only for side-effects.
14+
#' @export
15+
#'
16+
create_post <- function(title = "A study of Zika and Cholera",
17+
slug = NULL,
18+
category = c("tutorials", "practicals", "case studies"),
19+
author = c("Celina Turchi", "John Snow"),
20+
date = Sys.Date(),
21+
licence_name = "CC-BY",
22+
licence_url = "https://creativecommons.org/licenses/by/3.0/"){
23+
if(is.null(slug)){
24+
slug <- snakecase::to_any_case(title, case = "snake",
25+
sep_out = "-", sep_in = "-")
26+
}
27+
28+
singular_category <- switch(category[[1]],
29+
"practicals" = "practical",
30+
"tutorials" = "tutorial",
31+
"case studies" = "study")
32+
33+
post_path <- file.path("content", "post",
34+
glue::glue("{singular_category}-{slug}.Rmd"))
35+
36+
file.copy(system.file("rmarkdown", "templates",
37+
"skeleton",
38+
"skeleton.Rmd", package = "learn"),
39+
post_path,
40+
overwrite = TRUE)
41+
42+
file.copy(system.file("rmarkdown", "templates",
43+
"resources",
44+
"practical-phylogenetics.bib", package = "learn"),
45+
file.path("content", "post", paste0(slug, ".bib")),
46+
overwrite = TRUE)
47+
48+
file.copy(system.file("rmarkdown", "templates",
49+
"resources",
50+
"placeholder.jpg", package = "learn"),
51+
file.path("static","img", "highres", paste0(slug, ".jpg")),
52+
overwrite = TRUE)
53+
54+
yaml <- rmarkdown::yaml_front_matter(post_path, encoding = "UTF-8")
55+
56+
yaml$title <- title
57+
yaml$author <- toString(author)
58+
yaml$authors <- jsonlite::toJSON(author)
59+
yaml$topics <- jsonlite::toJSON(yaml$topics)
60+
yaml$categories <- jsonlite::toJSON(category)
61+
yaml$date <- as.character(date)
62+
yaml$bibliography <- paste0(slug, ".bib")
63+
yaml$image <- glue::glue("img/highres/{slug}.jpg")
64+
yaml$licenses <- licence_name
65+
66+
file_content <- readLines(post_path)
67+
i = grep('^---\\s*$', file_content)
68+
file_content <- file_content[(i[2]+1):length(file_content)]
69+
70+
newyaml <- yaml::as.yaml(yaml)
71+
newyaml <- stringr::str_remove_all(newyaml, "'")
72+
73+
file_content[grepl("\\- Zulma Cucunuba \\& Pierre Nouvellet\\: initial version",
74+
file_content)] <-
75+
glue::glue("- {toString(author)}: initial version")
76+
77+
file_content[grepl("\\[CC-BY\\]\\(htt", file_content)] <-
78+
glue::glue("**License**: [{licence_name}]({licence_url})")
79+
80+
file_content[grepl("Zulma Cucunuba \\& Pierre Nouvellet, 2017",
81+
file_content)] <-
82+
glue::glue("**Copyright**: {toString(author)}, {format(date, '%Y')}")
83+
84+
85+
file_content <- c(c("---"),
86+
newyaml,
87+
c("---"),
88+
file_content)
89+
print(post_path)
90+
writeLines(file_content, post_path,
91+
useBytes = TRUE)
92+
usethis::edit_file(post_path)
93+
94+
}

R/get_and_update_dependencies.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#' Determine all dependencies in blog posts and install them
22
#'
33
#' Using the automagic package, detect requirements in Rmd files
4-
#' and install them all. Will help you if packages appear to be on Github.
4+
#' and install them all. Will help you if packages appear to be on GitHub.
55
#'
66
#' @param dir Directory to search for packages in. Defaults to the blog post location of Hugo.
77
#'

R/render_new_rmds_to_md.R

+46-11
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,57 @@
66
#' preview option in Rstudio to quickly see how your md has worked.
77
#'
88
#' @param dir Directory to search for new Rmds in. Defaults to the blog post location of Hugo.
9+
#' @param build Selection criteria for which Rmds to convert to md, options are c("all", "old and new", "old", "new")
910
#'
1011
#' @return Used for pure side effect
1112
#' @export
12-
render_new_rmds_to_md <- function(dir = "content/post") {
13+
render_new_rmds_to_md <- function(dir = "content/post",
14+
build = "old and new") {
15+
match.arg(build, c("all", "old and new", "old", "new"))
1316
content = dir
14-
# Rmds not rendered
15-
rmds <- list.files(content, pattern = "\\.Rmd")
16-
mds <- list.files(content, pattern = "\\.md")
1717

18-
to_build <- setdiff(tools::file_path_sans_ext(rmds),
19-
tools::file_path_sans_ext(mds))
18+
# get info about all files
19+
info <- fs::dir_info(content)
20+
rmds <- info[stringr::str_detect(info$path, "\\.[Rr]md"),]
21+
mds <- info[stringr::str_detect(info$path, "\\.md"),]
2022

21-
for (b in to_build) {
22-
rmd = file.path(content, paste0(b, ".Rmd"))
23-
rmarkdown::render(rmd,
24-
rmarkdown::md_document(variant = "markdown_github",
25-
preserve_yaml = TRUE))
23+
rmds$slug <- fs::path_ext_remove(rmds$path)
24+
mds$slug <- fs::path_ext_remove(mds$path)
25+
26+
# Rmd without md
27+
unbuilt <- rmds$path[!rmds$slug %in% mds$slug]
28+
29+
# Rmd with a too old md
30+
dplyr::left_join(rmds, mds, by = "slug",
31+
suffix = c("_rmd", "_md")) %>%
32+
dplyr::filter(.data$change_time_md < .data$change_time_rmd) %>%
33+
dplyr::pull(.data$path_rmd) -> too_old
34+
35+
if(build == "all"){
36+
to_build <- rmds$path_rmd
37+
}
38+
if(build == "old and new"){
39+
to_build <- c(too_old, unbuilt)
40+
}
41+
if(build == "old"){
42+
to_build <- too_old
43+
}
44+
if(build == "new"){
45+
to_build <- unbuilt
2646
}
47+
48+
# build only the ones to be built
49+
if(length(to_build) > 0){
50+
51+
for (b in to_build) {
52+
rmd <- b
53+
rmarkdown::render(rmd,
54+
rmarkdown::md_document(variant = "markdown_github",
55+
preserve_yaml = TRUE ))
56+
}
57+
}else{
58+
message("Nothing to build, all .md up-to-date")
59+
}
60+
61+
2762
}

R/save_and_use_widget.R

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ save_and_use_widget <- function(x, filename) {
1414
dir.create("widgets", showWarnings = FALSE)
1515
origwd <- getwd()
1616
setwd("widgets")
17-
f = paste0(name, ".html")
18-
htmlwidget::saveWidget(x, f, selfcontained = TRUE)
17+
htmlwidgets::saveWidget(x, filename, selfcontained = TRUE)
1918
setwd(origwd)
20-
htmltools::tags$iframe(src = file.path("widgets", f),
19+
htmltools::tags$iframe(src = file.path("widgets", filename),
2120
width = "100%",
2221
height = "500px")
2322
}

R/utils-pipe.R

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#' Pipe operator
2+
#'
3+
#' See \code{magrittr::\link[magrittr]{\%>\%}} for details.
4+
#'
5+
#' @name %>%
6+
#' @rdname pipe
7+
#' @keywords internal
8+
#' @export
9+
#' @importFrom magrittr %>%
10+
#' @importFrom rlang .data
11+
#' @usage lhs \%>\% rhs
12+
NULL

0 commit comments

Comments
 (0)