library( irs990efile ) # functions for parsing efile XMLs
library( dplyr ) # data wrangling
library( purrr ) # data wrangling
library( pander ) # formatting
library( knitr ) # formatting
library( DT ) # table printing
library( httr ) # web requests
library( xmltools ) # xml utilities
library( xml2 ) # xml utilities
library( XML ) # xml utilities
library( data.tree ) # network visualization
library( networkD3 ) # network visualization
library( igraph ) # network visualization
source( "R/rdb-functions-v2.R")
source( "R/utils.R")head( index )The concordance is an RDB to XML Crosswalk that maps all xpaths onto a specified set of variables. It is located in the irs990efile package.
The full data dictionary is available on GitHub.
concordance %>%
filter( rdb_table == table.name )
| xpath | id |
|---|---|
| /Return/ReturnData/IRS990/Form990PartVIII/ProgramServiceRevenue/BusinessCode | 1 |
| /Return/ReturnData/IRS990/Form990PartVIII/ProgramServiceRevenue/Description | 2 |
| /Return/ReturnData/IRS990/Form990PartVIII/ProgramServiceRevenue/ExclusionAmount | 3 |
| /Return/ReturnData/IRS990/Form990PartVIII/ProgramServiceRevenue/RelatedOrExemptFunctionIncome | 4 |
| /Return/ReturnData/IRS990/Form990PartVIII/ProgramServiceRevenue/TotalRevenueColumn | 5 |
| /Return/ReturnData/IRS990/Form990PartVIII/ProgramServiceRevenue/UnrelatedBusinessRevenue | 6 |
| /Return/ReturnData/IRS990/ProgramServiceRevenue/BusinessCode | 7 |
| /Return/ReturnData/IRS990/ProgramServiceRevenue/Description | 8 |
| /Return/ReturnData/IRS990/ProgramServiceRevenue/ExclusionAmount | 9 |
| /Return/ReturnData/IRS990/ProgramServiceRevenue/RelatedOrExemptFunctionIncome | 10 |
| /Return/ReturnData/IRS990/ProgramServiceRevenue/TotalRevenueColumn | 11 |
| /Return/ReturnData/IRS990/ProgramServiceRevenue/UnrelatedBusinessRevenue | 12 |
| /Return/ReturnData/IRS990/ProgramServiceRevenueGrp/BusinessCd | 13 |
| /Return/ReturnData/IRS990/ProgramServiceRevenueGrp/Desc | 14 |
| /Return/ReturnData/IRS990/ProgramServiceRevenueGrp/ExclusionAmt | 15 |
| /Return/ReturnData/IRS990/ProgramServiceRevenueGrp/RelatedOrExemptFuncIncomeAmt | 16 |
| /Return/ReturnData/IRS990/ProgramServiceRevenueGrp/TotalRevenueColumnAmt | 17 |
| /Return/ReturnData/IRS990/ProgramServiceRevenueGrp/UnrelatedBusinessRevenueAmt | 18 |
xpaths <- as.character( t.xpaths$xpath )
el <- create_edgelist_v1( xpaths )
nd <- FromDataFrameNetwork( network=el )
print( nd )## levelName
## 1 Return
## 2 °--ReturnData
## 3 °--IRS990
## 4 ¦--Form990PartVIII
## 5 ¦ °--ProgramServiceRevenue
## 6 ¦ ¦--BusinessCode
## 7 ¦ ¦--Description
## 8 ¦ ¦--ExclusionAmount
## 9 ¦ ¦--RelatedOrExemptFunctionIncome
## 10 ¦ ¦--TotalRevenueColumn
## 11 ¦ °--UnrelatedBusinessRevenue
## 12 ¦--ProgramServiceRevenue
## 13 ¦ ¦--BusinessCode
## 14 ¦ ¦--Description
## 15 ¦ ¦--ExclusionAmount
## 16 ¦ ¦--RelatedOrExemptFunctionIncome
## 17 ¦ ¦--TotalRevenueColumn
## 18 ¦ °--UnrelatedBusinessRevenue
## 19 °--ProgramServiceRevenueGrp
## 20 ¦--BusinessCd
## 21 ¦--Desc
## 22 ¦--ExclusionAmt
## 23 ¦--RelatedOrExemptFuncIncomeAmt
## 24 ¦--TotalRevenueColumnAmt
## 25 °--UnrelatedBusinessRevenueAmt
SetGraphStyle( nd, rankdir = "LR")
SetEdgeStyle( nd, arrowhead = "vee", color = "grey20", penwidth = 2 )
SetNodeStyle( nd,
style = "filled,rounded",
shape = "box",
fillcolor = "LightBlue",
fontname = "helvetica",
fontcolor="black",
tooltip = GetDefaultTooltip )
# SetNodeStyle(acme$IT, fillcolor = "LightBlue", penwidth = "5px")
plot( nd )# old version
find_group_names( table.name )## [1] "//Form990PartVIII" "//ProgramServiceRevenue"
## [3] "//ProgramServiceRevenueGrp"
# new version
find_table_headers( table.name )## [1] "//Form990PartVIII/FederatedCampaigns"
## [2] "//Form990PartVIII/FundraisingEvents"
## [3] "//Form990PartVIII/RelatedOrganizations"
## [4] "//Form990PartVIII/GovernmentGrants"
## [5] "//Form990PartVIII/AllOtherContributions"
## [6] "//Form990PartVIII/NoncashContributions"
## [7] "//Form990PartVIII/TotalContributions"
## [8] "//Form990PartVIII/ProgramServiceRevenue"
## [9] "//Form990PartVIII/TotalOthProgramServiceRevenue"
## [10] "//Form990PartVIII/TotalProgramServiceRevenue"
## [11] "//Form990PartVIII/InvestmentIncome"
## [12] "//Form990PartVIII/IncomeFromInvestBondProceeds"
## [13] "//Form990PartVIII/RoyaltiesRevenue"
## [14] "//Form990PartVIII/GrossRents"
## [15] "//Form990PartVIII/LessRentalExpenses"
## [16] "//Form990PartVIII/RentalIncomeOrLoss"
## [17] "//Form990PartVIII/NetRentalIncomeOrLoss"
## [18] "//Form990PartVIII/GrossAmountSalesAssets"
## [19] "//Form990PartVIII/LessCostOthBasisSalesExpenses"
## [20] "//Form990PartVIII/GainOrLoss"
## [21] "//Form990PartVIII/NetGainOrLossInvestments"
## [22] "//Form990PartVIII/CntrbtnsRprtdFundraisingEvents"
## [23] "//Form990PartVIII/GrossIncomeFundraisingEvents"
## [24] "//Form990PartVIII/FundraisingDirectExpenses"
## [25] "//Form990PartVIII/NetIncomeFromFundraisingEvents"
## [26] "//Form990PartVIII/GrossIncomeGaming"
## [27] "//Form990PartVIII/GamingDirectExpenses"
## [28] "//Form990PartVIII/NetIncomeFromGaming"
## [29] "//Form990PartVIII/GrossSalesOfInventory"
## [30] "//Form990PartVIII/CostOfGoodsSold"
## [31] "//Form990PartVIII/NetIncomeOrLoss"
## [32] "//Form990PartVIII/OtherRevenueMisc"
## [33] "//Form990PartVIII/MiscellaneousRevenue"
## [34] "//Form990PartVIII/TotalOtherRevenue"
## [35] "//Form990PartVIII/TotalRevenue"
## [36] "//ProgramServiceRevenue/Form990PartVIII"
## [37] "//ProgramServiceRevenue/ProgramServiceRevenue"
## [38] "//ProgramServiceRevenue/TotalOthProgramServiceRevenue"
## [39] "//ProgramServiceRevenueGrp/Desc"
## [40] "//ProgramServiceRevenueGrp/TotalRevenueColumnAmt"
## [41] "//ProgramServiceRevenueGrp/RelatedOrExemptFuncIncomeAmt"
## [42] "//ProgramServiceRevenueGrp/BusinessCd"
## [43] "//ProgramServiceRevenueGrp/UnrelatedBusinessRevenueAmt"
## [44] "//ProgramServiceRevenueGrp/ExclusionAmt"
# manual
if( ! is.null(table.headers) )
{table.headers}
# create new folder
dir.create( folder.name )
setwd( folder.name )
# create sample
set.seed( 1234 )
sample.index <- sample_n( index, sample.size )
write.csv( index, "sample-index.csv", row.names=F )
sample.urls <- sample.index$URL
# erase existing log files
file.create("XPATH-LOG.txt")
file.create("FAIL-LOG.txt")
start.build.time <- Sys.time() # --------------------
results.list <- list()
xpath.list <- list()
for( i in 1:length( sample.urls ) )
{
url <- sample.urls[i]
results.list[[i]] <-
build_rdb_table_v2( url,
table.name,
table.headers=table.headers )
xpath.list[[i]] <-
get_table_xpaths( url,
table.name,
table.headers=table.headers )
if( i %% 100 == 0 ){ print(i) }
}
end.build.time <- Sys.time() # --------------------
df <- dplyr::bind_rows( results.list )
fail.log <- readLines( "FAIL-LOG.txt" )
all.xpaths <- xpath.list %>% unlist() %>% unique() %>% sort()
cat( all.xpaths, sep="\n", file="XPATH-LOG.txt" )TABLE: F9-P08-T01-REVENUE-PROGRAMS
## [1] "NUMBER OF RETURNS PROCESSED: 1000"
## [1] "TOTAL NUMBER OF TABLE ROWS: 677"
TOTAL RUN TIME:
## Time difference of 17.33954 mins
## Time difference of -0.2889923 hours
write.csv( df, paste0( folder.name, "/", table.name, ".csv" ), row.names=F )
head(df) Are all names standardized?
names(df)## [1] "OBJECT_ID" "EIN"
## [3] "NAME" "TAXYR"
## [5] "FORMTYPE" "URL"
## [7] "F9_08_REV_PROG_DESC" "F9_08_REV_PROG_BIZCODE"
## [9] "F9_08_REV_PROG_TOT" "F9_08_REV_PROG_RLTD"
## [11] "F9_08_REV_PROG_EXCL" "F9_08_REV_PROG_UBIZ"
Check for xpaths that were used in the tables but do not exist in the Concordance file.
c.table <- dplyr::filter( concordance, rdb_table == table.name )
xpath.concordance <- c.table$xpath
setdiff( all.xpaths, xpath.concordance )## [1] ""
Capture to update the Concordance file.
these <- setdiff( all.xpaths, xpath.concordance )
cat( these, sep="\n", file=paste0( folder.name, "/MISSING-XPATHS.txt" ) )Check the status of failed URLs.
if( length(fail.log) > 0 )
{
sapply( fail.log, get_message ) %>%
table() %>%
pander()
} else {
print("ALL URLs ARE VALID")
}| Client error: (404) Not Found |
|---|
| 1 |
The create_edgelist() functions v1 and v2 are in R/utils.R.
V1 retains the root nodes Return and ReturnData.
V2 drops the root nodes and uses form versions “990” and “990EZ” as the root to visualize the sets of tables separately.
# KEEP ROOT NOTES Return & ReturnData
el <- create_edgelist_v1( all.xpaths )Edgelist format:
| V1 | V2 |
|---|---|
| Return | ReturnData |
| ReturnData | IRS990 |
| IRS990 | Form990PartVIISectionAGrp |
| Form990PartVIISectionAGrp | PersonNm |
| Form990PartVIISectionAGrp | TitleTxt |
| Form990PartVIISectionAGrp | AverageHoursPerWeekRt |
| Form990PartVIISectionAGrp | IndividualTrusteeOrDirectorInd |
| Form990PartVIISectionAGrp | OfficerInd |
| Form990PartVIISectionAGrp | ReportableCompFromOrgAmt |
| Form990PartVIISectionAGrp | ReportableCompFromRltdOrgAmt |
# library( data.tree )
nd <- FromDataFrameNetwork( network=el )
print( nd )## levelName
## 1 Return
## 2 °--ReturnData
## 3 °--IRS990
## 4 °--ProgramServiceRevenueGrp
## 5 ¦--BusinessCd
## 6 ¦--Desc
## 7 ¦--ExclusionAmt
## 8 ¦--RelatedOrExemptFuncIncomeAmt
## 9 ¦--TotalRevenueColumnAmt
## 10 °--UnrelatedBusinessRevenueAmt
SetGraphStyle( nd, rankdir = "LR")
SetEdgeStyle( nd, arrowhead = "vee", color = "grey20", penwidth = 2 )
SetNodeStyle( nd,
style = "filled,rounded",
shape = "box",
fillcolor = "LightBlue",
fontname = "helvetica",
fontcolor="black",
tooltip = GetDefaultTooltip )
# SetNodeStyle(acme$IT, fillcolor = "LightBlue", penwidth = "5px")
plot( nd )# library( networkD3 )
# net <- ToDataFrameNetwork( nd, "name" )
# simpleNetwork( net, fontSize = 12 )
simpleNetwork( el, fontSize = 12 )#plot with networkD3
radnet <- ToListExplicit( nd, unname = TRUE )
radialNetwork( radnet, fontSize = 12 )
t.xpaths <-
concordance %>%
filter( rdb_table == table.name ) %>%
select( xpath ) %>%
unique() %>%
arrange( xpath ) %>%
mutate( id=row_number() )
t.xpaths %>%
kable( align="l" )| xpath | id |
|---|---|
| /Return/ReturnData/IRS990/Form990PartVIII/ProgramServiceRevenue/BusinessCode | 1 |
| /Return/ReturnData/IRS990/Form990PartVIII/ProgramServiceRevenue/Description | 2 |
| /Return/ReturnData/IRS990/Form990PartVIII/ProgramServiceRevenue/ExclusionAmount | 3 |
| /Return/ReturnData/IRS990/Form990PartVIII/ProgramServiceRevenue/RelatedOrExemptFunctionIncome | 4 |
| /Return/ReturnData/IRS990/Form990PartVIII/ProgramServiceRevenue/TotalRevenueColumn | 5 |
| /Return/ReturnData/IRS990/Form990PartVIII/ProgramServiceRevenue/UnrelatedBusinessRevenue | 6 |
| /Return/ReturnData/IRS990/ProgramServiceRevenue/BusinessCode | 7 |
| /Return/ReturnData/IRS990/ProgramServiceRevenue/Description | 8 |
| /Return/ReturnData/IRS990/ProgramServiceRevenue/ExclusionAmount | 9 |
| /Return/ReturnData/IRS990/ProgramServiceRevenue/RelatedOrExemptFunctionIncome | 10 |
| /Return/ReturnData/IRS990/ProgramServiceRevenue/TotalRevenueColumn | 11 |
| /Return/ReturnData/IRS990/ProgramServiceRevenue/UnrelatedBusinessRevenue | 12 |
| /Return/ReturnData/IRS990/ProgramServiceRevenueGrp/BusinessCd | 13 |
| /Return/ReturnData/IRS990/ProgramServiceRevenueGrp/Desc | 14 |
| /Return/ReturnData/IRS990/ProgramServiceRevenueGrp/ExclusionAmt | 15 |
| /Return/ReturnData/IRS990/ProgramServiceRevenueGrp/RelatedOrExemptFuncIncomeAmt | 16 |
| /Return/ReturnData/IRS990/ProgramServiceRevenueGrp/TotalRevenueColumnAmt | 17 |
| /Return/ReturnData/IRS990/ProgramServiceRevenueGrp/UnrelatedBusinessRevenueAmt | 18 |
Identify all unique groups used in xpaths.
if( is.null(table.headers) )
{ find_table_headers( table.name ) } else
table.headers## [1] "//Form990PartVIII/FederatedCampaigns"
## [2] "//Form990PartVIII/FundraisingEvents"
## [3] "//Form990PartVIII/RelatedOrganizations"
## [4] "//Form990PartVIII/GovernmentGrants"
## [5] "//Form990PartVIII/AllOtherContributions"
## [6] "//Form990PartVIII/NoncashContributions"
## [7] "//Form990PartVIII/TotalContributions"
## [8] "//Form990PartVIII/ProgramServiceRevenue"
## [9] "//Form990PartVIII/TotalOthProgramServiceRevenue"
## [10] "//Form990PartVIII/TotalProgramServiceRevenue"
## [11] "//Form990PartVIII/InvestmentIncome"
## [12] "//Form990PartVIII/IncomeFromInvestBondProceeds"
## [13] "//Form990PartVIII/RoyaltiesRevenue"
## [14] "//Form990PartVIII/GrossRents"
## [15] "//Form990PartVIII/LessRentalExpenses"
## [16] "//Form990PartVIII/RentalIncomeOrLoss"
## [17] "//Form990PartVIII/NetRentalIncomeOrLoss"
## [18] "//Form990PartVIII/GrossAmountSalesAssets"
## [19] "//Form990PartVIII/LessCostOthBasisSalesExpenses"
## [20] "//Form990PartVIII/GainOrLoss"
## [21] "//Form990PartVIII/NetGainOrLossInvestments"
## [22] "//Form990PartVIII/CntrbtnsRprtdFundraisingEvents"
## [23] "//Form990PartVIII/GrossIncomeFundraisingEvents"
## [24] "//Form990PartVIII/FundraisingDirectExpenses"
## [25] "//Form990PartVIII/NetIncomeFromFundraisingEvents"
## [26] "//Form990PartVIII/GrossIncomeGaming"
## [27] "//Form990PartVIII/GamingDirectExpenses"
## [28] "//Form990PartVIII/NetIncomeFromGaming"
## [29] "//Form990PartVIII/GrossSalesOfInventory"
## [30] "//Form990PartVIII/CostOfGoodsSold"
## [31] "//Form990PartVIII/NetIncomeOrLoss"
## [32] "//Form990PartVIII/OtherRevenueMisc"
## [33] "//Form990PartVIII/MiscellaneousRevenue"
## [34] "//Form990PartVIII/TotalOtherRevenue"
## [35] "//Form990PartVIII/TotalRevenue"
## [36] "//ProgramServiceRevenue/Form990PartVIII"
## [37] "//ProgramServiceRevenue/ProgramServiceRevenue"
## [38] "//ProgramServiceRevenue/TotalOthProgramServiceRevenue"
## [39] "//ProgramServiceRevenueGrp/Desc"
## [40] "//ProgramServiceRevenueGrp/TotalRevenueColumnAmt"
## [41] "//ProgramServiceRevenueGrp/RelatedOrExemptFuncIncomeAmt"
## [42] "//ProgramServiceRevenueGrp/BusinessCd"
## [43] "//ProgramServiceRevenueGrp/UnrelatedBusinessRevenueAmt"
## [44] "//ProgramServiceRevenueGrp/ExclusionAmt"
Groups can then be used to extract the 1:M RDB tables:
if( is.null(table.headers) )
{ table.headers <- find_table_headers( table.name ) }
demo.url <- "https://nccs-efile.s3.us-east-1.amazonaws.com/xml/201913079349300206_public.xml"
doc <- xml2::read_xml( demo.url )
xml2::xml_ns_strip( doc )
df.demo <- get_table_v2( doc, table.name, table.headers )
df.demo ## # A tibble: 2 × 4
## Desc BusinessCd TotalRevenueColumnAmt RelatedOrExemptFuncIncome…¹
## <chr> <chr> <chr> <chr>
## 1 Player Fees 713990 221745 221745
## 2 Training 713990 3510 3510
## # … with abbreviated variable name ¹RelatedOrExemptFuncIncomeAmt
Mapping of xml variable names to concordance variable names:
get_var_map( table.name=table.name )A table with proper names:
v.map <- get_var_map( table.name=table.name )
if( ! is.null(df.demo) )
{re_name( df.demo, v.map ) %>% as.data.frame()}## F9_08_REV_PROG_DESC F9_08_REV_PROG_BIZCODE F9_08_REV_PROG_TOT
## 1 Player Fees 713990 221745
## 2 Training 713990 3510
## F9_08_REV_PROG_RLTD
## 1 221745
## 2 3510
Demo of functions - demo data only.
[[1]]
<Form990PartVIISectionAGrp>
<PersonNm>
{text}
<TitleTxt>
{text}
<AverageHoursPerWeekRt>
{text}
<IndividualTrusteeOrDirectorInd>
{text}
<OfficerInd>
{text}
<ReportableCompFromOrgAmt>
{text}
<ReportableCompFromRltdOrgAmt>
{text}
<OtherCompensationAmt>
{text}
node.list[[1]] %>% xml_parent() %>% xmltools::xml_view_trees() (1) ------------
+-- Form990PartVIISectionAGrp
+-- PersonNm
+-- TitleTxt
+-- AverageHoursPerWeekRt
+-- IndividualTrusteeOrDirectorInd
+-- OfficerInd
+-- ReportableCompFromOrgAmt
+-- ReportableCompFromRltdOrgAmt
++-- OtherCompensationAmt
(2) ------------
+-- Form990PartVIISectionAGrp
+-- PersonNm
+-- TitleTxt
+-- AverageHoursPerWeekRt
+-- IndividualTrusteeOrDirectorInd
+-- OfficerInd
+-- ReportableCompFromOrgAmt
+-- ReportableCompFromRltdOrgAmt
++-- OtherCompensationAmt