fetchTimestampHelpers: Methods to assist in writing and debugging custom...

Description Usage Arguments Examples

Description

These functions are used within vizlab fetchTimestamp() methods and may be useful within custom methods as well.

urlFetcher - Creates a temporary viz object that has fetcher: url and a custom remoteURL that was probably derived just now in the calling method. The results of this function can be passed to fetch() or fetchTimestamp().

writeTimestamp writes a timestamp file with the conventions used by fetchTimestamp - should be used within custom timestamp fetchers to ensure the expected format is used.

updateTimestamp can be called from within a fetch method (at the end of that method) to update the item's timestamp. This function should rarely be necessary - really only when the timestamp recorded in the fetchTimestamp() call just before this fetch() call is made to go out of date by this fetch() call, e.g., if the timestamp value acquired by fetchTimestamp() is based on the modified time of the fetched file. If that's the case, then calling this function should let remake avoid trying to make the fetched item again right away. This function uses the recipe defined in remake.yaml to update the timestamp.

formatTimestamp formats a POSIXct timestamp according to vizlab timestamp conventions - converts a timestamp to UTC and creates a character string with format '

locateTimestampFile returns a relative file path to the timestamp file for an id.

readTimestamp reads an old timestamp for a viz item, It returns NA if the file is missing or a POSIXct timestamp if given (as text) within the timestamp file.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
urlFetcher(id, location = NA, remoteURL)

writeTimestamp(new.timestamp, viz, timestamp.mtime = NA)

updateTimestamp(viz, verbose = FALSE)

formatTimestamp(timestamp)

locateTimestampFile(id)

readTimestamp(viz)

Arguments

id

the viz id - should be the same as for the viz in the calling function

location

the fetched data file location - should be the same as for the viz in the calling function. May be NA if you're only passing the output to fetchTimestamp(); required for fetch().

remoteURL

the new URL to use for fetching the data and/or timestamp (assuming fetch() or fetchTimestamp() is called on the results of this function)

new.timestamp

the new timestamp to write to file

viz

a viz item (e.g., from as.viz)

timestamp.mtime

the timestamp to assign to the timestamp file's metadata (using Sys.setFileTime - sets the mtime=modified time property); NA leaves the metadata alone

verbose

logical to pass on to remake::make()

timestamp

POSIXct timestamp to format into a character string

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
## Not run: 
# this custom method probably won't do what you want,
# just shows how to use the helper functions
fetchTimestamp.myfetcher <- function(viz) {
  timestamp.file <- locateTimestampFile(viz$id)
  old.timestamp <- readTimestamp(viz)
  new.timestamp <- getOnlineTimestamp() # made-up function, not implemented
  if(!file.exists(timestamp.file) || old.timestamp < new.timestamp) {
    writeTimestamp(new.timestamp, viz)
  }
  invisible()
}

## End(Not run)
## Not run: 
fetchTimestamp.myfetcher <- function(viz) {
  url <- constructURLFromViz(viz)
  urlLastModified(viz$id, remoteURL=url)
}

## End(Not run)

USGS-VIZLAB/vizlab documentation built on Nov. 20, 2017, 7:36 p.m.