ProcessTimeseriesResponse = function(tsResponse, tsName) { if (!is.null(tsResponse)) { # Any request dealing with a single user created item returns a # DSUserObjectResponse. This has ResponseStatus property that indicates # success or failure if (tsResponse$ResponseStatus != DSUserObjectResponseStatus$UserObjectSuccess) print(paste('Request failed for timeseries', tsName, 'with error ', names(DSUserObjectResponseStatus)[tsResponse$ResponseStatus + 1], ': ', tsResponse$ErrorMessage, '\n\n')) else if (!is.null(tsResponse$UserObject)) # The timeseries item won't be returned if you set SkipItem true in CreateItem or UpdateItem { # Here we simply display the timeseries data using a dataframe. tsItem = tsResponse$UserObject metadata = c (Id = tsItem$Id, Desc = tsItem$Description, LastModified = as.character(tsItem$LastModified), StartDate = ifelse (!is.null(tsItem$DateInfo), as.character(tsItem$DateInfo$StartDate), NULL), EndDate = ifelse(!is.null(tsItem$DateInfo),as.character(tsItem$DateInfo$EndDate), NULL), Frequency = ifelse(!is.null(tsItem$DateInfo), names(DSUserObjectFrequency)[tsItem$DateInfo$Frequency + 1], NULL), NoOfValues = ifelse(!is.null(tsItem$DateRange), tsItem$DateRange$ValuesCount , 0)) df = data.frame(metadata) print(df) if (!is.null(tsItem$DateRange)) { df = data.frame(Dates = sapply(tsItem$DateRange$Dates, FUN = function(x) { return (as.character(x)) }), Values = sapply(tsItem$DateRange$Values, FUN = function(x) { ifelse (is.null(x), return (NA_character_ ), return (x) )} )) # Values if NULL, is printed as because, while # convertind list to vector either by using as.vector or sapply, # the NULL values in the list are deleted. and thus there will # be mismatch in no of rows and cannot be put in a dataframe print(df) } } } else print(paste('Timeseries ', tsName, ' successfully updated but item details not returned.')) } library(DatastreamR) # Login using Configuration timeseriesClient = TimeSeriesClient$new('Config.ini') timeseriesClient$useNaNforNotANumber = TRUE getItemResp = timeseriesClient$GetItem('TS000001') # Let's use ProcessTimeseriesResponse to perfom general processing of the # DSUserObjectResponse as we will be handling a few timeseries responses. # See the comments in ProcessTimeseriesResponse for a description of handling # a response ProcessTimeseriesResponse(getItemResp, 'TS000001')