-
Notifications
You must be signed in to change notification settings - Fork 24
/
get_dataframe.R
164 lines (153 loc) · 4.79 KB
/
get_dataframe.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#' Download dataverse file as a dataframe
#'
#' Use `get_dataframe_by_name` if you know the name of the datafile and the DOI
#' of the dataset. Use `get_dataframe_by_doi` if you know the DOI of the datafile
#' itself. Use `get_dataframe_by_id` if you know the numeric ID of the
#' datafile.
#'
#' @rdname get_dataframe
#'
#' @param filename The name of the file of interest, with file extension, for example
#' `"roster-bulls-1996.tab"`.
#' @param .f The function to used for reading in the raw dataset. This user
#' must choose the appropriate function: for example if the target is a .rds
#' file, then `.f` should be `readRDS` or `readr::read_rds`.
#' @param original A logical, defaulting to TRUE. Whether to read the ingested,
#' archival version of the datafile if one exists. The archival versions are tab-delimited
#' `.tab` files so if `original = FALSE`, `.f` is set to `readr::read_tsv`.
#' If functions to read the original version is available, then `original = TRUE`
#' with a specified `.f` is better.
#'
#' @inheritDotParams get_file
#'
#' @examples
#' # Retrieve data.frame from dataverse DOI and file name
#' df_tab <-
#' get_dataframe_by_name(
#' filename = "roster-bulls-1996.tab",
#' dataset = "doi:10.70122/FK2/HXJVJU",
#' server = "demo.dataverse.org"
#' )
#'
#' # Retrieve the same file from file DOI
#' df_tab <-
#' get_dataframe_by_doi(
#' filedoi = "10.70122/FK2/HXJVJU/SA3Z2V",
#' server = "demo.dataverse.org"
#' )
#'
#' # Do not run when submitting to CRAN, because the whole
#' # example sometimes takes longer than 10 sec.
#' \dontrun{
#' # Retrieve ingested file originally a Stata dta
#' df_from_stata_ingested <-
#' get_dataframe_by_name(
#' filename = "nlsw88.tab",
#' dataset = "doi:10.70122/FK2/PPIAXE",
#' server = "demo.dataverse.org"
#' )
#'
#' # To use the original file version, or for non-ingested data,
#' # please specify `original = TRUE` and specify a 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
#' )
#' }
#'
#' # Get Stata file as original
#' 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
#' )
#' }
#'
#' # Stata file as ingested file (less information than original)
#' df_stata_ingested <-
#' get_dataframe_by_name(
#' filename = "nlsw88.tab",
#' dataset = "doi:10.70122/FK2/PPIAXE",
#' server = "demo.dataverse.org"
#' )
#'
#' }
#'
#' @export
get_dataframe_by_name <- function (
filename,
dataset = NULL,
.f = NULL,
original = FALSE,
...
) {
# retrieve ID
fileid <- get_fileid.character(x = dataset, file = filename, ...)
get_dataframe_by_id(fileid, .f, original = original, ...)
}
#' @rdname get_dataframe
#' @export
get_dataframe_by_id <- function(
fileid,
.f = NULL,
original = FALSE,
...
) {
# if not ingested, then whether to take the original is not relevant.
ingested <- is_ingested(fileid, ...)
if (isFALSE(ingested)) {
original <- NA
}
if (is.null(.f) & isTRUE(ingested) & isFALSE(original)) {
message("Downloading ingested version of data with readr::read_tsv. To download the original version and remove this message, set original = TRUE.\n")
.f <- readr::read_tsv
}
if (is.null(.f) & (isFALSE(ingested) | isTRUE(original))) {
stop("read-in function was left NULL, but the target file is not ingested or you asked for the original version. Please supply a .f argument.\n")
}
# READ raw data
raw <- get_file(file = fileid, original = original, ...)
# save to temp and then read it in with supplied function
if (!is.null(.f)) {
get_dataframe_internal(raw, filename = "foo", .f = .f)
}
}
#' @rdname get_dataframe
#' @inheritParams get_file_by_doi
#' @export
get_dataframe_by_doi <- function (
filedoi,
.f = NULL,
original = FALSE,
...
) {
filedoi <- prepend_doi(filedoi)
# get_file can also take doi now
get_dataframe_by_id(fileid = filedoi, .f = .f, original = original, ...)
}
#' Write to temp and apply function
#'
#' @keywords internal
get_dataframe_internal <- function (raw, filename, .f) {
tryCatch(
{
tmp <- tempfile(filename)
writeBin(raw, tmp)
do.call(.f, list(tmp))
},
finally = {
if (file.exists(tmp)) unlink(tmp)
}
)
}