Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement download_model() using {httr2} and add tests #885

Merged
merged 5 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,19 @@ Description: A tool to provide an easy, intuitive and consistent
used to fit the model or data from response variables. 'insight'
mainly revolves around two types of functions: Functions that find
(the names of) information, starting with 'find_', and functions that
get the underlying data, starting with 'get_'. The package has a
get the underlying data, starting with 'get_'. The package has a
consistent syntax and works with many different model objects, where
otherwise functions to access these information are missing.
License: GPL-3
URL: https://easystats.github.io/insight/
BugReports: https://github.com/easystats/insight/issues
Depends:
Depends:
R (>= 3.6)
Imports:
Imports:
methods,
stats,
utils
Suggests:
Suggests:
AER,
afex,
aod,
Expand Down Expand Up @@ -129,7 +129,8 @@ Suggests:
gmnl,
grDevices,
gt,
httr,
httptest2,
httr2,
interp,
ivreg,
JM,
Expand All @@ -142,8 +143,8 @@ Suggests:
lmtest,
logistf,
logitr,
MASS,
marginaleffects,
MASS,
Matrix,
mclogit,
mclust,
Expand Down Expand Up @@ -179,9 +180,9 @@ Suggests:
Rcpp,
rmarkdown,
rms,
rpart,
robustbase,
robustlmm,
rpart,
rstanarm (>= 2.21.1),
rstantools (>= 2.1.0),
rstudioapi,
Expand All @@ -194,9 +195,9 @@ Suggests:
survival,
testthat,
tinytable (>= 0.1.0),
TMB,
truncreg,
tweedie,
TMB,
VGAM,
withr
VignetteBuilder:
Expand All @@ -207,7 +208,4 @@ RoxygenNote: 7.3.1
Roxygen: list(markdown = TRUE)
Config/testthat/edition: 3
Config/testthat/parallel: true
Config/Needs/website:
rstudio/bslib,
r-lib/pkgdown,
easystats/easystatstemplate
Config/Needs/website: easystats/easystatstemplate
46 changes: 27 additions & 19 deletions R/download_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#' Optional, and should only be used in case the repository-URL is
#' changing. By default, models are downloaded from
#' `https://raw.github.com/easystats/circus/master/data/`.
#' @param extension File extension. Default is `.rda`.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case we ever start saving data in some other format.

#' @param verbose Toggle messages and warnings.
#'
#' @return A model from the *circus*-repository, or `NULL` if model could
#' not be downloaded (e.g., due to server problems).
Expand All @@ -19,43 +21,49 @@
#'
#' @references <https://easystats.github.io/circus/>
#'
#' @examplesIf require("httr2", quietly = TRUE) && curl::has_internet() && interactive()
#' \donttest{
#' download_model("aov_1")
#' try(download_model("non_existent_model"))
#' }
#'
#' @export
download_model <- function(name, url = NULL) {
.download_data_github(name, url)
download_model <- function(name,
url = "https://raw.github.com/easystats/circus/master/data/",
extension = ".rda",
verbose = TRUE) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verbosity is useful here to control output in tests.

.download_data_github(name, url, extension, verbose)
}


# Download rda files from github
.download_data_github <- function(name, url) {
check_if_installed("httr", "to download models from the circus-repo")

if (is.null(url)) {
url <- "https://raw.github.com/easystats/circus/master/data/"
}
.download_data_github <- function(name, url, extension = ".rda", verbose = TRUE) {
check_if_installed("httr2", "to download models from the circus-repo")

url <- paste0(url, name, ".rda")
url <- paste0(url, name, extension)
req <- httr2::request(url)

temp_file <- tempfile()
on.exit(unlink(temp_file))

result <- tryCatch(
res <- tryCatch(
{
request <- httr::GET(url)
httr::stop_for_status(request)
httr2::req_perform(req, verbosity = 0L)
},
error = function(e) {
format_alert(
"Could not download model. Request failed with following error:",
e$message
)
if (verbose) {
format_alert(
"Could not download model. Request failed with following error:",
e$message
)
}
NULL
}
)
if (is.null(result)) {
if (is.null(res)) {
return(NULL)
}

writeBin(httr::content(request, type = "raw"), temp_file)
writeBin(httr2::resp_body_raw(res), temp_file)

x <- load(temp_file)
model <- get(x)
Expand Down
19 changes: 18 additions & 1 deletion man/download_model.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# nolint start
# styler: off
structure(list(
method = "GET", url = "https://raw.githubusercontent.com/easystats/circus/master/data/xyz.rda",
status_code = 404L, headers = structure(list(
`content-security-policy` = "default-src 'none'; style-src 'unsafe-inline'; sandbox",
`strict-transport-security` = "max-age=31536000", `x-content-type-options` = "nosniff",
`x-frame-options` = "deny", `x-xss-protection` = "1; mode=block",
`content-type` = "text/plain; charset=utf-8", `x-github-request-id` = "63EC:2A5B9F:A27173:ADAE50:66655C62",
`accept-ranges` = "bytes", date = "Sun, 09 Jun 2024 07:40:19 GMT",
via = "1.1 varnish", `x-served-by` = "cache-muc13972-MUC",
`x-cache` = "MISS", `x-cache-hits` = "0", `x-timer` = "S1717918819.042740,VS0,VE145",
vary = "Authorization,Accept-Encoding,Origin", `access-control-allow-origin` = "*",
`cross-origin-resource-policy` = "cross-origin", `x-fastly-request-id` = "e629ba1c490defdad14a749303c5f1649289f72c",
expires = "Sun, 09 Jun 2024 07:45:19 GMT", `source-age` = "0",
`content-length` = "14"
), class = "httr2_headers"), body = charToRaw("404: Not Found"),
cache = new.env(parent = emptyenv())
), class = "httr2_response")
# styler: on
# nolint end
Loading
Loading