Skip to content

Instantly share code, notes, and snippets.

@mbjones
Created October 26, 2023 23:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbjones/7defd8e6e9c9fe8da5084ceb4f20536c to your computer and use it in GitHub Desktop.
Save mbjones/7defd8e6e9c9fe8da5084ceb4f20536c to your computer and use it in GitHub Desktop.
KNB download CSV
library(dataone)
# Download a CSV-formatted data file from the KNB
# repository, and convert it to a data.frame
get_dataframe <- function(pid) {
d1c <- D1Client("PROD", "urn:node:KNB")
data_raw <- getObject(d1c@mn, pid)
data_char <- rawToChar(data_raw)
csv_text <- textConnection(data_char)
df <- read.csv(csv_text, stringsAsFactors = FALSE)
return(df)
}
# data_url <- "https://knb.ecoinformatics.org/knb/d1/mn/v2/object/urn%3Auuid%3A4c9bdb65-e58b-41bc-8f80-10f59ee332e9"
# be sure to set your DataONE auth token first (but don't check it into scripts) for access to private data
# options(dataone_token = "eyJhbGciOiJSUzI1NiJ9.eyJzdWIi...........lZNw")
pid <- "urn:uuid:4c9bdb65-e58b-41bc-8f80-10f59ee332e9"
df <- get_dataframe(pid)
head(df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment