Skip to content

Commit

Permalink
fix vroom problem
Browse files Browse the repository at this point in the history
The issue was that the uniprot data seems to now be gziped. I handle this case now in try_query. Not sure if it is generally handled for any potential case but it at least works for uniprot.
  • Loading branch information
jpquast committed Sep 29, 2024
1 parent 008c498 commit 151e795
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Imports:
methods,
R.utils,
stats
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Suggests:
testthat,
covr,
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ importFrom(httr,modify_url)
importFrom(httr,timeout)
importFrom(janitor,clean_names)
importFrom(janitor,make_clean_names)
importFrom(jsonlite,fromJSON)
importFrom(magrittr,"%>%")
importFrom(methods,is)
importFrom(plotly,ggplotly)
Expand All @@ -134,6 +135,7 @@ importFrom(purrr,pluck)
importFrom(purrr,pmap)
importFrom(purrr,reduce)
importFrom(purrr,set_names)
importFrom(readr,read_csv)
importFrom(readr,read_tsv)
importFrom(readr,write_csv)
importFrom(readr,write_tsv)
Expand Down Expand Up @@ -191,3 +193,5 @@ importFrom(utils,data)
importFrom(utils,download.file)
importFrom(utils,head)
importFrom(utils,untar)
importFrom(xml2,read_html)
importFrom(xml2,read_xml)
35 changes: 33 additions & 2 deletions R/try_query.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#' @param type a character value that specifies the type of data at the target URL. Options are
#' all options that can be supplied to httr::content, these include e.g.
#' "text/tab-separated-values", "application/json" and "txt/csv". Default is "text/tab-separated-values".
#' Default is "tab-separated-values".
#' @param timeout a numeric value that specifies the maximum request time. Default is 60 seconds.
#' @param accept a character value that specifies the type of data that should be sent by the API if
#' it uses content negotiation. The default is NULL and it should only be set for APIs that use
Expand All @@ -22,6 +21,9 @@
#'
#' @importFrom curl has_internet
#' @importFrom httr GET timeout http_error message_for_status http_status content accept
#' @importFrom readr read_tsv read_csv
#' @importFrom jsonlite fromJSON
#' @importFrom xml2 read_html read_xml
#'
#' @return A data frame that contains the table from the url.
try_query <-
Expand Down Expand Up @@ -88,7 +90,36 @@ try_query <-
# Change variable to not show progress if readr is used
options(readr.show_progress = FALSE)

result <- suppressMessages(httr::content(query_result, type = type, encoding = "UTF-8", ...))
# Check if the content is gzip compressed
if (query_result$headers[["content-encoding"]] == "gzip") {
# Retrieve the content as raw bytes using httr::content
raw_content <- httr::content(query_result, type = "raw")

# Decompress the raw content using base R's `memDecompress`
decompressed_content <- memDecompress(raw_content, type = "gzip")

# Convert the raw bytes to a character string
text_content <- rawToChar(decompressed_content)

# Read the decompressed content based on the specified type
if (type == "text/tab-separated-values") {
result <- readr::read_tsv(text_content, ...)
} else if (type == "text/html") {
result <- xml2::read_html(text_content, ...)
} else if (type == "text/xml") {
result <- xml2::read_xml(text_content, ...)
} else if (type == "text/csv" || type == "txt/csv") {
result <- readr::read_csv(text_content, ...)
} else if (type == "application/json") {
result <- jsonlite::fromJSON(text_content, ...) # Using jsonlite for JSON parsing
} else if (type == "text") {
result <- text_content # Return raw text as-is
} else {
stop("Unsupported content type: ", type)
}
} else {
result <- suppressMessages(httr::content(query_result, type = type, encoding = "UTF-8", ...))
}

return(result)
}
3 changes: 1 addition & 2 deletions man/try_query.Rd

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

0 comments on commit 151e795

Please sign in to comment.