atorus-research / datasetjson

Read and write CDISC Dataset JSON files
https://atorus-research.github.io/datasetjson/
Apache License 2.0
12 stars 2 forks source link

Feature Request: Allow reader to pull data from a URL #8

Closed mstackhouse closed 11 months ago

mstackhouse commented 11 months ago

Feature Idea

Replicate the function of haven, where haven::read_xpt() can pull directly from a URL like GitHub raw files

Relevant Input

URL

Relevant Output

Dataframe

Reproducible Example/Pseudo Code

datasetjson::read_dataset_json(https://raw.githubusercontent.com/cdisc-org/DataExchange-DatasetJson/master/examples/sdtm/dm.json)
mstackhouse commented 11 months ago
con <- url(
  "https://github.com/cdisc-org/DataExchange-DatasetJson/raw/master/examples/sdtm/ti.json", 
  method = "libcurl"
  )

x <- readLines(con, warn=FALSE)

close(con)

jsonlite::fromJSON(x)
mstackhouse commented 11 months ago

For a local file for testing:

con <- url(paste0("file://", normalizePath(test_path("testdata", "ta.json"))))
mstackhouse commented 11 months ago

This is url detection from {readr}:

https://github.com/tidyverse/readr/blob/b3d09122074143987754a83ccb6eec730a20a37e/R/source.R#L196

is_url <- function(path) {
  grepl("^((http|ftp)s?|sftp)://", path)
}

Could just add in additionally for local files:

^((http|ftp)s?|sftp|file):\/\/