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. > library(DatastreamR) > > 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.')) + } > > # we need to know which dates are supported on Datastream. > # Datastream only stores the actual working weekdays for a given date range, > # so, we need to provide values that match the dates supported. > > # GetTimeseriesDateRange method which returns a list of supported dates > # for a given start date, end date and frequency. > > timeseriesClient = TimeSeriesClient$new("Config.ini") INFO [2024-03-14 15:45:57] DSConnect.R DSConnect$getToken :Getting Token and Token Expiry could not find function "content_type_json"ERROR [2024-03-14 15:45:57] DSConnect.R DSConnect$getToken could not find function "content_type_json"ERROR [2024-03-14 15:45:57] DSConnect.R DSConnect$getToken No response received > # Retrieving the supported dates. > # This example intentionally shows two different date constructors, > # requesting quarterly dates between 2016 and 2022. > startDate = as.Date.character('2016-1-1', format = "%Y-%m-%d") > endDate = as.Date('2022-04-01') > freq = DSUserObjectFrequency$Quarterly > print('Requesting GetTimeseriesDateRange to get supported dates from + 2016-01-0 to 2022-04-01 at quarterly frequency.') [1] "Requesting GetTimeseriesDateRange to get supported dates from\n 2016-01-0 to 2022-04-01 at quarterly frequency." > dateRangeResp = timeseriesClient$GetTimeseriesDateRange(startDate, + endDate, freq) INFO [2024-03-14 15:45:57] DatastreamUserCreated_TimeSeries.RTimeSeriesClient$GetTimeseriesDateRangeRequesting date range You are not logged on. Please recreate the client supplying valid user credentials.ERROR [2024-03-14 15:45:57] DatastreamUserCreated_TimeSeries.R TimeSeriesClient$GetTimeseriesDateRange Exception occured. You are not logged on. Please recreate the client supplying valid user credentials.Error in value[[3L]](cond) : Calls: ... tryCatch -> tryCatchList -> tryCatchOne -> Execution halted