Skip to content

Commit 930f712

Browse files
committedFeb 27, 2025·
fix: examples sandbox
1 parent 0a32d58 commit 930f712

18 files changed

+160
-150
lines changed
 

‎DESCRIPTION

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Depends:
2626
Imports:
2727
cli,
2828
condathis,
29+
fs,
2930
httr2,
3031
janitor,
3132
pingr,

‎NAMESPACE

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ export(extr_pubchem_fema)
1313
export(extr_pubchem_ghs)
1414
export(extr_tetramer)
1515
export(extr_tox)
16-
export(with_extr_sandbox)
16+
export(with_sandbox_dir)
1717
export(write_dataframes_to_excel)

‎R/cache.R

+3-31
Original file line numberDiff line numberDiff line change
@@ -82,34 +82,6 @@ read_from_cache <- function(file_name, verbose = FALSE) {
8282
}
8383

8484

85-
#' Run Code in a Temporary Sandbox Environment
86-
#'
87-
#' This function creates a temporary directory and sets it as `R_USER_CACHE_DIR`
88-
#' before executing the provided code block. It is used for testing or running
89-
#' code without affecting the user's default cache directory as required by CRAN
90-
#' for the examples .This function is not designed to be used by package users.
91-
#' Shamelessly "inspired" by some @luciorq code.
92-
#' @param code The code to be executed inside the sandbox. Should be an
93-
#' expression.
94-
#' @param temp_dir A temporary directory created using `temdir()`.
95-
#' @return The result of the executed code.
96-
#' @keywords internal
97-
#' @examples
98-
#' with_extr_sandbox(Sys.getenv("R_USER_CACHE_DIR"))
99-
#' with_extr_sandbox(tools::R_user_dir("extractox", "cache"))
100-
with_extr_sandbox <- function(code, temp_dir = tempdir()) {
101-
if (base::missing(code)) {
102-
cli::cli_abort("The argument {.field {file_name}} is required.")
103-
}
104-
105-
withr::with_envvar(
106-
new = c("R_USER_CACHE_DIR" = temp_dir),
107-
code = {
108-
eval(substitute(code), envir = parent.frame())
109-
}
110-
)
111-
}
112-
11385
#' Execute Code in a Temporary Directory
11486
#'
11587
#' @description
@@ -118,8 +90,8 @@ with_extr_sandbox <- function(code, temp_dir = tempdir()) {
11890
#' and ensures that no data is written to the user's file space.
11991
#' Environment variables such as `HOME`, `APPDATA`, `R_USER_DATA_DIR`,
12092
#' `XDG_DATA_HOME`, `LOCALAPPDATA`, and `USERPROFILE` are redirected to
121-
#' temporary directories. This function was implemented by @luciorq in
122-
#' {condathis} dev
93+
#' temporary directories. This function was implemented by @luciorq in
94+
#' `condathis` dev.
12395
#'
12496
#' @details
12597
#' This function is not designed for direct use by package users. It is
@@ -182,4 +154,4 @@ with_sandbox_dir <- function(code, .local_envir = base::parent.frame()) {
182154
code <- base::substitute(expr = code)
183155
rlang::eval_bare(expr = code, env = .local_envir)
184156
return(invisible(NULL))
185-
}
157+
}

‎R/extr_pprtv.R

