This package provides an R interface to XBRL US API.
devtools::install_github("bergant/xbrlus")
All APIs (except for the CIKLookup
) require use of a valid XBRL US API
key. You can get the key and read the terms of usage at
http://xbrl.us/use/howto/data-analysis-toolkit/.
xbrlus package will read the API key from environment variable
XBRLUS_API_KEY
.
To start R session with the initialized environment variable
create a file in your R home with a line like this:
XBRLUS_API_KEY=EnterKeyHere
and name it as .Renviron
. To check where your R home is, type normalizePath("~")
in your R console.
Get information about companies and XBRL concepts with xbrlCIKLookup
and xbrlBaseElement
:
library(xbrlus) companies <- xbrlCIKLookup(c( "aapl", "goog", "fb" )) elements <- xbrlBaseElement(c( "AssetsCurrent", "AssetsNoncurrent", "Assets", "LiabilitiesCurrent", "LiabilitiesNoncurrent", "Liabilities", "StockholdersEquity", "MinorityInterest", "StockholdersEquityIncludingPortionAttributableToNoncontrollingInterest", "LiabilitiesAndStockholdersEquity" ))
Use xbrlValues
to get balance sheet values:
values <- xbrlValues( CIK = companies$cik, Element = elements$elementName, DimReqd = FALSE, Period = "Y", Year = 2013, NoYears = 1, Ultimus = TRUE, Small = TRUE, as_data_frame = TRUE )
Reshape to wide format and print table:
library(dplyr) library(tidyr) balance_sheet <- elements %>% left_join(values, by = "elementName") %>% select(entity, standard.text, amount) %>% mutate(amount = round(amount / 10e6,0)) %>% spread(entity, amount) balance_sheet <- balance_sheet[ order(order(elements$elementName)), !is.na(names(balance_sheet))] row.names(balance_sheet) <- NULL library(pander) pandoc.table( balance_sheet, caption = "Balance Sheet Comparison", big.mark = ",", split.table = 200, style = "rmarkdown", justify = c("left", rep("right", 3)))
Data Analysis Toolkit and API description on GitHub: https://github.com/xbrlus/data_analysis_toolkit
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.