- Introduction
- Requirements
- Installation
- Upgrading the Macrobond API
- Getting Started
- Working with R API
- Time series
- Download one series
- Download one series wrapped in ratio (#PerCapita)
- Download several series, transform them to a common length and calendar
- Download series and set start year
- Download series and set common start Date and end Date
- Download several series, change frequency, and set missing value method
- Download series and convert it with 'to higher frequency' method
- Download series, convert frequency with 'to lower frequency' method and set one currency
- Download series, convert frequency and fill in partial periods
- Download series, and set calendar mode
- Get values and dates
- Create and upload a series with only one value
- Create and upload a monthly series
- Create and upload a daily series with currency metadata
- Create and upload a monthly series with flagged forecast
- Append new value to in-house
- How to automatically update in-house from my R-file?
- Delete a series
- Metadata
- How to see series metadata?
- How to see specific metadata?
- Get information about the metadata attribute
- Get series title
- Get the presentation text for a region code
- Get currency metadata from a series
- Get a list of all currencies
- Get name of release
- Get the next release date
- Get next publication date
- How to know if series is discontinued?
- How to handle double region?
- Concept/Region key
- Search
- Generate search code in Macrobond main app
- Simple search query
- Search for 'concept' based on a series, download whole data set
- Search for specific time series defined by the 'concept' and download them
- Search for series of a specific industry sector
- Search for series closing prices and traded volume for companies of a specific industry sector
- Search for series with free text and exclude a source
- Revisions/Vintage
- XTS
- Convert the Macrobond time series object to an xts series
- Create data frame in one step
- Download one series, create data frame and scale values
- Download one series, set start year, and then create an xts data frame
- Download two series, convert them to the same frequency using default settings, and then create an xts data frame
- Download two series, convert them to quarterly, set missing value method for one of them, and then create an xts data frame
- Convert series to use them with ggplot2
- Charts with R
- Macrobond version
- Time series
- Troubleshooting
- License
Introduction
Macrobond offers an API that can be used in R for Windows. You can download time series (values, dates and metadata) and upload your own series to the in-house database which can then be used in the Macrobond application and optionally shared with others.
This document refers to Macrobond application version 1.27 and later and Macrobond API for R version 1.2-8. Examples were tested on R 4.3.0.
Requirements
The API requires Macrobond to be installed and functioning on the computer where R is run.
Package version
We provide a package for R 3.5 - 4.4. If you are running the 64-bit version of R, you need to use the 64-bit version of Macrobond. We have tested our package with various R versions and recommend to use:
for R 3.5.0 - 4.1.0 | Macrobond API 1.1-5 |
for R 4.2.0 - 4.4.0 | Macrobond API 1.2-8 |
The Macrobond API uses the xts package and requires at least version 0.9-7 of that package.
Macrobond license and main app version
This API can be used only with Legacy (without searching and revision history functions) or Data+ license.
R in other programs
Note we test that the API works with R.exe and RGui.exe. When R is used as an embedded component in other programs, it might or might not work. You need to test this yourself and we can only give limited support in those cases.
Installation
The Macrobond R API comes in the form of an R binary package called 'MacrobondAPI' from the Macrobond repository at https://download.macrobond.com/installation/R (you cannot browse to that URL). Here are two ways to install the Macrobond API package.
These methods always download the newest version of our package. If you have older R version, please check here if you need an older Macrobond API package for your R version. If yes, please contact our support for further instructions.
Method 1: Add the Macrobond repository to the list of repositories
You can add this repository to the list of repositories that R will search in various ways. One option is to use the command:
setRepositories(addURLs = c(Macrobond = "https://download.macrobond.com/installation/R"))
or add the repository to the configuration file. See ?setRepositories
for details.
Once you have added the Macrobond repository to your list of repositories, you can just run this R command:
install.packages("MacrobondAPI", type="win.binary")
Method 2: Explicitly specify the repository
- Restart R
- If you do not have 'xts' installed, install it by
install.packages("xts")
If you have 'xts' installed, make sure you have the latest version by running
update.packages("xts")
- Install the MacrobondAPI package by running
install.packages("MacrobondAPI", type="win.binary", repos="https://download.macrobond.com/installation/R")
Upgrading the Macrobond API
- Restart R
- Run
update.packages(repos="https://download.macrobond.com/installation/R", type="win.binary")
We urge you to keep your Macrobond API for R up to date. We also suggest that you periodically upgrade the Macrobond application as described here.
Getting Started
To successfully use the API, you might want to learn more about the Commonly used metadata.
Working with R API
Once you have installed the Macrobond API package, you can view the documentation from R like this:
library(MacrobondAPI) ?MacrobondAPI
You can also view documentation for a specific function directly with:
?getValues
While using help available directly from within R - as described above - is the recommended way of reading the Macrobond R API documentation, you can also view it online (list of commands).
Time series
Download one series
library(MacrobondAPI) seriesGdp <- FetchOneTimeSeries("usgdp") seriesGdp #check that the series loaded OK if (getIsError(seriesGdp)) stop(getErrorMessage(seriesGdp))
Download one series wrapped in ratio (#PerCapita)
Use the #PerCapita ratio to get a series of the US GDP per capita. For more information about this functionality see Ratios.
library(MacrobondAPI) seriesGdpPerCapita <- FetchOneTimeSeries("#PerCapita(usgdp)") seriesGdpPerCapita
Download several series, transform them to a common length and calendar
You need to use addSeries for each series. This allows you to access the settings object for each requested series. By default, the highest frequency of the series and the union on the ranges will be used.
library(MacrobondAPI) seriesRequest <- CreateUnifiedTimeSeriesRequest() addSeries(seriesRequest, "usgdp") addSeries(seriesRequest, "uscpi") twoSeries <- FetchTimeSeries(seriesRequest) twoSeries
Download series and set start year
This will download two series and convert them to the higher frequency. The data range is set to start year 2020 and continue until the last observation that has data in both series. You can also set relative date like "-5y" or "-10" (observations). For more information see SeriesRequest-class.
library(MacrobondAPI) seriesRequest <- CreateUnifiedTimeSeriesRequest() setStartDate(seriesRequest, "2020-01-01") seriesAll <- addSeries(seriesRequest, "usgdp") seriesAll <- addSeries(seriesRequest, "uscpi") twoSeries <- FetchTimeSeries(seriesRequest) twoSeries
Download series and set common start Date and end Date
For all available 'MissingValue' methods see CalendarDateMode.
library(MacrobondAPI) seriesRequest <- CreateUnifiedTimeSeriesRequest() #set the start to the first point where data is available in all series setStartDateMode(seriesRequest, "DataInAllSeries") #set the end to the last point where data is available in all series setEndDateMode(seriesRequest, "DataInAllSeries") #or you can write: #setStartDateMode(seriesRequest, CalendarDateMode[["DataInAllSeries"]]) #setEndDateMode(seriesRequest, CalendarDateMode[["DataInAllSeries"]]) addSeries(seriesRequest, "usgdp") addSeries(seriesRequest, "uscpi") twoSeries <- FetchTimeSeries(seriesRequest) twoSeries
Download several series, change frequency, and set missing value method
For all available 'MissingValue' methods see Missing value methods.
library(MacrobondAPI) seriesRequest <- CreateUnifiedTimeSeriesRequest() setFrequency(seriesRequest, "Monthly") seriesAll <- addSeries(seriesRequest, "usgdp") seriesAll <- addSeries(seriesRequest, "uscpi") setMissingValueMethod(seriesAll, "None") twoSeries <- FetchTimeSeries(seriesRequest) twoSeries
Download series and convert it with 'to higher frequency' method
For all available 'ToHigher' methods see To higher frequency methods.
library(MacrobondAPI) seriesRequest <- CreateUnifiedTimeSeriesRequest() setFrequency(seriesRequest, "Weekly") seriesAll<-addSeries(seriesRequest, "usgdp") seriesAll <- addSeries(seriesRequest, "uscpi") setToHigherFrequencyMethod(seriesAll, "LinearInterpolation") twoSeries <- FetchTimeSeries(seriesRequest) twoSeries
Download series, convert frequency with 'to lower frequency' method and set one currency
For all available 'ToLower' methods see To lower frequency methods.
library(MacrobondAPI) seriesRequest <- CreateUnifiedTimeSeriesRequest() setFrequency(seriesRequest, "Annual") setCurrency(seriesRequest, "chf") seriesAll<-addSeries(seriesRequest, "usgdp") seriesAll <- addSeries(seriesRequest, "chgdp") setToLowerFrequencyMethod(seriesAll, "Last") twoSeries <- FetchTimeSeries(seriesRequest) twoSeries
Download series, convert frequency and fill in partial periods
For all available 'PartialPeriods' methods see TimeSeriesPartialPeriodsMethod.
library(MacrobondAPI) seriesRequest <- CreateUnifiedTimeSeriesRequest() setFrequency(seriesRequest, "Quarterly") series <- addSeries(seriesRequest, "uscpi") setPartialPeriodsMethod(series, "RepeatLastValue") seriesCPI <- FetchTimeSeries(seriesRequest) seriesCPI
Download series, and set calendar mode
For all available 'CalendarMode' methods see CalendarMergeMode.
library(MacrobondAPI) seriesRequest <- CreateUnifiedTimeSeriesRequest() #all holidays will always be included as missing values instead of being skipped in the calendar setCalendarMergeMode(seriesRequest,"FullCalendar") #or you can write: setCalendarMergeMode(seriesRequest, CalendarMergeMode[["FullCalendar"]]) addSeries(seriesRequest, "usfcst3898") addSeries(seriesRequest, "eur") twoSeries <- FetchTimeSeries(seriesRequest) twoSeries
Get values and dates
Obtain separate vectors with values and dates.
library(MacrobondAPI) seriesGdp <- FetchOneTimeSeries("usgdp") vectorOfValues <- getValues(seriesGdp) vectorOfObservationDates <- getDatesAtStartOfPeriod(seriesGdp) vectorOfValues vectorOfObservationDates
Create and upload a series with only one value
Normally it's not possible to create series with only one value because "The values must be specified as an array". But see below workaround:
library(MacrobondAPI) seriesNew <- CreateTimeSeriesObject("ih:mb:priv:s2", "My forecast", "de", "Financial", "Monthly", as.Date("1990-01-01"), list(2)) seriesNew UploadOneOrMoreTimeSeries(seriesNew)
Create and upload a monthly series
library(MacrobondAPI) seriesNew <- CreateTimeSeriesObject("ih:mb:priv:s1", "My forecast", "us", "Forecasts", "Monthly", as.Date("1990-01-01"), c(12.2, 12.7, 12.8, 13.0)) UploadOneOrMoreTimeSeries(seriesNew)
To see series in Macrobond main app after uploading please refresh Account in-house tree in Macrobond with rounded arrow icon.
Create and upload a daily series with currency metadata
The default value for 'dayMask' parameter is "MondayToFriday". For the list of all available parameters see TimeSeriesWeekdays.
library(MacrobondAPI) meta <- CreateEmptyMetadata() addMetadataValue(meta, "Currency", "eur") seriesNew <- CreateTimeSeriesObject("ih:mb:priv:s2", "My forecast", "de", "Financial", "Daily", as.Date("1990-01-01"), c(12.2, 12.7, 12.8, 13.0, 12.2, 13.1, 14.4),"FullWeek", meta) UploadOneOrMoreTimeSeries(seriesNew)
To see series in Macrobond main app after uploading please refresh Account in-house tree in Macrobond with rounded arrow icon.
Create and upload a monthly series with flagged forecast
seriesNew <- CreateTimeSeriesObjectWithForecastFlags("ih:mb:priv:s1", "My forecast", "us", "Forecasts", "Monthly", as.Date("2022-11-01"), c(12.2, 12.7, 12.8, 13.0), c(FALSE, FALSE, TRUE, TRUE)) UploadOneOrMoreTimeSeries(seriesNew)
To see series in Macrobond main app after uploading please refresh Account in-house tree in Macrobond with rounded arrow icon.
Append new value to in-house
First create an in-house time series - s1, that step is needed only once. It'll fetch that in-house series, add to it value 123 at a specific date, then upload it.
library(MacrobondAPI) mySeries <- FetchOneTimeSeries("ih:mb:priv:s1") values = getValues(mySeries) values[getIndexAtDate(mySeries,as.Date("1990-11-01"))] <- 123 seriesNew = CreateTimeSeriesObject("ih:mb:priv:s1", "My forecast", "us", "Forecasts", "Monthly", as.Date("1990-01-01"), values ) UploadOneOrMoreTimeSeries(seriesNew)
How to automatically update in-house from my R-file?
There is no such function in MB API, we recommend to use taskscheduleR package or use built-in schedulers.
Delete a series
library(MacrobondAPI) DeleteOneOrMoreTimeSeries("ih:mb:priv:s1")
Metadata
How to see series metadata?
List metadata available for a series.
library(MacrobondAPI) seriesGdp <- FetchOneTimeSeries("usgdp") meta <- getMetadata(seriesGdp) show(meta)
How to see specific metadata?
To see other metadata replace text in last line's quotation marks:
library(MacrobondAPI) seriesGdp <- FetchOneTimeSeries("usgdp") getMetadataValues(getMetadata(seriesGdp), "LastModifiedTimeStamp")
Get information about the metadata attribute
All possible commands can be found under MetadataInformation.
library(MacrobondAPI) #get information about the TimeDim metadata attribute metaInfo <- GetMetadataInformation("TimeDim") show(metaInfo) #information about metadata listAllValues(metaInfo) #list of versions of this metadata #other getDescription(metaInfo) getComment(metaInfo) getValueType(metaInfo)
Get series title
library(MacrobondAPI) seriesGdp <- FetchOneTimeSeries("usgdp") getTitle(seriesGdp)
Get the presentation text for a region code
This will get information about the metadata type called 'Region' and then get the presentation text for the region called 'us'. The result will be the string 'United States'.
library(MacrobondAPI) seriesGdp <- FetchOneTimeSeries("usgdp") meta <- GetMetadataInformation("Region") country <- getMetadataValues(getMetadata(seriesGdp), "Region") getValuePresentationText(meta, country)
Get currency metadata from a series
library(MacrobondAPI) seriesGdp <- FetchOneTimeSeries("usgdp") metaGdp <- getMetadata(seriesGdp) currency <- getFirstMetadataValue(metaGdp, "Currency") currency
Get a list of all currencies
This will get information about the metadata type called 'Currency' and list all available currency codes.
library(MacrobondAPI) metaInfo <- GetMetadataInformation("Currency") metaInfo listAllValues(metaInfo)
Get name of release
The name of the release is a metadata property of the time series. The release has a corresponding "entity" in the database.
library(MacrobondAPI) seriesGdp <- FetchOneTimeSeries("usgdp") metaGdp <- getMetadata(seriesGdp) releaseGdpName <- getFirstMetadataValue(metaGdp, "Release") releaseGdpName
Get the next release date
The NextReleaseEventTime attribute contains information about the next scheduled release of updated series. The date might be missing if no release date is known.
library(MacrobondAPI) seriesGdp <- FetchOneTimeSeries("usgdp") metaGdp <- getMetadata(seriesGdp) releaseGdpName <- getFirstMetadataValue(metaGdp, "Release") if (!is.null(releaseGdpName)) { releaseGdp <- FetchOneEntity(releaseGdpName) getFirstMetadataValue(getMetadata(releaseGdp), "NextReleaseEventTime") }
Get next publication date
Some series have 'NextReleaseEventTime' metadata but some don't. Then it's the best to use 'CalendarUrl' metadata connected to Release metadata.
library(MacrobondAPI) seriesGdp <- FetchOneTimeSeries("usgdp") metaGdp <- getMetadata(seriesGdp) releaseGdpName <- getFirstMetadataValue(metaGdp, "Release") if (!is.null(releaseGdpName)) { releaseGdp <- FetchOneEntity(releaseGdpName) getFirstMetadataValue(getMetadata(releaseGdp), "CalendarUrl") }
How to know if series is discontinued?
EntityState = 0 means series is active
EntityState = 4 means series is discontinued
library(MacrobondAPI) seriesGdp <- FetchOneTimeSeries("usgdp") getMetadataValues(getMetadata(seriesGdp), "EntityState")
How to handle double region?
Some series have two regions and they do not display properly when you ask for Region metadata in R because of it. We have two solutions:
library(MacrobondAPI) series <- FetchOneTimeSeries("eea_8_579") metaRegion <- GetMetadataInformation("Region") meta <- getMetadata(series) for (singleRegion in getMetadataValues(meta, name = "Region") ){ print(getValuePresentationText(metaRegion, singleRegion ) ) }
library(MacrobondAPI) series <- FetchOneTimeSeries("eea_8_579") metaRegion <- GetMetadataInformation("Region") meta <- getMetadata(series) paste(lapply(getMetadataValues(meta, name = "Region"), getValuePresentationText,m=metaRegion),collapse = ", ") #to get country you can use to this code: getValuePresentationText(metaRegion, getMetadataValues(meta, name = "Region")[1])
Unfortunately you cannot be sure that from two attributes country always the first element. If you really want to find the region, the best option right now is to look for the region where RegionType="country". In future versions there will be a "ArrangeValues" method you can use sort them.
Concept/Region key
Some of our series are connected with each other through metadata called Region Key. If you know it, you can find series for other countries/regions. In main-app you can see those data sets under Concept & Region data tree.
Get 'concept' from a series
library(MacrobondAPI) getMetadata(FetchOneTimeSeries("delead1000"))
In the output you will see:
RegionKey busc_mb : Macrobond Defined, Business Cycle
The busc_mb is a Region key.
Get 'concept' description
library(MacrobondAPI) series <- FetchOneTimeSeries("uscpi") show(GetConceptDescription(getConcepts(series)))
Use 'concept' to reference a series
library(MacrobondAPI) FetchOneTimeSeries(":Region:gdp_total[Region=us]")
Use 'concepts' to find series for several countries
library(MacrobondAPI) ks <- function(type, region) paste0(":Region:", type, "[Region=", region, "]") rs <- function(region) FetchTimeSeries(c(ks("cpi_total", region), ks("gdp_total", region), ks("trade_balance", region))) #this will download three series for United States rs("us") #this will download the corresponding three series for Great Britain rs("gb") #this will download all three series for both countries rs(c("gb", "us"))
Search
The number of results returned by MacrobondAPI::SearchEntities is limited to 12,000 without search text (only filters) and 6,000 with search text. If you’re experiencing result truncation – please provide more precise search criteria.
To see how to use search function in creating charts go to Circular bar chart (incl. automated search) and Choropleth map (incl. automated search).
Generate search code in Macrobond main app
When you input a query into search field in Macrobond’s Search tab, you can then generate a ready-to-use code for search in R:
Simple search query
This method requires a Data+ license.
library(MacrobondAPI) query <- CreateSearchQuery() setSearchText(query, "germany cpi") entities <- SearchEntities(query) entities
Search for 'concept' based on a series, download whole data set
This method requires a Data+ license.
library(MacrobondAPI) series<-FetchOneTimeSeries("ted_ar_ahw") #get the concept of a series and create search query based on it query <- CreateSearchQuery() setEntityTypeFilter(query, "TimeSeries") addAttributeValueFilter(query, "RegionKey", getConcepts(series)) country_set <- getEntities(SearchEntities(query)) #create empty vector list_of_series<-NULL #get list of series' codes for (i in country_set){ list_of_series<-append(list_of_series, getName(i)) } list_of_series
Search for specific time series defined by the 'concept' and download them
This method requires a Data+ license.
Search for time series defined for the concept 'gdp_total' for four countries (based on List of countries and regions) and then download those series.
library(MacrobondAPI) gdp_names <- SearchByConcept(c("us", "gb", "jp", "cn"), "gdp_total") series <- FetchUnifiedTimeSeriesDefault(gdp_names) series
Search for series of a specific industry sector
This example only works with Data+ license.
This will select the Companies where IcbClassification=icb2713 (Aerospace).
library(MacrobondAPI) query <- CreateSearchQuery() setEntityTypeFilter(query, "Company") addAttributeValueFilter(query, "IcbClassification", "icb_2713") dataset <- getEntities(SearchEntities(query)) #create empty vector list_of_series<-NULL #get list of series' codes for (i in dataset){ list_of_series<-append(list_of_series, getName(i)) } list_of_series
Search for series closing prices and traded volume for companies of a specific industry sector
This example only works with Data+ license.
Search for series closing prices and traded volume for companies of a specific industry sector.
The first query will select the Companies where IcbClassification=icb2713 (Aerospace).
The second query (querySecurity) will find all securities where CompanyKey=main_equity and has the Company equal to one of the companies in the result of the first query.
The third query called queryCloseSeries will find all series where DataType=close and has the Security equal to one of the securities found by the previous filter.
The fourth query, volume series are found in an analogous way.
library(MacrobondAPI) query <- CreateSearchQuery() setEntityTypeFilter(query, "Company") addAttributeValueFilter(query, "IcbClassification", "icb_2713") querySecurity <- CreateSearchQuery() setEntityTypeFilter(querySecurity, "Security") addAttributeValueFilter(querySecurity, "CompanyKey", "main_equity") queryCloseSeries <- CreateSearchQuery() setEntityTypeFilter(queryCloseSeries, "TimeSeries") addAttributeValueFilter(queryCloseSeries, "DataType", "close") querySeriesVolume <- CreateSearchQuery() setEntityTypeFilter(querySeriesVolume, "TimeSeries") addAttributeValueFilter(querySeriesVolume, "PriceType", "vl") entities_close <- SearchEntities(c(query, querySecurity, queryCloseSeries)) entities_volume <- SearchEntities(c(query, querySecurity, querySeriesVolume)) entities_close entities_volume
Search for series with free text and exclude a source
This method requires a Data+ license.
Search for time series, that are seasonally adjusted, belongs to the region us or de and matches the free text 'steel production'. Exclude series from the source World Steel Association.
To get metadata code use How to see specific metadata?.
library(MacrobondAPI) query <- CreateSearchQuery() setEntityTypeFilter(query, "TimeSeries") setSearchText(query, "steel production") addAttributeFilter(query, "SeasonAdj") addAttributeValueFilter(query, "Region", c("us", "de")) addAttributeValueFilter(query, "Source", "src_worldsteel", FALSE) entities <- SearchEntities(query) entities
Revisions/Vintage
To see how to use this feature in creating charts go to Slope chart (incl. vintage/revision data).
Get all available revisions
This method requires a Data+ license.
library(MacrobondAPI) getCompleteHistory(FetchOneTimeSeriesWithRevisions("usgdp"))
Get timestamps of each value in the current series
This method requires a Data+ license.
library(MacrobondAPI) getVintageTimes(FetchOneTimeSeriesWithRevisions("usgdp"))
Get the vintage series that shows what it looked like at specific point in time
This method requires a Data+ license.
library(MacrobondAPI) seriesWithRevisions <- FetchOneTimeSeriesWithRevisions("usgdp") series2018 <- getVintage(seriesWithRevisions, as.POSIXct("2018-01-01 18:00:00", tz = "EST")) plot(series2018)
Get revision history for original and first release
This method requires a Data+ license.
library(MacrobondAPI) seriesGdp <- FetchOneTimeSeriesWithRevisions("usgdp") firstRelease <- getNthRelease(seriesGdp, 0) secondRelease <- getNthRelease(seriesGdp, 1) series.xts <- MakeXtsFromUnifiedResponse(c(firstRelease, secondRelease)) plot(series.xts)
Get the timestamps of each value in Nth release
This method requires a Data+ license.
library(MacrobondAPI) seriesGdp <- FetchOneTimeSeriesWithRevisions("usgdp") firstRelease <- getNthRelease(seriesGdp, 0) getRevisionTimesFromNthRelease(firstRelease)
XTS
MacrobondAPI for R uses package xts to create its objects and you can convert series to xts objects.
Convert the Macrobond time series object to an xts series
library(MacrobondAPI) seriesGdp <- FetchOneTimeSeries("usgdp") seriesGdp.xts <- as.xts(seriesGdp) seriesGdp.xts
Create data frame in one step
library(MacrobondAPI) seriesGdp.xts <- as.xts(FetchOneTimeSeries("usgdp"))
Download one series, create data frame and scale values
We store values in their original form, in single units, i.e., 19699465000000. While our main app transforms these values automatically on Time table/Time chart, R download them in original form, without any formatting. You can scale them by yourself with below code:
library(MacrobondAPI) seriesGdp <- FetchOneTimeSeries("usgdp") seriesGdp.xts <- as.xts(FetchOneTimeSeries("usgdp")) seriesGdp.xts/1000000000000
Outcome:
2022-01-01 19.727918
2022-04-01 19.699465
Download one series, set start year, and then create an xts data frame
Set start year for a series and download it with shortened history. It can be modified for more than one series - use this line: addSeries(seriesRequest, "usgdp")
to add more time series.
library(MacrobondAPI) seriesRequest <- CreateUnifiedTimeSeriesRequest() setStartDate(seriesRequest, "2020-01-01") addSeries(seriesRequest, "usgdp") oneSeries <- FetchTimeSeries(seriesRequest) series.xts <- MakeXtsFromUnifiedResponse(oneSeries) series.xts
Download two series, convert them to the same frequency using default settings, and then create an xts data frame
library(MacrobondAPI) twoSeries <- FetchUnifiedTimeSeriesDefault(c("usgdp", "uscpi")) series.xts <- MakeXtsFromUnifiedResponse(twoSeries) series.xts
Download two series, convert them to quarterly, set missing value method for one of them, and then create an xts data frame
library(MacrobondAPI) seriesRequest <- CreateUnifiedTimeSeriesRequest() setFrequency(seriesRequest, "Quarterly") addSeries(seriesRequest, "usgdp") seriesExpressionCpi <- addSeries(seriesRequest, "uscpi") setMissingValueMethod(seriesExpressionCpi, "None") twoSeries <- FetchTimeSeries(seriesRequest) series.xts <- MakeXtsFromUnifiedResponse(twoSeries) series.xts
Convert series to use them with ggplot2
To go from MacrobondAPI to ggplot2 please use for example below code. For more advanced examples see Charts with R.
library(MacrobondAPI) library(ggplot2) #download series series_pl <- FetchOneTimeSeries("plnaac0195") series_hu <- FetchOneTimeSeries("hunaac0118") series_cz <- FetchOneTimeSeries("cznaac0246") series_ro <- FetchOneTimeSeries("ronaac0012") #convert them into xts objects series_pl.xts <- as.xts(series_pl) series_hu.xts <- as.xts(series_hu) series_cz.xts <- as.xts(series_cz) series_ro.xts <- as.xts(series_ro) #combine them br<-cbind(series_pl.xts, series_hu.xts,series_cz.xts, series_ro.xts) #plot one series with ggplot2 ggplot(br, aes(x=plnaac0195, y=Index))+geom_line() #plot all four series with ggplot2 ggplot(br, aes(y=Index))+ geom_line(aes(x=plnaac0195, colour="red")) + geom_line(aes(x=hunaac0118, colour="blue")) + geom_line(aes(x=cznaac0246, colour="black")) + geom_line(aes(x=ronaac0012, colour="green"))
Charts with R
Simple plot
library(MacrobondAPI) seriesGdp <- FetchOneTimeSeries("usgdp") plot(seriesGdp)
Outside of Macrobond - charts with R
Under Charts with R you can find examples (with code) how to download and construct certain charts unavailable in Macrobond.
Macrobond version
Some functions are available since a specific version of main application. With this command you can check in R which version you have.
GetMacrobondVersion()
Troubleshooting
If you're having problems with the Macrobond R connector, please contact us via https://www.macrobond.com/contact/support/ and:
- Describe what you’re trying to achieve
- Include the relevant code you tried to run
- Provide information about version of Macrobond, R and the Macrobond’s R add–in. To do it, issue those two commands:
library(MacrobondAPI) sessionInfo()
and send us the output.
License
The Macrobond R API uses the license GPL-2. It uses portions of an enhanced version of RDCOMClient. There is a source package available.