/
decks.R
304 lines (275 loc) · 8.49 KB
/
decks.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#' @include shoji-catalog.R
NULL
# Generics ----------------------------------------------------------------
#' Manipulate deck titles
#'
#' Crunch slides have titles and subtitles. You can change these features at
#' either the deck level by assigning a character vector which is
#' the same length as the deck to the CrunchDeck, or by assigning character strings to the
#' the slide.
#' @param x a `CrunchDeck` or `CrunchSlide`
#' @param value character, the new title or subtitle
#' @rdname deck-titles
#' @return `x`, modified
#' @export
#'
#' @examples
#' \dontrun{
#' titles(deck)
#' titles(deck) <- c(new_title1, new_title2)
#' slide <- deck[[1]]
#' title(slide) <- "new title"
#' subtitle(slide) <- "new subtitle"
#' subtitles(deck)
#' }
setGeneric("titles", function(x) standardGeneric("titles"))
#' @rdname deck-titles
#' @export
setGeneric("titles<-", function(x, value) standardGeneric("titles<-"))
#' @rdname deck-titles
setGeneric("title", function(x) standardGeneric("title"))
#' @rdname deck-titles
setGeneric("title<-", function(x, value) standardGeneric("title<-"))
#' @rdname deck-titles
#' @export
setGeneric("subtitles", function(x, value) standardGeneric("subtitles"))
#' @rdname deck-titles
#' @export
setGeneric("subtitles<-", function(x, value) standardGeneric("subtitles<-"))
#' @rdname deck-titles
#' @export
setGeneric("subtitle", function(x, value) standardGeneric("subtitle"))
#' @rdname deck-titles
#' @export
setGeneric("subtitle<-", function(x, value) standardGeneric("subtitle<-"))
#' Get a dataset's DeckCatalog
#'
#' Crunch decks are stored in catalogs. This function returns those catalogs so
#' that you can access and manipulate decks in R.
#'
#' @param x a Crunch Dataset
#' @param value a `CrunchDeck` to add
#'
#' @return a DeckCatalog
#' @rdname decks
#' @export
setGeneric("decks", function(x) standardGeneric("decks"))
#' @rdname decks
#' @export
setGeneric("decks<-", function(x, value) standardGeneric("decks<-"))
#' @rdname decks
#' @export
setMethod("decks", "CrunchDataset", function(x) {
return(DeckCatalog(crGET(shojiURL(x, "catalogs", "decks"))))
})
# Deck Catalog ------------------------------------------------------------
#' Create an empty Crunch Deck
#'
#' @param dataset A Crunch Dataset
#' @param name The name of the Deck
#' @param ... Further attributes of the deck such as the description, see API
#' docs for options.
#' @return The `CrunchDeck` that was created.
#' @export
newDeck <- function(dataset, name, ...) {
stopifnot(is.dataset(dataset))
payload <- wrapEntity(name = name, ...)
shoji <- shojiURL(dataset, "catalogs", "decks")
url <- crPOST(shoji, body = toJSON(payload))
return(CrunchDeck(crGET(url)))
}
setMethod("initialize", "DeckCatalog", init.sortCatalog)
#' @rdname crunch-extract
#' @export
setMethod("[[", "DeckCatalog", function(x, i, ...) {
getEntity(x, i, CrunchDeck, ...)
})
#' @rdname crunch-extract
#' @export
setMethod("[[", c("DeckCatalog", "character", "ANY"), function(x, i, ...) {
if (length(i) > 1) {
halt("You can only select one deck at a time")
}
matches <- names(x) %in% i
if (all(!matches)) {
halt(dQuote(i), " is not present in deck catalog")
}
index <- which(matches)
if (sum(matches) > 1) {
warning(
dQuote(i),
" does not uniquely identify elements. Returning the first match"
)
index <- index[1]
}
getEntity(x, index, CrunchDeck, ...)
})
#' @rdname crunch-extract
#' @export
setMethod(
"[[<-", c("DeckCatalog", "ANY", "missing", "CrunchDeck"),
modifyCatalogInPlace
)
#' This function is no-op because the items are already updated on the server
#' with other methods called prior to it.
#' @rdname crunch-extract
#' @export
setMethod(
"[[<-", c("CrunchDeck", "ANY", "missing", "AnalysisCatalog"),
function(x, i, j, value) invisible(refresh(x))
)
#' @rdname crunch-extract
#' @export
setMethod(
"[[<-", c("SlideCatalog", "ANY", "missing", "CrunchSlide"),
modifyCatalogInPlace
)
#' @rdname crunch-extract
#' @export
setMethod(
"[[<-", c("AnalysisCatalog", "ANY", "missing", "Analysis"),
modifyCatalogInPlace
)
# CrunchDeck --------------------------------------------------------------
#' @rdname crunch-extract
#' @export
setMethod("[[", "CrunchDeck", function(x, i, ...) slides(x)[[i]])
#' @rdname crunch-extract
#' @export
setMethod("[[<-", "CrunchDeck", function(x, i, j, value) {
slides(x)[[i]] <- value
return(invisible(refresh(x)))
})
#' Access the slides of a CrunchDeck
#'
#' Return a `SlideCatalog` from a `CrunchDeck`. All slide catalog methods should be
#' available for `CrunchDecks`, but this function is used internally to model the
#' API.
#'
#' @param x a CrunchDeck
#' @param value a `SlideCatalog` or `CrunchSlide` to add
#'
#' @return a `SlideCatalog`
#' @rdname slides
#' @export
setGeneric("slides", function(x) standardGeneric("slides"))
#' The following function is no-op because the items are already updated on the
#' server with other methods called prior to it.
#' @rdname slides
#' @export
setGeneric("slides<-", function(x, value) standardGeneric("slides<-"))
#' @rdname slides
#' @export
setMethod("slides", "CrunchDeck", function(x) {
return(SlideCatalog(crGET(shojiURL(x, "catalogs", "slides"))))
})
#' @rdname slides
#' @export
setMethod("slides<-", "CrunchDeck", function(x, value) invisible(refresh(x)))
#' @rdname describe-entity
#' @export
setMethod("name<-", "CrunchDeck", function(x, value) {
stopifnot(is.singleCharacter(value))
out <- setEntitySlot(x, "name", value)
return(invisible(out))
})
#' @rdname describe-entity
#' @export
setMethod("description", "CrunchDeck", function(x) x@body$description)
#' @rdname describe-entity
#' @export
setMethod("description<-", "CrunchDeck", function(x, value) {
stopifnot(is.singleCharacter(value))
out <- setEntitySlot(x, "description", value)
return(invisible(out))
})
#' @rdname describe-catalog
#' @export
setMethod("names", "CrunchDeck", function(x) titles(slides(x)))
#' @rdname describe-catalog
#' @export
setMethod("names<-", "CrunchDeck", function(x, value) {
slide_cat <- slides(x)
titles(slide_cat) <- value
})
#' @rdname deck-titles
#' @export
setMethod("titles", "CrunchDeck", function(x) titles(slides(x)))
#' @rdname deck-titles
#' @export
setMethod("titles<-", "CrunchDeck", function(x, value) {
slide_cat <- slides(x)
titles(slide_cat) <- value
invisible(refresh(x))
})
#' @rdname deck-titles
#' @export
setMethod("subtitles", "CrunchDeck", function(x) subtitles(slides(x)))
#' @rdname deck-titles
#' @export
setMethod("subtitles<-", "CrunchDeck", function(x, value) {
slide_cat <- slides(x)
subtitles(slide_cat) <- value
invisible(refresh(x))
})
#' @rdname describe-catalog
#' @export
setMethod("types", "CrunchDeck", function(x) types(slides(x)))
#' Export a Crunch Deck
#'
#' Crunch decks can be exported as excel or json files.
#'
#' @param deck A CrunchDeck
#' @param file The file path to save the exported deck
#' @param format Either `"xlsx"`, `"pptx"`, or `"json"`
#' @param ... Further options to be passed on to the API
#' @return the filename (`file`, if specified, or the the autogenerated file
#' name).
#' @export
exportDeck <- function(deck, file, format = c("xlsx", "pptx", "json"), ...) {
if (!inherits(deck, "CrunchDeck")) {
halt("exportDeck is only available for CrunchDecks.")
}
url <- deck@views$export
format <- match.arg(format)
accept <- extToContentType(format)
if (missing(file)) {
file <- paste0(name(deck), ".", format)
}
body <- list(...)
if (length(body) == 0) {
body <- emptyObject()
}
dl_link <- crPOST(
url,
body = toJSON(list(
element = "shoji:entity",
body = body
)),
config = add_headers(`Accept` = accept)
)
crDownload(dl_link, file)
}
#' @rdname analysis-methods
#' @export
setMethod("weight<-", c("CrunchDeck", "ANY"), function(x, value) {
# Don't set filter on markdown slides
lapply(which(types(x) == "analysis"), function(slide_num) {
weight(x[[slide_num]]) <- value
})
return(refresh(x))
})
#' @rdname analysis-methods
#' @export
setMethod("filter<-", c("CrunchDeck", "ANY"), function(x, value) {
deprecate_filter("CrunchDeck", arrow = TRUE)
})
#' @rdname analysis-methods
#' @export
setMethod("filters<-", c("CrunchDeck", "ANY"), function(x, value) {
# Don't set filter on markdown slides
lapply(which(types(x) == "analysis"), function(slide_num) {
filters(x[[slide_num]]) <- value
})
return(refresh(x))
})