if (FALSE) {
# 1. For files originally in plain-text (.csv, .tsv), we recommend
# retreiving data.frame from dataverse DOI and file name, or the file's DOI.
df_tab <-
get_dataframe_by_name(
filename = "roster-bulls-1996.tab",
dataset = "doi:10.70122/FK2/HXJVJU",
server = "demo.dataverse.org"
)
df_tab <-
get_dataframe_by_doi(
filedoi = "10.70122/FK2/HXJVJU/SA3Z2V",
server = "demo.dataverse.org"
)
# 2. For files where Dataverse's ingest loses information (Stata .dta, SPSS .sav)
# or cannot be ingested (R .rds), we recommend
# specifying `original = TRUE` and specifying a read-in function in .f.
# Rds files are not ingested so original = TRUE and .f is required.
if (requireNamespace("readr", quietly = TRUE)) {
df_from_rds_original <-
get_dataframe_by_name(
filename = "nlsw88_rds-export.rds",
dataset = "doi:10.70122/FK2/PPIAXE",
server = "demo.dataverse.org",
original = TRUE,
.f = readr::read_rds
)
}
# Stata dta files lose attributes such as value labels upon ingest so
# reading the original version by a Stata reader such as `haven` is recommended.
if (requireNamespace("haven", quietly = TRUE)) {
df_stata_original <-
get_dataframe_by_name(
filename = "nlsw88.tab",
dataset = "doi:10.70122/FK2/PPIAXE",
server = "demo.dataverse.org",
original = TRUE,
.f = haven::read_dta
)
}
# 3. RData files are read in by `base::load()` but cannot be assigned to an
# object name. The following shows two possible ways to read in such files.
# First, the RData object can be loaded to the environment without object assignment.
get_dataframe_by_doi(
filedoi = "10.70122/FK2/PPIAXE/X2FC5V",
server = "demo.dataverse.org",
original = TRUE,
.f = function(x) load(x, envir = .GlobalEnv))
# If you are certain each RData contains only one object, one could define a
# custom function used in https://stackoverflow.com/a/34926943
load_object <- function(file) {
tmp <- new.env()
load(file = file, envir = tmp)
tmp[[ls(tmp)[1]]]
}
# https://demo.dataverse.org/file.xhtml?persistentId=doi:10.70122/FK2/PPIAXE/X2FC5V
as_rda <- get_dataframe_by_id(
file = 1939003,
server = "demo.dataverse.org",
.f = load_object,
original = TRUE)
}
Run the code above in your browser using DataLab