knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(usda.arms.api)
The Department of Agriculture of the USA has made the data from its Annual Resource Management Survey accessible through an API. The survey has contains data on farm companies and families finances and resource utilization. The data is organized by state and year.
The base functionality is confined to two functions: pull_data(), which makes a call to the API with the options specified in its arguments, and returns the response data, and refresh_metadata(), which collects all possible values for the five variables used as input for pull_data().
The function pull_data() takes 5 arguments: year, report type, state, farm type and variable. State, farm type and variable have default values of, respectively, "all [states]", "all farm types" and "all [variables]".
There are more granular subcategories in the API; these are not included as filtering options in the pull_data()-function, but they are available as visualization options in the Shiny app.
apiCall1 <- pull_data(c(2015,2016,2017),"Farm Business Balance Sheet") apiCall1[1,c(1:10,18)]
The API has a limit of a maximum of 1000 calls per hour; should the remaining calls in a given hour drop below 50, a warning will be thrown from the function.
We need metadata (specifically all the possible values of the 5 above-mentioned categories as well as their subcategories) in order to populate the drop-down menus in the control interfaces of the shiny app as well as for some internal uses in the pull_data()-function. The refresh_metadata()-function downloads this information for us. While it is called automatically when the package is loaded, it can also be called in the shiny app by way of pressing the "update metadata"-button.
refresh_metadata() takes no arguments.
metadata <- refresh_metadata()
##attempt at an embedded display for the shiny app. Get back to it later, maybe? #shinyDirectory=paste0(dirname(getwd()),"/shiny_app/") ##the following only works if the user is already running the shiny app on their machine #knitr::include_app("http://127.0.0.1:5806")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.