Skip to content

Commit d1c9735

Browse files
- change options to follow teal.lockfile. pattern
- lockfile logic only in the server
1 parent debc4ce commit d1c9735

File tree

6 files changed

+45
-40
lines changed

6 files changed

+45
-40
lines changed

R/module_teal_lockfile.R

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
#'
88
#' - User-specified:
99
#' - **Pre-computed lockfile**: Users can provide their own pre-computed lockfile by specifying the path via
10-
#' `teal.renv.lockfile` option. Automatic lockfile computation is skipped in such case.
10+
#' `teal.lockfile.path` option. Automatic lockfile computation is skipped in such case.
1111
#' - Automatically computed:
12-
#' - **Working directory lockfile**: If `teal.renv.lockfile` is not set, `teal` will, by default, create an
12+
#' - **Working directory lockfile**: If `teal.lockfile.path` is not set, `teal` will, by default, create an
1313
#' `implicit` type lockfile that uses `renv::dependencies()` to detect all R packages in the current project's
1414
#' working directory.
1515
#' - **`DESCRIPTION`-based lockfile**: To generate a lockfile based on a `DESCRIPTION` file in your working
@@ -34,13 +34,6 @@ NULL
3434

3535
#' @rdname module_teal_lockfile
3636
ui_teal_lockfile <- function(id) {
37-
if (!isTRUE(getOption("teal.renv.enable"))) {
38-
return(NULL)
39-
} else if (!.is_lockfile_deps_installed()) {
40-
warning("Lockfile feature disabled. `mirai` and `renv` packages must be installed.")
41-
return(NULL)
42-
}
43-
4437
ns <- NS(id)
4538
shiny::tagList(
4639
tags$span("", id = ns("lockFileStatus")),
@@ -50,23 +43,43 @@ ui_teal_lockfile <- function(id) {
5043

5144
#' @rdname module_teal_lockfile
5245
srv_teal_lockfile <- function(id) {
53-
if (!isTRUE(getOption("teal.renv.enable")) || !.is_lockfile_deps_installed()) {
54-
return(NULL)
55-
}
5646
moduleServer(id, function(input, output, session) {
5747
logger::log_debug("Initialize srv_teal_lockfile.")
5848
enable_lockfile_download <- function() {
5949
shinyjs::html("lockFileStatus", "Application lockfile ready.")
6050
shinyjs::hide("lockFileStatus", anim = TRUE)
6151
shinyjs::enable("lockFileLink")
52+
output$lockFileLink <- shiny::downloadHandler(
53+
filename = function() {
54+
"renv.lock"
55+
},
56+
content = function(file) {
57+
file.copy(lockfile_path, file)
58+
file
59+
},
60+
contentType = "application/json"
61+
)
6262
}
6363
disable_lockfile_download <- function() {
6464
warning("Lockfile creation failed.", call. = FALSE)
6565
shinyjs::html("lockFileStatus", "Lockfile creation failed.")
6666
shinyjs::disable("lockFileLink")
6767
}
6868

69+
is_lockfile_enabled <- isTRUE(getOption("teal.lockfile.enable"))
70+
user_lockfile <- getOption("teal.lockfile.path", "")
6971
lockfile_path <- "teal_app.lock"
72+
73+
if (!is_lockfile_enabled) {
74+
logger::log_debug("'teal.lockfile.enable' option is set to false. Hiding a lockfile download button.")
75+
shinyjs::hide("lockFileLink")
76+
return(NULL)
77+
} else if (identical(user_lockfile, "") && !.is_lockfile_deps_installed()) {
78+
warning("Automatic lockfile creation disabled. `mirai` and `renv` packages must be installed.")
79+
shinyjs::hide("lockFileLink")
80+
return(NULL)
81+
}
82+
7083
shiny::onStop(function() {
7184
if (file.exists(lockfile_path) && !shiny::isRunning()) {
7285
logger::log_debug("Removing lockfile after shutting down the app")
@@ -81,16 +94,15 @@ srv_teal_lockfile <- function(id) {
8194
return(NULL)
8295
}
8396

97+
8498
# don't run renv::snapshot when option is set
85-
user_lockfile <- getOption("teal.renv.lockfile", "")
8699
if (!identical(user_lockfile, "")) {
87100
if (file.exists(user_lockfile)) {
88101
file.copy(user_lockfile, lockfile_path)
89-
logger::log_debug('Lockfile set using option "teal.renv.lockfile" - skipping automatic creation.')
102+
logger::log_debug('Lockfile set using option "teal.lockfile.path" - skipping automatic creation.')
90103
enable_lockfile_download()
91-
return(NULL)
92104
} else {
93-
warning("Lockfile provided through options('teal.renv.lockfile') does not exist.", call. = FALSE)
105+
warning("Lockfile provided through options('teal.lockfile.path') does not exist.", call. = FALSE)
94106
}
95107
}
96108

@@ -120,17 +132,6 @@ srv_teal_lockfile <- function(id) {
120132
}
121133
})
122134

123-
output$lockFileLink <- shiny::downloadHandler(
124-
filename = function() {
125-
"renv.lock"
126-
},
127-
content = function(file) {
128-
file.copy(lockfile_path, file)
129-
file
130-
},
131-
contentType = "application/json"
132-
)
133-
134135
NULL
135136
})
136137
}

R/zzz.R

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
# adapted from https://github.com/r-lib/devtools/blob/master/R/zzz.R
33
teal_default_options <- list(
44
teal.show_js_log = FALSE,
5-
# todo: make below teal.renv.enable = FALSE during run_examples, test_package, r cmd check
6-
# run_examples probably doesn't set any sysvars but maybe detecting run_examples in sys.calls() helps
7-
teal.renv.enable = TRUE,
5+
teal.lockfile.enable = TRUE,
86
shiny.sanitize.errors = FALSE
97
)
108

man/module_teal_lockfile.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/setup-options.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
withr::local_options(
22
# we should't run lockfile process multiple times in tests as it starts background process which is not
33
# possible to kill once run. It means that background R sessions are being cumulated
4-
list(teal.renv.enable = FALSE),
4+
list(teal.lockfile.enable = FALSE),
55
.local_envir = testthat::teardown_env()
66
)
77

@@ -14,7 +14,7 @@ opts_partial_match_old <- list(
1414
warnPartialMatchAttr = getOption("warnPartialMatchAttr")
1515
)
1616
opts_partial_match_new <- list(
17-
teal.renv.enable = FALSE,
17+
teal.lockfile.enable = FALSE,
1818
warnPartialMatchDollar = TRUE,
1919
warnPartialMatchArgs = TRUE,
2020
warnPartialMatchAttr = TRUE

tests/testthat/test-module_teal.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ transform_list <<- list(
6868
testthat::describe("srv_teal lockfile", {
6969
testthat::it("creation process is invoked and snapshot is copied to teal_app.lock and removed after session ended", {
7070
withr::with_options(
71-
list(teal.renv.enable = TRUE),
71+
list(teal.lockfile.enable = TRUE),
7272
{
7373
renv_filename <- "teal_app.lock"
7474
shiny::testServer(
@@ -91,13 +91,13 @@ testthat::describe("srv_teal lockfile", {
9191
}
9292
)
9393
})
94-
testthat::it("is copied from option-teal.renv.lockfile and then removed from an app directory", {
94+
testthat::it("is copied from option-teal.lockfile.path and then removed from an app directory", {
9595
temp_lockfile_mock <- tempfile()
9696
writeLines("test", temp_lockfile_mock)
9797
withr::with_options(
9898
list(
99-
teal.renv.enable = TRUE,
100-
teal.renv.lockfile = temp_lockfile_mock
99+
teal.lockfile.enable = TRUE,
100+
teal.lockfile.path = temp_lockfile_mock
101101
),
102102
{
103103
renv_filename <- "teal_app.lock"

vignettes/teal-options.Rmd

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,16 @@ This indicates whether to print the `JavaScript` console logs to the `R` console
8484

8585
Default: `FALSE`.
8686

87-
### `teal.renv.lockfile` (`character`)
87+
### `teal.lockfile.enable` (`logical`)
8888

89-
The path to the pre-computed `renv` lockfile that will be shared through teal app. To read more about lockfile usage creation check `?teal::teal_lockfile`.
89+
This enables app users to download `renv` lockfile by clicking "download lockfile" in the footer. If
90+
`teal.lockfile.path` is not specified then lockfile is created automatically by `teal`. To read more about
91+
lockfile usage creation check `?teal::teal_lockfile`.
9092

93+
### `teal.lockfile.path` (`character`)
94+
95+
The path to the pre-computed `renv` lockfile linked by app developer. Lockfile will be available to download if
96+
`teal.lockfile.enable` is set to `TRUE`.
9197

9298
# Deprecated options
9399

0 commit comments

Comments
 (0)