1313# ' If missing, no filtering is applied to remove old files.
1414# ' @param until A \link{POSIXt} object to filter out newer files, i.e., only files older than \code{until} will be retained.
1515# ' If missing, no filtering is applied to remove new files.
16+ # ' @param metadata Logical scalar indicating whether the metadata itself should be returned.
17+ # ' This can be set to \code{FALSE} for better performance if only the path is of interest.
1618# ' @param number Integer specifying the maximum number of results to return.
1719# ' This can also be \code{Inf} to return all results.
1820# ' @param on.truncation String specifying what to do when the number of results exceeds \code{number}.
2628# ' \item \code{user}, the identity of the file owner.
2729# ' \item \code{time}, the Unix time of most recent file modification.
2830# ' \item \code{metadata}, a list representing the JSON contents of the file.
31+ # ' Only present if metadata retrieval was requested via \code{metadata=TRUE} in the \code{query} call.
2932# ' }
3033# '
31- # ' For \code{formatQueryResults}, a data frame containing \code{path}, \code{user}, \code{time} and \code{metadata}.
34+ # ' For \code{formatQueryResults}, a data frame containing \code{path}, \code{user}, \code{time} and (if requested) \code{metadata}.
3235# ' Each row corresponds to one of the search results in \code{results}.
3336# ' Each \code{time} is now a \link{POSIXct} object.
3437# '
8689# ' formatQueryResults(q)
8790# ' @export
8891# ' @import httr2
89- # ' @importFrom utils head
90- query <- function (text , user , path , from , until , url , number = 100 , on.truncation = c(" message" , " warning" , " none" )) {
92+ query <- function (text , user , path , from , until , url , number = 100 , metadata = TRUE , on.truncation = c(" message" , " warning" , " none" )) {
9193 conditions <- list ()
9294
9395 if (! missing(text )) {
@@ -125,8 +127,11 @@ query <- function(text, user, path, from, until, url, number=100, on.truncation=
125127 }
126128
127129 stub <- paste0(" /query?translate=true" )
128- collected <- list ()
130+ if (! metadata ) {
131+ stub <- paste0(stub , " &metadata=false" )
132+ }
129133
134+ collected <- list ()
130135 while (length(collected ) < number ) {
131136 current.url <- paste0(url , stub )
132137 if (! is.infinite(number )) {
@@ -148,19 +153,7 @@ query <- function(text, user, path, from, until, url, number=100, on.truncation=
148153 }
149154 }
150155
151- if (on.truncation != " none" ) {
152- if (! is.infinite(original.number ) && original.number < length(collected )) {
153- msg <- sprintf(" truncated query results to the first %i matches" , original.number )
154- if (on.truncation == " warning" ) {
155- warning(msg )
156- } else {
157- message(msg )
158- }
159- collected <- head(collected , original.number )
160- }
161- }
162-
163- collected
156+ handle_truncated_pages(on.truncation , original.number , collected )
164157}
165158
166159# ' @export
@@ -171,19 +164,27 @@ formatQueryResults <- function(results) {
171164 all.times <- double(N )
172165 all.users <- character (N )
173166 all.meta <- vector(" list" , N )
167+ has.metadata <- FALSE
174168
175169 for (i in seq_along(results )) {
176170 y <- results [[i ]]
177171 all.paths [i ] <- y $ path
178172 all.times [i ] <- y $ time
179173 all.users [i ] <- y $ user
180- all.meta [[i ]] <- y $ metadata
174+ if (! is.null(y $ metadata )) {
175+ all.meta [[i ]] <- y $ metadata
176+ has.metadata <- TRUE
177+ }
181178 }
182179
183- data.frame (
180+ output <- data.frame (
184181 path = all.paths ,
185182 user = all.users ,
186- time = as.POSIXct(all.times ),
187- metadata = I(all.meta )
188- )
183+ time = as.POSIXct(all.times )
184+ )
185+ if (has.metadata ) {
186+ output $ metadata <- I(all.meta )
187+ }
188+
189+ output
189190}
0 commit comments