Skip to content

Commit

Permalink
fix memoisation for offline use
Browse files Browse the repository at this point in the history
  • Loading branch information
equitable-equations committed Aug 21, 2023
1 parent e015133 commit 23f964a
Show file tree
Hide file tree
Showing 10 changed files with 341 additions and 210 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ importFrom(ggplot2,labs)
importFrom(ggplot2,scale_x_continuous)
importFrom(ggplot2,theme_minimal)
importFrom(memoise,drop_cache)
importFrom(memoise,forget)
importFrom(memoise,has_cache)
importFrom(memoise,memoise)
importFrom(rlang,.data)
Expand Down
8 changes: 6 additions & 2 deletions R/download_database_internal.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@

download_database_internal <- memoise::memoise(function(database_id) {
if (!is.numeric(database_id)) {
stop("database_id must be an integer.", call. = FALSE)
stop("database_id must be an integer.",
call. = FALSE
)
}
if (database_id %% 1 != 0) {
stop("database_id must be an integer.", call. = FALSE)
stop("database_id must be an integer.",
call. = FALSE
)
}
if (database_id == -40000) {
return(invisible(NULL))
Expand Down
58 changes: 7 additions & 51 deletions R/download_transect.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
#' overview, and \code{\link[=transect_inventory]{transect_inventory()}} for
#' species-level data.
#'
#' @import jsonlite httr
#' @importFrom memoise memoise
#' @importFrom memoise drop_cache
#'
#' @examples
#' \donttest{
Expand All @@ -33,55 +32,12 @@
#' @export


download_transect <- memoise::memoise(function(transect_id) {
if (!is.numeric(transect_id)) {
stop("transect_id must be an integer.", call. = FALSE)
download_transect <- function(transect_id) {
out <- download_transect_internal(transect_id)
if (is.null(out)){
memoise::drop_cache(download_transect_internal)({{ transect_id }})
}
if (transect_id %% 1 != 0) {
stop("transect_id must be an integer.", call. = FALSE)
}

trans_address <-
paste0("http://universalfqa.org/get/transect/", transect_id)
ua <-
httr::user_agent("https://github.com/equitable-equations/fqar")

trans_get <- tryCatch(httr::GET(trans_address, ua),
error = function(e){
message("Unable to connect. Please check internet connection.")
return(invisible(NULL))
}
)
if (httr::http_error(trans_get)) {
message(
paste(
"API request to universalFQA.org failed. Error",
httr::status_code(assessments_get)
)
)
return(invisible(NULL))
}

trans_text <- httr::content(trans_get,
"text",
encoding = "ISO-8859-1")
trans_json <- jsonlite::fromJSON(trans_text)
list_data <- trans_json[[2]]

if ((list_data[[1]] == "The requested assessment is not public") &
(!is.na(list_data[[1]]))) {
stop("The requested assessment is not public", call. = FALSE)
}

max_length <-
max(unlist(lapply(list_data, length))) # determines how wide the df must be
list_data <- lapply(list_data,
function(x) {
length(x) <- max_length
unlist(x)
})

as.data.frame(do.call(rbind, list_data))
out
}

})

73 changes: 73 additions & 0 deletions R/download_transect_internal.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#' Download a single floristic quality transect assessment with possible null result cached
#'
#' @param transect_id A numeric identifier of the desired floristic quality
#' transect assessment
#'
#' @return An untidy data frame in the original format of the Universal FQA
#' website.
#'
#' @import httr jsonlite
#' @importFrom memoise memoise
#'
#' @noRd

download_transect <- memoise::memoise(function(transect_id) {
if (!is.numeric(transect_id)) {
stop("transect_id must be an integer.", call. = FALSE)
}
if (transect_id %% 1 != 0) {
stop("transect_id must be an integer.", call. = FALSE)
}
if (transect_id == -40000) {
return(invisible(NULL))
} # for testing memoisation

trans_address <-
paste0("http://universalfqa.org/get/transect/", transect_id)
ua <-
httr::user_agent("https://github.com/equitable-equations/fqar")

trans_get <- tryCatch(httr::GET(trans_address, ua),
error = function(e){
message("Unable to connect. Please check internet connection.")
character(0)
}
)

cl <- class(trans_get)
if (cl != "response"){
return(invisible(NULL))
}

if (httr::http_error(trans_get)) {
message(
paste(
"API request to universalFQA.org failed. Error",
httr::status_code(assessments_get)
)
)
return(invisible(NULL))
}

trans_text <- httr::content(trans_get,
"text",
encoding = "ISO-8859-1")
trans_json <- jsonlite::fromJSON(trans_text)
list_data <- trans_json[[2]]

if ((list_data[[1]] == "The requested assessment is not public") &
(!is.na(list_data[[1]]))) {
stop("The requested assessment is not public", call. = FALSE)
}

max_length <-
max(unlist(lapply(list_data, length))) # determines how wide the df must be
list_data <- lapply(list_data,
function(x) {
length(x) <- max_length
unlist(x)
})

as.data.frame(do.call(rbind, list_data))

})
67 changes: 7 additions & 60 deletions R/index_fqa_assessments.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
#' \item practitioner (character)
#' }
#'
#' @import jsonlite httr
#' @importFrom memoise memoise
#' @importFrom memoise drop_cache
#'
#' @examples
#' \donttest{
Expand All @@ -31,66 +30,14 @@
#' @export


index_fqa_assessments <- memoise(function(database_id) {
if (!is.numeric(database_id)) {
stop(
"database_id must be an integer corresponding to an existing FQA database. Use index_fqa_databases() to obtain a data frame of valid options.",
call. = FALSE
)
index_fqa_assessments <- function(database_id) {
out <- index_fqa_assessments_internal(database_id)
if (is.null(out)){
memoise::drop_cache(index_fqa_assessments_internal)({{ database_id }})
}
if (database_id %% 1 != 0) {
stop(
"database_id must be an integer corresponding to an existing FQA database. Use index_fqa_databases() to obtain a data frame of valid options.",
call. = FALSE
)
}

assessments_address <-
paste0("http://universalfqa.org/get/database/",
database_id,
"/inventory")
ua <-
httr::user_agent("https://github.com/equitable-equations/fqar")

assessments_get <- tryCatch(httr::GET(assessments_address, ua),
error = function(e){
message("Unable to connect. Please check internet connection.")
return(invisible(NULL))
}
)
if (httr::http_error(assessments_get)) {
message(
paste(
"API request to universalFQA.org failed. Error",
httr::status_code(assessments_get)
)
)
return(invisible(NULL))
}

assessments_text <- httr::content(assessments_get,
"text",
encoding = "ISO-8859-1")
assessments_json <- jsonlite::fromJSON(assessments_text)
list_data <- assessments_json[[2]]
out
}

inventories_summary <- as.data.frame(list_data)

if (nrow(inventories_summary) == 0) {
stop("no data associated with specified database_id.", call. = FALSE)
}

colnames(inventories_summary) <- c("id", "assessment",
"date",
"site",
"practitioner")
inventories_summary$id <- as.double(inventories_summary$id)
inventories_summary$date[inventories_summary$date == "0000-00-00"] <-
NA
inventories_summary$date <- as.Date(inventories_summary$date)
class(inventories_summary) <- c("tbl_df",
"tbl",
"data.frame")

inventories_summary
})
89 changes: 89 additions & 0 deletions R/index_fqa_assessments_internal.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#' List all available public floristic quality assessments with possible null
#' results cached
#'
#' @param database_id A numeric identifier of the desired database
#' @return A data frame with 5 columns
#'
#' @import jsonlite httr
#' @importFrom memoise memoise
#'
#' @noRd


index_fqa_assessments_internal <- memoise::memoise(function(database_id) {
if (!is.numeric(database_id)) {
stop(
"database_id must be an integer corresponding to an existing FQA database. Use index_fqa_databases() to obtain a data frame of valid options.",
call. = FALSE
)
}
if (database_id %% 1 != 0) {
stop(
"database_id must be an integer corresponding to an existing FQA database. Use index_fqa_databases() to obtain a data frame of valid options.",
call. = FALSE
)
}
if (database_id == -40000) {
return(invisible(NULL))
} # for testing memoisation

assessments_address <-
paste0("http://universalfqa.org/get/database/",
database_id,
"/inventory")
ua <-
httr::user_agent("https://github.com/equitable-equations/fqar")

assessments_get <- tryCatch(httr::GET(assessments_address, ua),
error = function(e){
message("Unable to connect. Please check internet connection.")
character(0)
}
)

cl <- class(assessments_get)
if (cl != "response"){
return(invisible(NULL))
}

if (httr::http_error(assessments_get)) {
message(
paste(
"API request to universalFQA.org failed. Error",
httr::status_code(assessments_get)
)
)
return(invisible(NULL))
}

assessments_text <- httr::content(assessments_get,
"text",
encoding = "ISO-8859-1")
assessments_json <- jsonlite::fromJSON(assessments_text)
list_data <- assessments_json[[2]]

inventories_summary <- as.data.frame(list_data)

if (nrow(inventories_summary) == 0) {
stop("no data associated with specified database_id.", call. = FALSE)
}

colnames(inventories_summary) <- c("id", "assessment",
"date",
"site",
"practitioner")
inventories_summary$id <- as.double(inventories_summary$id)
inventories_summary$date[inventories_summary$date == "0000-00-00"] <-
NA
inventories_summary$date <- as.Date(inventories_summary$date)
class(inventories_summary) <- c("tbl_df",
"tbl",
"data.frame")

inventories_summary
})





Loading

0 comments on commit 23f964a

Please sign in to comment.