Skip to content

Commit

Permalink
Merge pull request #559 from olivroy/check-installed
Browse files Browse the repository at this point in the history
Use `check_installed()` + drop crayon from Suggests
  • Loading branch information
rich-iannone authored Aug 6, 2024
2 parents 82ee45e + 82c37ff commit a147ccb
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 148 deletions.
2 changes: 0 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ Imports:
Suggests:
arrow,
bigrquery,
covr,
crayon,
data.table,
duckdb,
ggforce,
Expand Down
16 changes: 4 additions & 12 deletions R/datasets.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,10 @@
small_table_sqlite <- function() {

# nocov start

if (!requireNamespace("DBI", quietly = TRUE) &&
!requireNamespace("RSQLite", quietly = TRUE)) {

stop(
"Creating the SQLite table object requires both the DBI and RSQLite ",
"packages:\n",
"* Install them with `install.packages(\"DBI\")` and ",
"`install.packages(\"RSQLite\")`.",
call. = FALSE
)
}
rlang::check_installed(
c("DBI", "RSQLite"),
"to create an SQLite table object."
)

con <-
DBI::dbConnect(
Expand Down
9 changes: 1 addition & 8 deletions R/logging.R
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,7 @@ log4r_step <- function(
append_to = "pb_log_file"
) {

if (!requireNamespace("log4r", quietly = TRUE)) {

stop(
"Using the `log4r_step()` function requires the log4r package:\n",
"* It can be installed with `install.packages(\"log4r\")`.",
call. = FALSE
)
}
rlang::check_installed("log4r", "to use the `log4r_step()` function.")

type <- x$this_type
warn_val <- x$warn
Expand Down
24 changes: 4 additions & 20 deletions R/scan_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -221,26 +221,10 @@ scan_data <- function(
}

if (any(c("interactions", "correlations") %in% sections)) {

if (!requireNamespace("ggplot2", quietly = TRUE)) {

stop(
"The `interactions` and `correlations` sections require ",
"the ggplot2 package:\n",
"* It can be installed with `install.packages(\"ggplot2\")`.",
call. = FALSE
)
}

if (!requireNamespace("ggforce", quietly = TRUE)) {

stop(
"The `interactions` and `correlations` sections require ",
"the ggforce package:\n",
"* It can be installed with `install.packages(\"ggforce\")`.",
call. = FALSE
)
}
rlang::check_installed(
c("ggforce", "ggplot2"),
"to use the `interactions` and `correlations` sections."
)
}

# Normalize the reporting language identifier and stop if necessary
Expand Down
26 changes: 2 additions & 24 deletions R/table_transformers.R
Original file line number Diff line number Diff line change
Expand Up @@ -546,18 +546,7 @@ tt_time_shift <- function(
time_shift = "0y 0m 0d 0H 0M 0S"
) {

# nocov start

if (!requireNamespace("lubridate", quietly = TRUE)) {

stop(
"The `tt_time_shift()` function requires the lubridate package:\n",
"* It can be installed with `install.packages(\"lubridate\")`.",
call. = FALSE
)
}

# nocov end
rlang::check_installed("lubridate", "to use the `tt_time_shift()` function.")

# Determine whether the `tbl` object is acceptable here
check_is_a_table_object(tbl = tbl)
Expand Down Expand Up @@ -760,18 +749,7 @@ tt_time_slice <- function(
arrange = FALSE
) {

# nocov start

if (!requireNamespace("lubridate", quietly = TRUE)) {

stop(
"The `tt_time_shift()` function requires the lubridate package:\n",
"* It can be installed with `install.packages(\"lubridate\")`.",
call. = FALSE
)
}

# nocov end
rlang::check_installed("lubridate", "to use the `tt_time_slice()` function.")

keep <- match.arg(keep)

Expand Down
64 changes: 6 additions & 58 deletions R/tbl_from_db.R
Original file line number Diff line number Diff line change
Expand Up @@ -368,14 +368,7 @@ db_tbl <- function(
dbname <- bq_project
}

if (!requireNamespace("DBI", quietly = TRUE)) {

stop(
"Accessing a database table requires the DBI package:\n",
"* It can be installed with `install.packages(\"DBI\")`.",
call. = FALSE
)
}
rlang::check_installed("DBI", "to access a database table.")

if (is.character(dbtype)) {

Expand Down Expand Up @@ -523,72 +516,27 @@ db_tbl <- function(
# nolint start

RPostgres_driver <- function() {

if (!requireNamespace("RPostgres", quietly = TRUE)) {

stop(
"Accessing a PostgreSQL table requires the RPostgres package:\n",
"* It can be installed with `install.packages(\"RPostgres\")`.",
call. = FALSE
)
}

rlang::check_installed("RPostgres", "to access a PostgreSQL table.")
RPostgres::Postgres()
}

RMySQL_driver <- function() {

if (!requireNamespace("RMySQL", quietly = TRUE)) {

stop(
"Accessing a MariaDB or MySQL table requires the RMySQL package:\n",
"* It can be installed with `install.packages(\"RMySQL\")`.",
call. = FALSE
)
}

rlang::check_installed("RMySQL", "to access a MariaDB or MySQL table.")
RMySQL::MySQL()
}

bigrquery_driver <- function() {

if (!requireNamespace("bigrquery", quietly = TRUE)) {

stop(
"Accessing a BigQuery table requires the bigrquery package:\n",
"* It can be installed with `install.packages(\"bigrquery\")`.",
call. = FALSE
)
}

rlang::check_installed("bigquery", "to access a BigQuery table.")
bigrquery::bigquery()
}

DuckDB_driver <- function() {

if (!requireNamespace("duckdb", quietly = TRUE)) {

stop(
"Accessing a DuckDB table requires the duckdb package:\n",
"* It can be installed with `install.packages(\"duckdb\")`.",
call. = FALSE
)
}

rlang::check_installed("duckdb", "to access a DuckDB table.")
duckdb::duckdb()
}

RSQLite_driver <- function() {

if (!requireNamespace("RSQLite", quietly = TRUE)) {

stop(
"Accessing a SQLite table requires the RSQLite package:\n",
"* It can be installed with `install.packages(\"RSQLite\")`.",
call. = FALSE
)
}

rlang::check_installed("RSQLite", "to access a SQLite table.")
RSQLite::SQLite()
}

Expand Down
16 changes: 2 additions & 14 deletions R/tbl_from_file.R
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,7 @@ file_tbl <- function(
verify = TRUE
) {

if (!requireNamespace("readr", quietly = TRUE)) {
stop(
"Reading a table from a file requires the readr package:\n",
" * It can be installed with `install.packages(\"readr\")`.",
call. = FALSE
)
}
rlang::check_installed("readr", "to read a table from a file.")

file_extension <- tolower(tools::file_ext(file))
file_name <- basename(file)
Expand Down Expand Up @@ -529,13 +523,7 @@ from_github <- function(

} else if (grepl("#", repo, fixed = TRUE)) {

if (!requireNamespace("jsonlite", quietly = TRUE)) {
stop(
"Getting a table from a file in a PR requires the jsonlite package:\n",
" * It can be installed with `install.packages(\"jsonlite\")`.",
call. = FALSE
)
}
rlang::check_installed("jsonlite", "to get a table from a file in a PR.")

pr_number <- unlist(strsplit(repo, "#"))[2]
pulls_doc_tempfile <- tempfile(pattern = "pulls", fileext = ".json")
Expand Down
4 changes: 2 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -1611,8 +1611,8 @@ cli_bullet_msg <- function(
msg <- glue::glue_collapse(msg, "\n")
msg <- glue::glue(msg, .envir = .envir)

if (!is.null(color) && requireNamespace("crayon", quietly = TRUE)) {
color_style <- crayon::make_style(color)
if (!is.null(color)) {
color_style <- cli::make_ansi_style(color)
bullet <- color_style(bullet)
}

Expand Down
9 changes: 1 addition & 8 deletions R/validate_rmd.R
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,7 @@ validate_rmd <- function(

if (test_options$perform_logging) {

if (!requireNamespace("log4r", quietly = TRUE)) {

stop(
"Using the `log4r_step()` function requires the log4r package:\n",
"* It can be installed with `install.packages(\"log4r\")`.",
call. = FALSE
)
}
rlang::check_installed("log4r", "to use the `log4r_step()` function.")

# Create a log4r `logger` object and store it in `test_options`
test_options$logger <-
Expand Down

0 comments on commit a147ccb

Please sign in to comment.