@@ -59,45 +59,59 @@ parse_source <- function(source, base_url) {
59
59
r
60
60
}
61
61
62
- # ' @method as.data.frame covidcast_data_signal_list
62
+ # ' @method as_tibble covidcast_data_signal_list
63
+ # ' @importFrom tibble as_tibble
64
+ # ' @importFrom purrr map_chr map_lgl
63
65
# ' @export
64
- as.data.frame.covidcast_data_signal_list <- function (x , ... ) {
65
- as.data.frame(
66
- do.call(rbind , lapply(x , function (z ) {
67
- sub <- z [c(
68
- " source" ,
69
- " signal" ,
70
- " name" ,
71
- " active" ,
72
- " short_description" ,
73
- " description" ,
74
- " time_type" ,
75
- " time_label" ,
76
- " value_label" ,
77
- " format" ,
78
- " category" ,
79
- " high_values_are" ,
80
- " is_smoothed" ,
81
- " is_weighted" ,
82
- " is_cumulative" ,
83
- " has_stderr" ,
84
- " has_sample_size"
85
- )]
86
- sub $ geo_types <- paste0(names(z $ geo_types ), collapse = " ," )
87
- sub
88
- })),
89
- row.names = sapply(x , function (y ) y $ key ),
90
- ...
91
- )
66
+ as_tibble.covidcast_data_signal_list <- function (x , ... ) {
67
+ tib <- list ()
68
+ tib $ source <- unname(map_chr(x , " source" ))
69
+ tib $ signal <- unname(map_chr(x , " signal" ))
70
+ tib $ name <- unname(map_chr(x , " name" ))
71
+ tib $ active <- unname(map_lgl(x , " active" ))
72
+ tib $ short_description <- unname(map_chr(x , " short_description" ))
73
+ tib $ description <- unname(map_chr(x , " description" ))
74
+ tib $ time_type <- unname(map_chr(x , " time_type" ))
75
+ tib $ time_label <- unname(map_chr(x , " time_label" ))
76
+ tib $ value_label <- unname(map_chr(x , " value_label" ))
77
+ tib $ format <- unname(map_chr(x , " format" ))
78
+ tib $ category <- unname(map_chr(x , " category" ))
79
+ tib $ high_values_are <- unname(map_chr(x , " high_values_are" ))
80
+ if (" is_smoothed" %in% names(x )) {
81
+ tib $ is_smoothed <- unname(map_lgl(x , " is_smoothed" ))
82
+ } else {
83
+ tib $ is_smoothed <- NA
84
+ }
85
+ if (" is_weighted" %in% names(x )) {
86
+ tib $ is_weighted <- unname(map_lgl(x , " is_weighted" ))
87
+ } else {
88
+ tib $ is_weighted <- NA
89
+ }
90
+ if (" is_cumulative" %in% names(x )) {
91
+ tib $ is_cumulative <- unname(map_lgl(x , " is_cumulative" ))
92
+ } else {
93
+ tib $ is_cumulative <- NA
94
+ }
95
+ if (" has_stderr" %in% names(x )) {
96
+ tib $ has_stderr <- unname(map_lgl(x , " has_stderr" ))
97
+ } else {
98
+ tib $ has_stderr <- NA
99
+ }
100
+ if (" has_sample_size" %in% names(x )) {
101
+ tib $ has_sample_size <- unname(map_lgl(x , " has_sample_size" ))
102
+ } else {
103
+ tib $ has_sample_size <- NA
104
+ }
105
+ as_tibble(tib )
92
106
}
93
107
94
108
# ' @export
95
109
print.covidcast_data_source <- function (x , ... ) {
96
110
print(x $ name , ... )
97
111
print(x $ source , ... )
98
112
print(x $ description , ... )
99
- signals <- as.data.frame (x $ signals )
100
- print(signals [, c(" signal" , " name " , " short_description" )], ... )
113
+ signals <- as_tibble (x $ signals )
114
+ print(signals [, c(" signal" , " short_description" )], ... )
101
115
}
102
116
103
117
# ' Creates the COVIDcast Epidata autocomplete helper
@@ -152,45 +166,26 @@ covidcast_epidata <- function(base_url = global_base_url, timeout_seconds = 30)
152
166
)
153
167
}
154
168
155
- # ' @method as.data.frame covidcast_data_source_list
169
+ # ' @method as_tibble covidcast_data_source_list
156
170
# ' @export
157
- as.data.frame.covidcast_data_source_list <- function (x , ... ) {
158
- as.data.frame(
159
- do.call(
160
- rbind ,
161
- lapply(
162
- x ,
163
- FUN = function (z ) {
164
- cols <- c(
165
- " source" , " name" , " description" , " reference_signal" ,
166
- " license"
167
- )
168
- sub <- z [cols ]
169
- sub $ signals <- paste0(
170
- sapply(z $ signals , function (y ) y $ signal ),
171
- collapse = " ,"
172
- )
173
- sub
174
- }
175
- )
176
- ),
177
- row.names = sapply(x , function (z ) z $ source ),
178
- ...
179
- )
171
+ as_tibble.covidcast_data_source_list <- function (x , ... ) {
172
+ tib <- list ()
173
+ tib $ source <- unname(map_chr(x , " source" ))
174
+ tib $ name <- unname(map_chr(x , " name" ))
175
+ tib $ description <- unname(map_chr(x , " description" ))
176
+ tib $ reference_signal <- unname(map_chr(x , " reference_signal" ))
177
+ tib $ license <- unname(map_chr(x , " license" ))
178
+ as_tibble(tib )
180
179
}
181
180
181
+ # ' @export
182
182
print.covidcast_epidata <- function (x , ... ) {
183
183
print(" COVIDcast Epidata Fetcher" )
184
184
print(" Sources:" )
185
- sources <- as.data.frame(x $ sources )
186
- print(sources [1 : 5 , c(" source" , " name" )], ... )
187
- if (nrow(sources ) > 5 ) {
188
- print(paste0((nrow(sources ) - 5 ), " more..." ))
189
- }
185
+ sources <- as_tibble(x $ sources )
186
+ print(sources [, c(" source" , " name" )], ... )
187
+
190
188
print(" Signals" )
191
- signals <- as.data.frame(x $ signals )
192
- print(signals [1 : 5 , c(" source" , " signal" , " name" )], ... )
193
- if (nrow(signals ) > 5 ) {
194
- print(paste0((nrow(signals ) - 5 ), " more..." ))
195
- }
189
+ signals <- as_tibble(x $ signals )
190
+ print(signals [, c(" source" , " signal" , " name" )], ... )
196
191
}
0 commit comments