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. > # Datastream User Created Timeseries > # ---------------------------------- > # > # Datastream permits users to create and manage custom items. One of these > # types is custom timeseries data. Clients can upload their own timeseries > # data to Datastream’s central systems. These can then be used in combination > # with Datastream maintained series in charts and data requests with the full > # power of the Datastream expressions language. > # > # DatastreamUserCreated_TimeSeries.R defines a TimeseriesClient class and > # ancillary classes that assist in creating and managing timeseries data. > # > # This simple test script demonstrates how to manage custom timeseries data > # using the DatastreamUserCreated_TimeSeries.R module. > # > # Prerequesites: > # > # Firstly, your Datastream credentials need to be permissioned to use the > # Datastream API services. To verify you have access, please access the API > # test page at https://product.datastream.com/dswsclient/Docs/TestUserCreatedItems.aspx. > # Selecting the "Get Token" service method, insert your Datastream credentials > # and press the Execute button. If your credentials are valid and are permissioned > # to access the API service, you will receive a token response. If you are not > # permissioned, you will receive below message > # "User 'Your ID' not entitled to ClientApi service.". > # Please contact your LSEG representative to get your ID permissioned. > # > # Once you have permissions to access the API service, you also need to be > # permissioned to specifically access the service that permits you to access > # and manage custom user created items such as timeseries. You can verify this > # by using the "Get All Items" service method on the above test page. Using the > # token returned from the GetToken method, copy the token into the Token field > # and then select Execute. If you are permissioned for the user created items > # service, you will get a response listing the timeseries available to you on > # Datastream. This may be an empty list if you are a new customer or have never created user created items before. If you > # are not permissioned, you will receive an error message: > # "ID 'Your ID' is not permissioned for user created items.". > # Please contact your LSEG representative to get your ID permissioned. > # > > library (DatastreamR) > library(logger) > > # You can set the threshold for log messages as given below > # logger::log_threshold(ERROR) > > # This is a simple support function for displaying the contents of an individual > # timeseries item. It's used frequently in the demos below. > 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)) + 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 converting 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.')) + } > > > # > # To query and modify timeseries, you first need to create a TimeseriesClient > # object using your Datastream credentials. > # > # This can be done in two ways. The first method is simply to create the > # client passing your Datastream credentials in directly: > # > # timeseriesClient = TimeseriesClient(NULL, 'YourID', 'YourPwd') > # > # Alternatively, you can use a configuration file containing your > # credentials. The config file just needs your credentials in the following > # format: > # > # [credentials] > # username=YourID > # password=YourPwd > # > # Then create the TimeseriesClient object passing in the config file. > # > # timeseriesClient = TimeseriesClient('config.ini') > # > # > # Okay, let's start the demo by creating the TimeseriesClient. > > timeseriesClient = NULL > > # Assuming you have permissioned credentials, replace 'YourID' and 'YourPwd' > # with them and then let's logon and create the TimeseriesClient object. > # Note the basic error handling for DSUserObjectFault exceptions > > tryCatch({ + # First step is creating the client by replacing 'YourID' and + # 'YourPwd' with your own credentials. + print('Creating a TimeseriesClient object using your credentials.') + + timeseriesClient = TimeSeriesClient$new(NULL, "zdsm043", "horse485") + + #timeseriesClient = TimeSeriesClient('Config.ini') + + }, + error = function(e) + { + stop(message(e)) + } + ) [1] "Creating a TimeseriesClient object using your credentials." INFO [2024-03-14 15:45:52] DSConnect.R DSConnect$getToken :Getting Token and Token Expiry could not find function "content_type_json"Error in value[[3L]](cond) : Calls: tryCatch -> tryCatchList -> tryCatchOne -> Execution halted