Skip to content

Commit

Permalink
Use curl download because base R has terrible defaults for timeout an…
Browse files Browse the repository at this point in the history
…d mode.
  • Loading branch information
jeroen committed Dec 2, 2024
1 parent aa2fb37 commit 072d946
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ SystemRequirements: OpenSSL, Cyrus SASL (aka libsasl2)
RoxygenNote: 7.3.2.9000
Roxygen: list(markdown = TRUE)
Suggests:
curl,
spelling,
nycflights13,
ggplot2
Expand Down
17 changes: 11 additions & 6 deletions R/reader.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,28 @@
#'
#' Utility to read BSON data into R without MongoDB. Useful to read a dump from
#' from a `mongoexport` dump if it fits in memory. This utility does not attempt
#' to convert data into a data.frame: the output is a vector with length equal
#' to the number of documents in the collection.
#' to convert data into a data.frame: the output is a vector with length of the
#' total number of documents in the bson collection.
#'
#' Alternatively, to import a bson dump into your local mongodb server, use the
#' [mongo$import][mongo] function instead. This requires little memory and once
#' data is in mongo you can easily query it.
#'
#' @export
#' @useDynLib mongolite R_bson_reader_file
#' @param file path to a bson file on disk
#' @param file path or url to a bson file
#' @param as_json read data into json strings instead of R lists.
#' @param verbose print some progress output while reading
#' @examples
#' download.file("http://jeroen.github.io/data/diamonds.bson", "diamonds.bson")
#' diamonds <- read_bson("diamonds.bson")
#' unlink("diamonds.bson")
#' diamonds <- read_bson("http://jeroen.github.io/data/diamonds.bson")
#' length(diamonds)
read_bson <- function(file, as_json = FALSE, verbose = interactive()){
if(grepl("^https?://", file)){
file_url <- file
file <- tempfile()
on.exit(unlink(file))
curl::curl_download(file_url, file, quiet = !isTRUE(verbose))
}
file <- normalizePath(file, mustWork = TRUE)
.Call(R_bson_reader_file, file, as_json, verbose)
}
11 changes: 5 additions & 6 deletions man/read_bson.Rd

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

0 comments on commit 072d946

Please sign in to comment.