R Under development (unstable) (2024-03-14 r86117 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > 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') INFO [2024-03-14 15:46:00] DSConnect.R DSConnect$getToken :Getting Token and Token Expiry could not find function "content_type_json"ERROR [2024-03-14 15:46:00] DSConnect.R DSConnect$getToken could not find function "content_type_json"ERROR [2024-03-14 15:46:00] DSConnect.R DSConnect$getToken No response received > timeseriesClient$useNaNforNotANumber = TRUE > > getItemResp = timeseriesClient$GetItem('TS000001') INFO [2024-03-14 15:46:00] DatastreamUserCreated_TimeSeries.R TimeSeriesClient$GetItem Requesting TS000001 You are not logged on. Please recreate the client supplying valid user credentials.ERROR [2024-03-14 15:46:00] DatastreamUserCreated_TimeSeries.R TimeSeriesClient$GetItem Exception occured. You are not logged on. Please recreate the client supplying valid user credentials.> > # 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') [1] "Timeseries TS000001 successfully updated but item details not returned." > > proc.time() user system elapsed 0.50 0.14 0.62