+15-14
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
#' Extract Data from EPA PPRTVs
22
#'
3-
#' Extracts data for specified identifiers (CASRN or chemical names) from the EPA's
4-
#' Provisional Peer-Reviewed Toxicity Values (PPRTVs) database. The function
5-
#' retrieves and processes data, with options to use cached files or force a
6-
#' fresh download.
3+
#' Extracts data for specified identifiers (CASRN or chemical names) from the
4+
#' EPA's Provisional Peer-Reviewed Toxicity Values (PPRTVs) database. The
5+
#' function retrieves and processes data, with options to use cached files
6+
#' or force a fresh download.
77
#'
88
#' @param ids Character vector of identifiers to search (e.g., CASRN or chemical
99
#' names).
10-
#' @param search_type Character string specifying the type of identifier: "casrn"
11-
#' or "name".
12-
#' Default is "casrn". If `search_type` is "name", the function performs a
13-
#' partial match search for the chemical name. NOTE: Since partial mached is
14-
#' use, multiple seraches might match the same chemical, therefore chemical ids
15-
#' might not be uniques.
10+
#' @param search_type Character string specifying the type of identifier:
11+
#' "casrn" or "name". Default is "casrn". If `search_type` is "name", the
12+
#' function performs a partial match search for the chemical name. NOTE:
13+
#' Since partial mached is use, multiple seraches might match the same
14+
#' chemical, therefore chemical ids might not be uniques.
1615
#' @param verbose Logical indicating whether to display progress messages.
1716
#' Default is TRUE.
1817
#' @param force Logical indicating whether to force a fresh download of the
@@ -22,27 +21,29 @@
2221
#' This is was introduced for debugging purposes.
2322
#' @return A data frame with extracted information matching the specified
2423
#' identifiers, or NULL if no matches are found.
25-
#' @seealso \href{https://www.epa.gov/pprtv/provisional-peer-reviewed-toxicity-values-pprtvs-assessments}{EPA PPRTVs}
24+
#' @seealso \href{https://www.epa.gov/pprtv/provisional-peer-reviewed-toxicity-values-pprtvs-assessments}{EPA PPRTVs} # nolint
2625
#' @export
2726
#' @examples
2827
#' \donttest{
29-
#' with_extr_sandbox({ # this is to write on tempdir as for CRAN policies
28+
#' condathis::with_sandbox_dir({ # this is to write on tempdir as for CRAN policies # nolint
3029
#' # Extract data for a specific CASRN
3130
#' extr_pprtv(ids = "107-02-8", search_type = "casrn", verbose = TRUE)
3231
#'
3332
#' # Extract data for a chemical name
34-
#' extr_pprtv(
33+
#' out <- extr_pprtv(
3534
#' ids = "Acrolein", search_type = "name", verbose = TRUE,
3635
#' force = FALSE
3736
#' )
37+
#' print(out)
3838
#'
3939
#' # Extract data for multiple identifiers
40-
#' extr_pprtv(
40+
#' out2 <- extr_pprtv(
4141
#' ids = c("107-02-8", "79-10-7", "42576-02-3"),
4242
#' search_type = "casrn",
4343
#' verbose = TRUE,
4444
#' force = FALSE
4545
#' )
46+
#' print(out2)
4647
#' })
4748
#' }
4849
extr_pprtv <- function(ids, search_type = "casrn", verbose = TRUE, force = TRUE,

‎R/utils.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ download_db <- function(url,
5252

5353
if (isTRUE(check_need_libcurl_condathis())) {
5454
condathis_downgrade_libcurl()
55-
55+
5656
url_to_use <-
5757
paste0(
5858
url,
@@ -164,7 +164,7 @@ search_and_match <- function(dat,
164164
#'
165165
#' @param df_list A named list of dataframes to write to the Excel file.
166166
#' @param filename The name of the Excel file to create.
167-
#' @return No return value. The function prints a message indicating
167+
#' @return No return value. The function prints a message indicating
168168
#' the completion of the Excel file writing.
169169
#' @export
170170
#' @examples

‎data-raw/Untitled-1.R

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ temp_dir <- tempdir()
33

44
condathis::with_sandbox_dir({ # this is to write on tempdir as for CRAN policies
55

6-
with_extr_sandbox(temp_dir = temp_dir,
7-
# Extract data for a specific CASRN
8-
dat <- extr_pprtv(ids = "107-02-8", search_type = "casrn", verbose = TRUE, force = TRUE)
6+
with_extr_sandbox(
7+
temp_dir = temp_dir,
8+
# Extract data for a specific CASRN
9+
dat <- extr_pprtv(ids = "107-02-8", search_type = "casrn", verbose = TRUE, force = TRUE)
910
)
1011
})

‎docs/pkgdown.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ pandoc: 3.1.3
22
pkgdown: 2.1.1
33
pkgdown_sha: ~
44
articles: {}
5-
last_built: 2025-02-21T20:19Z
5+
last_built: 2025-02-27T04:17Z
66
urls:
77
reference: https://c1au6i0.github.io/extractox/reference
88
article: https://c1au6i0.github.io/extractox/articles

‎docs/reference/extr_casrn_from_cid.html

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎docs/reference/extr_comptox.html

+11-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎docs/reference/index.html

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎man/extr_pprtv.Rd

+15-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎man/search_and_match.Rd

+8-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎man/with_extr_sandbox.Rd

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎man/with_sandbox_dir.Rd

+39
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎man/write_dataframes_to_excel.Rd

+4-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/testthat/setup.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,4 @@ withr::local_envvar(
6060
`R_USER_CACHE_DIR` = tmp_cache_path
6161
),
6262
.local_envir = testthat::teardown_env()
63-
)
63+
)

‎tests/testthat/test-cache.R

+5-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ song <- c("bella", "ciao", "bella", "ciao", "ciao", "ciao")
1111
test_that("Save to cache works", {
1212
expect_message(
1313
{
14-
file_path <- save_to_cache(dat = song,
15-
file_name = "song.txt",
16-
verbose = TRUE)
14+
file_path <- save_to_cache(
15+
dat = song,
16+
file_name = "song.txt",
17+
verbose = TRUE
18+
)
1719
},
1820
"Saving"
1921
)

‎tests/testthat/test-pprtv.R

+45-57
Original file line numberDiff line numberDiff line change
@@ -24,68 +24,56 @@ test_that("extr_pprtv casrn hit and not hit, verbose, force = TRUE", {
2424
expect_equal(out$query, ids_search)
2525
})
2626

27-
# test_that("Function to warn with verbose = TRUE", {
28-
# skip_on_cran()
29-
# skip_if_offline()
30-
31-
# ids_search <- c("112-27-6", "bella", "ciao")
32-
# expect_warning(
33-
# {
34-
# with_extr_sandbox(
35-
# temp_dir = temp_dir,
36-
# out <- extr_pprtv(
37-
# ids = ids_search,
38-
# force = FALSE, verbose = TRUE
39-
# )
40-
# )
41-
# },
42-
# "Chemicals .* not found!"
43-
# )
27+
test_that("Function to warn with verbose = TRUE", {
28+
skip_on_cran()
29+
skip_if_offline()
4430

45-
# expect_equal(out$query, ids_search)
46-
# expect_equal(nrow(out), length(ids_search))
47-
# expect_true(is.na(out$casrn[[3]]))
48-
# })
31+
ids_search <- c("112-27-6", "bella", "ciao")
32+
expect_warning(
33+
{
34+
out <- extr_pprtv(
35+
ids = ids_search,
36+
force = FALSE, verbose = TRUE
37+
)
38+
},
39+
"Chemicals .* not found!"
40+
)
4941

50-
# test_that("Function verbose = FALSE", {
51-
# skip_on_cran()
52-
# skip_if_offline()
42+
expect_equal(out$query, ids_search)
43+
expect_equal(nrow(out), length(ids_search))
44+
expect_true(is.na(out$casrn[[3]]))
45+
})
5346

54-
# ids_search <- c("112-27-6", "98-86-2")
55-
# expect_silent({
56-
# with_extr_sandbox(
57-
# temp_dir = temp_dir,
58-
# out <- extr_pprtv(
59-
# ids = ids_search,
60-
# force = FALSE, verbose = FALSE
61-
# )
62-
# )
63-
# })
64-
# })
47+
test_that("Function verbose = FALSE", {
48+
skip_on_cran()
49+
skip_if_offline()
6550

66-
# test_that("extr_pprtv na,es hit and not hit, verbose, force = TRUE", {
67-
# skip_on_cran()
68-
# skip_if_offline()
51+
ids_search <- c("112-27-6", "98-86-2")
52+
expect_silent({
53+
out <- extr_pprtv(
54+
ids = ids_search,
55+
force = FALSE, verbose = FALSE
56+
)
57+
})
58+
})
6959

70-
# ids_search <- c("Ace", "Acetophenone")
60+
test_that("extr_pprtv na,es hit and not hit, verbose, force = TRUE", {
61+
skip_on_cran()
62+
skip_if_offline()
7163

72-
# expect_message(
73-
# {
74-
# with_extr_sandbox(
75-
# temp_dir = temp_dir,
76-
# out <- extr_pprtv(
77-
# ids = ids_search,
78-
# search_type = "name",
79-
# force = TRUE,
80-
# verbose = TRUE
81-
# )
82-
# )
83-
# },
84-
# "Extracting EPA PPRTVs."
85-
# )
64+
ids_search <- c("Ace", "Acetophenone")
8665

87-
# tmp_out <- file.path(temp_dir, "R", "extractox")
88-
# cache_exist <- file.exists(file.path(tmp_out, "epa_pprtvs.rds"))
66+
expect_message(
67+
{
68+
out <- extr_pprtv(
69+
ids = ids_search,
70+
search_type = "name",
71+
force = TRUE,
72+
verbose = TRUE
73+
)
74+
},
75+
"Extracting EPA PPRTVs."
76+
)
8977

90-
# expect_equal(nrow(out), 11)
91-
# })
78+
expect_equal(nrow(out), 11)
79+
})

0 commit comments

Comments
 (0)
Please sign in to comment.