-
Notifications
You must be signed in to change notification settings - Fork 4
/
readme.rmd
104 lines (84 loc) · 2.29 KB
/
readme.rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
---
title: "xbrlus"
output:
html_document:
keep_md: yes
---
This package provides an R interface to
[XBRL US API](https://github.com/xbrlus/data_analysis_toolkit).
## Installation
```{r eval=FALSE}
devtools::install_github("bergant/xbrlus")
```
## Setup
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.
## Usage
Get information about companies and XBRL concepts with `xbrlCIKLookup`
and `xbrlBaseElement`:
```{r cache=TRUE}
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:
```{r cache=TRUE}
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:
```{r message=FALSE, results='asis'}
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)))
```
## References
Data Analysis Toolkit and API description on GitHub: https://github.com/xbrlus/data_analysis_toolkit