generated from tidylab/template.package
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rprofile
154 lines (132 loc) · 5.91 KB
/
.Rprofile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
assign(".Rprofile", new.env(), envir = globalenv())
# .First ------------------------------------------------------------------
.First <- function(){
try(if(testthat::is_testing()) return())
# Package Management System
Date <- as.character(read.dcf("DESCRIPTION", "Date"));
URL <- if(is.na(Date)) "https://cran.rstudio.com/" else paste0("https://mran.microsoft.com/snapshot/", Date)
options(repos = URL)
suppressMessages(try({renv::consent(provided = TRUE); unlink("./renv")}))
options(
renv.lockfile = "renv.lock",
renv.consent = TRUE,
renv.clean = FALSE,
renv.settings = list(
ignored.packages = c("renv"),
snapshot.type = "explicit",
auto.snapshot = FALSE,
package.dependency.fields = c("Imports", "Depends", "LinkingTo", "Suggests")[1:3],
vcs.ignore.library = TRUE,
use.cache = TRUE
)
)
# Programming Logic
pkgs <- c("usethis", "devtools", "magrittr", "testthat")
invisible(sapply(pkgs, require, warn.conflicts = FALSE, character.only = TRUE))
}
# .Last -------------------------------------------------------------------
.Last <- function(){
try(if(testthat::is_testing()) return())
unlink("./renv", recursive = TRUE)
try(system('docker-compose down'), silent = TRUE)
}
# Docker ------------------------------------------------------------------
.Rprofile$docker$browse_url <- function(service){
path_script <- tempfile("system-", fileext = ".R")
job_name <- paste("Testing", as.character(read.dcf('DESCRIPTION', 'Package')), "in a Docker Container")
define_service <- paste0("service = c(", paste0(paste0("'",service,"'"), collapse = ", "),")")
define_service <- if(is.null(service)) "service = NULL" else define_service
writeLines(c(
"source('./R/docker-DockerCompose.R')",
define_service,
"DockerCompose$new()$browse_url(service)"), path_script)
.Rprofile$utils$run_script(path_script, job_name)
}
.Rprofile$docker$start <- function(service = NULL){
path_script <- tempfile("system-", fileext = ".R")
job_name <- paste("Testing", as.character(read.dcf('DESCRIPTION', 'Package')), "in a Docker Container")
define_service <- paste0("service <- c(", paste0(paste0("'",service,"'"), collapse = ", "),")")
define_service <- if(is.null(service)) "service = NULL" else define_service
writeLines(c(
"source('./R/docker-DockerCompose.R')",
define_service,
"DockerCompose$new()$start(service)"), path_script)
.Rprofile$utils$run_script(path_script, job_name)
}
.Rprofile$docker$stop <- function(){
path_script <- tempfile("system-", fileext = ".R")
job_name <- paste("Testing", as.character(read.dcf('DESCRIPTION', 'Package')), "in a Docker Container")
writeLines(c("source('./R/docker-DockerCompose.R'); DockerCompose$new()$stop()"), path_script)
.Rprofile$utils$run_script(path_script, job_name)
}
.Rprofile$docker$restart <- function(service = NULL){
path_script <- tempfile("system-", fileext = ".R")
job_name <- paste("Testing", as.character(read.dcf('DESCRIPTION', 'Package')), "in a Docker Container")
define_service <- paste0("service <- c(", paste0(paste0("'",service,"'"), collapse = ", "),")")
define_service <- if(is.null(service)) "service = NULL" else define_service
writeLines(c(
"source('./R/docker-DockerCompose.R')",
define_service,
"DockerCompose$new()$restart(service)"), path_script)
.Rprofile$utils$run_script(path_script, job_name)
}
.Rprofile$docker$reset <- function(){
path_script <- tempfile("system-", fileext = ".R")
job_name <- paste("Testing", as.character(read.dcf('DESCRIPTION', 'Package')), "in a Docker Container")
writeLines(c("source('./R/docker-DockerCompose.R'); DockerCompose$new()$reset()"), path_script)
.Rprofile$utils$run_script(path_script, job_name)
}
# pkgdown -----------------------------------------------------------------
.Rprofile$pkgdown$site$browse <- function(name){
if(missing(name)){
path <- "./docs"
name <- "index.html"
} else {
path <- "./docs/articles"
name <- match.arg(name, list.files(path, "*.html"))
}
try(browseURL(stringr::str_glue('{path}/{name}', path = path, name = name)))
invisible()
}
.Rprofile$pkgdown$site$create <- function(){
path_script <- tempfile("system-", fileext = ".R")
job_name <- "Rendering Package Website"
writeLines(c(
"unlink(usethis::proj_path('docs'), TRUE, TRUE)",
"pkgdown::build_site(devel = FALSE, lazy = FALSE)"
), path_script)
.Rprofile$utils$run_script(path_script, job_name)
}
.Rprofile$pkgdown$site$update <- function(){
path_script <- tempfile("system-", fileext = ".R")
job_name <- "Rendering Package Website"
writeLines(c(
"devtools::document()",
"pkgdown::build_site(devel = TRUE, lazy = TRUE)"
), path_script)
.Rprofile$utils$run_script(path_script, job_name)
}
.Rprofile$pkgdown$article$crate <- function(name){
name <- match.arg(name, list.files("./vignettes", "*.Rmd"))
name <- fs::path_ext_remove(name)
path_script <- tempfile("system-", fileext = ".R")
job_name <- "Rendering Package Article"
writeLines(
stringr::str_glue("pkgdown::build_article('{name}', lazy = FALSE, quiet = FALSE)", name = name),
path_script
)
.Rprofile$utils$run_script(path_script, job_name)
}
# Utils -------------------------------------------------------------------
.Rprofile$utils$run_script <- function(path, name){
withr::with_envvar(
c(TESTTHAT = "true"),
rstudioapi::jobRunScript(
path = path,
name = name,
workingDir = ".",
importEnv = FALSE,
exportEnv = ""
))
invisible()
}