-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rprofile
126 lines (109 loc) · 5.13 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
assign(".Rprofile", new.env(), envir = globalenv())
# .First ------------------------------------------------------------------
.First <- function(){
try(if(testthat::is_testing()) return())
try(readRenviron(".Renviron"), silent = TRUE)
# 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)
}
# .Last -------------------------------------------------------------------
.Last <- function(){
try(if(testthat::is_testing()) return())
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/utils-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/utils-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/utils-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/utils-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/utils-DockerCompose.R'); DockerCompose$new()$reset()"), path_script)
.Rprofile$utils$run_script(path_script, job_name)
}
# pkgdown -----------------------------------------------------------------
.Rprofile$pkgdown$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$create <- function(){
path_script <- tempfile("system-", fileext = ".R")
job_name <- "Rendering Package Website"
writeLines(c(
"devtools::document()",
"rmarkdown::render('README.Rmd', 'md_document')",
"unlink(usethis::proj_path('docs'), TRUE, TRUE)",
paste0("try(detach('package:",read.dcf("DESCRIPTION", "Package")[[1]], "', unload = TRUE, force = TRUE))"),
"pkgdown::build_site(devel = FALSE, lazy = FALSE)"
), path_script)
.Rprofile$utils$run_script(path_script, job_name)
}
.Rprofile$pkgdown$update <- function(){
path_script <- tempfile("system-", fileext = ".R")
job_name <- "Rendering Package Website"
writeLines(c(
"devtools::document()",
"rmarkdown::render('README.Rmd', 'md_document')",
paste0("try(detach('package:",read.dcf("DESCRIPTION", "Package")[[1]], "', unload = TRUE, force = TRUE))"),
"pkgdown::build_site(devel = TRUE, lazy = TRUE)"
), 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()
}