Skip to content

Commit

Permalink
Updated documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
byrongibby committed Apr 24, 2024
1 parent d0d26a2 commit 5a242bb
Show file tree
Hide file tree
Showing 13 changed files with 270 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
*.prj
.lsp
.lsp/*
11 changes: 6 additions & 5 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
importFrom("httr", "POST", "GET", "PUT", "accept", "accept_json", "authenticate",
"content", "content_type", "cookies", "set_cookies", "upload_file")
importFrom("jsonlite", "fromJSON", "toJSON", "unbox")
importFrom("readODS", "write_ods", "read_ods")
importFrom("collapse", "add_vars<-", "ckmatch", "dapply", "fmutate", "fnobs",
"frename", "get_vars<-", "qDT", "qF", "ss", "unlist2d", "vlabels",
"vlabels<-")
importFrom("data.table", "dcast", "rbindlist", "setcolorder")
importFrom("utils", "sessionInfo")
importFrom("httr", "POST", "GET", "PUT", "accept", "accept_json", "authenticate",
"content", "content_type", "cookies", "set_cookies", "upload_file")
importFrom("jsonlite", "fromJSON", "toJSON", "unbox")
importFrom("readODS", "write_ods", "read_ods")
importFrom("stats", "na.omit")
importFrom("utils", "URLdecode", "head", "tail")

export(read_dataset)
export(read_database)
Expand Down
6 changes: 5 additions & 1 deletion R/tidy_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ tidy_long <- function(x, combine = FALSE, allmeta = FALSE, origmeta = FALSE, pre
}

# Tidying the output of read_release()
tidy_data.eds_release <- function(x) {
tidy_data.eds_release <- function(x, ...) {
axnull <- is.null(attributes(x))
if (axnull && length(x) > 1L) {
res <- lapply(x, tidy_data.eds_release)
Expand Down Expand Up @@ -179,4 +179,8 @@ tidy_data.eds_database <- function(x, ...) {
rbindlist()
}

tidy_data.eds_registry <- function(x, ...) {
return(x)
}

tidy_data <- function(x, ...) UseMethod("tidy_data", x)
14 changes: 3 additions & 11 deletions R/write_database.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,10 @@ write_database <- function(x, method = "update", ...) {
login_helper(credentials, env$repository$url)
}
header <- list()
header$id <- tryCatch(
unbox(paste0("ECONDATAR-V",
sessionInfo()[[7]]$econdatar[[4]])),
error = function(e) {
unbox("Unknown")
})
header$id <- unbox("ECONDATAR")
header$prepared <- unbox(format(Sys.time(), format = "%Y-%m-%dT%T"))
header$sender <- tryCatch(
unbox(Sys.getenv()[["USER"]]),
error = function(e) {
unbox("Anonymous")
})
header$sender <- tryCatch(unbox(Sys.getenv()[["USER"]]),
error = function(e) unbox("Anonymous"))
header$receiver <- unbox("EconData web application")
if (any(names(x) == "data-sets")) {
datasets <- x[["data-sets"]]
Expand Down
71 changes: 71 additions & 0 deletions man/read_database.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
\name{read_database}
\alias{read_database}
\alias{tidy_data.eds_database}
\title{
read_database
}
\description{
Returns the data sets for the given reference - agencyid:id(version), as a list, or as a tidy \emph{data.table}. A collection of data sets in \emph{econdatar} is referred to as a database. Tidying can be done directly within \code{read_database()}, or ex-post using \code{tidy_data()}.
}
\usage{
read_database(id, include_series = FALSE, tidy = FALSE, \dots)

\method{tidy_data}{eds_database}(x, \dots)
}
\arguments{
\item{id}{Data set identifier. May be \code{"all"}}
\item{include_series}{If \code{TRUE} series (and their metadata) with obervations omitted are included in the return (series are discarded when \emph{tidy} is \code{TRUE}).}
\item{x}{A raw API return object to be tidied. Can also be done directly in \code{read_database()} by setting \code{tidy = TRUE}}.

\item{\dots}{Further \emph{Optional} arguments:
\tabular{llll}{
\code{agencyid} \tab\tab character. Defaults to \code{ECONDATA}. Agency responsible for the metadata creation/maintenance. \cr
\code{version} \tab\tab character. Version(s) of the data (different versions will have different metadata), or \code{"all"} to return all available versions. \cr
\code{username} \tab\tab character. Web username. \cr
\code{password} \tab\tab character. Web password. \cr
}
}

\item{tidy}{logical. Return data and metadata in tidy \emph{data.table}'s (see Value), by passing the result through \code{tidy_data}.}
}
\details{
An EconData account (http://econdata.co.za) is required to use this function. The user must provide their credentials either through the function arguments, or by setting the ECONDATA_CREDENTIALS environment variable using the syntax: "username;password", e.g. \code{Sys.setenv(ECONDATA_CREDENTIALS="username;password")}. If credentials are not supplied by the aforementioned methods a GUI dialog will prompt the user for credentials.
}
\value{
%% ~Describe the value returned
If \code{tidy = FALSE}, an unnamed list of data frames is returned. Each data frame further has a \code{"metadata"} attribute providing information about the data set.
If \code{tidy = TRUE} a single \emph{data.table} is returned with columns for \code{"agencyid"}, \code{"id"}, \code{"version"}, \code{"name"}, and \code{"description"}.
%% \item{comp1 }{Description of 'comp1'}
%% \item{comp2 }{Description of 'comp2'}
%% ...
}
\seealso{
%% ~~objects to See Also as \code{\link{help}}, ~~~
\code{\link{write_database}}
}
\examples{
\dontrun{
# library(econdatar)
# Sys.setenv(ECONDATA_CREDENTIALS="username;password")
# Mining production and sales
MINING <- read_database(id = "MINING")
# Include previous versions
MINING <- read_database(id = "MINING", versions = "all")
# Return series without obervations (empty data set)
MINING <- read_database(id = "MINING", include_series = TRUE)
# Return all data sets (useful for browsing available data)
MINING <- read_database(id = "all")
tidy_data(MINING)
}
}
% Add one or more standard keywords, see file 'KEYWORDS' in the
% R documentation directory.
\keyword{ load }% use one of RShowDoc("KEYWORDS")
\keyword{ download }% __ONLY ONE__ keyword per line
7 changes: 4 additions & 3 deletions man/read_dataset.Rd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
\name{read_dataset}
\alias{read_dataset}
\alias{tidy_data}
\alias{tidy_data.eds_dataset}
\title{
read_dataset
}
Expand All @@ -10,15 +10,16 @@ Returns the data for the given data set - ECONDATA:id(version), as a list, or as
\usage{
read_dataset(id, tidy = FALSE, \dots)
tidy_data(x, \dots)
\method{tidy_data}{eds_dataset}(x, wide = TRUE, \dots)
}
\arguments{
\item{id}{Data set identifier.}
\item{x}{A raw API return object to be tidied. Can also be done directly in \code{read_dataset()} by setting \code{tidy = TRUE}. See \code{tidy} below for tidying options.}
\item{wide}{Should the tidied data be returned in \emph{wide} or \emph{long} format.}
\item{\dots}{Further \emph{Optional} arguments:
\tabular{llll}{
\code{agencyid} \tab\tab character. Agency responsible for the metadata creation/maintenance. \cr
\code{agencyid} \tab\tab character. Defaults to \code{ECONDATA}. Agency responsible for the metadata creation/maintenance. \cr
\code{version} \tab\tab character. Version(s) of the data (different versions will have different metadata), or 'all' to return all available versions. \cr
\code{series_key} \tab\tab character. A character vector specifying a subset of time series (see the web platform (export function) for details). \cr
\code{release} \tab\tab character (optionally with format \%Y-\%m-\%dT\%H:\%M:\%S, to be coerced to a date/time). The release description, which will return the data associated with that release (if the given description matches an existsing release); or a date/time which will return the data as it was at the given time; or 'latest' which will return the latest release; or 'unreleased' which will return any unreleased data (useful for data that is updated more often than it is released, e.g. daily data). \cr
Expand Down
26 changes: 13 additions & 13 deletions man/read_econdata.Rd
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
\name{read_econdata}
\alias{read_econdata}
\alias{econdata_tidy}
\alias{tidy_data}
\title{
read_econdata
}
\description{
Returns the data for the given data set - ECONDATA:id(version), as a list, or as tidy \emph{data.table}'s. Available data sets can be looked up from the web platform (http://www.econdata.co.za). Tidying can be done directly within \code{read_econdata()}, or ex-post using \code{econdata_tidy()}.
Returns the data for the given data set - ECONDATA:id(version), as a list, or as tidy \emph{data.table}'s. Available data sets can be looked up from the web platform (http://www.econdata.co.za). Tidying can be done directly within \code{read_econdata()}, or ex-post using \code{tidy_data()}.
}
\usage{
read_econdata(id, \dots, tidy = FALSE)
read_econdata(id, tidy = FALSE, \dots)
econdata_tidy(x, \dots)
tidy_data(x, \dots)
}
\arguments{
\item{id}{Data identifier.}
\item{x}{A raw API return object to be tidied. Can also be done directly in \code{read_econdata()} by setting \code{tidy = TRUE}. See \code{tidy} below for tidying options.}
\item{\dots}{Further \emph{Optional} arguments:
\tabular{llll}{
\code{agencyid} \tab\tab character. Agency responsible for the metadata creation/maintenance. \cr
\code{agencyid} \tab\tab character. Defaults to \code{ECONDATA}. Agency responsible for the metadata creation/maintenance. \cr
\code{version} \tab\tab character. Version(s) of the data (different versions will have different metadata), or 'all' to return all available versions. \cr
\code{series_key} \tab\tab character. A character vector specifying a subset of time series (see the web platform (export function) for details). \cr
\code{release} \tab\tab character (optionally with format \%Y-\%m-\%dT\%H:\%M:\%S, that will be coerced to a date/time). The release description, which will return the data associated with that release (if the given description matches an existsing release); or a date/time which will return the data as it was at that moment; or 'latest' which will return the latest release; or 'unreleased' which will return any unreleased data (useful for data that is updated more often than it is released, usually daily data). \cr
Expand All @@ -28,7 +28,7 @@ econdata_tidy(x, \dots)
}
}
\item{tidy}{logical. Return data and metadata in tidy \emph{data.table}'s (see Value), by passing the result through \code{econdata_tidy}. If \code{TRUE}, \code{read_econdata()/econdata_tidy()} admit the following additional arguments:
\item{tidy}{logical. Return data and metadata in tidy \emph{data.table}'s (see Value), by passing the result through \code{tidy_data}. If \code{TRUE}, \code{read_econdata()/tidy_data()} admit the following additional arguments:
\tabular{llll}{
\code{wide} \tab\tab logical, default: \code{TRUE}. Returns data in a column-based format, with \code{"label"} and \code{"source_identifier"} attributes to columns (when available) and an overall "metadata" attribute to the table, otherwise a long-format is returned. See Value. \cr

Expand All @@ -42,7 +42,7 @@ econdata_tidy(x, \dots)

\code{prettymeta} \tab\tab logical, default: \code{TRUE}. Attempts to make the returned metadata more human readable replacing each code category and enumeration with its name. It is advisable to leave this set to \code{TRUE}, in some cases, where speed is paramount you may want to set this flag to \code{FALSE}. If multiple datasets are being querioed this option is automatically set to \code{FALSE}. \cr

\code{release} \tab\tab logical, default: \code{FALSE}. \code{TRUE} allows you to apply \code{econdata_tidy()} to objects returned by \code{read_release()}. All other flags to \code{econdata_tidy()} are ignored.
\code{release} \tab\tab logical, default: \code{FALSE}. \code{TRUE} allows you to apply \code{tidy_data()} to objects returned by \code{read_release()}. All other flags to \code{tidy_data()} are ignored.
}
}
}
Expand Down Expand Up @@ -75,16 +75,16 @@ If \code{tidy = TRUE} and \code{wide = FALSE} and \code{compact = FALSE} (the de

# Electricity Generated
ELECTRICITY <- read_econdata(id = "ELECTRICITY")
ELECTRICITY_WIDE <- econdata_tidy(ELECTRICITY) # Or: read_econdata("ELECTRICITY", tidy = TRUE)
ELECTRICITY_LONG <- econdata_tidy(ELECTRICITY, wide = FALSE)
# Same as econdata_tidy(ELECTRICITY, wide = FALSE, combine = TRUE):
ELECTRICITY_WIDE <- tidy_data(ELECTRICITY) # Or: read_econdata("ELECTRICITY", tidy = TRUE)
ELECTRICITY_LONG <- tidy_data(ELECTRICITY, wide = FALSE)
# Same as tidy_data(ELECTRICITY, wide = FALSE, combine = TRUE):
with(ELECTRICITY_LONG, metadata[data, on = "data_key"])

# CPI Analytical Series: Different Revisions
CPI_ANL <- read_econdata(id = "CPI_ANL_SERIES", version = "all")
CPI_ANL_WIDE <- econdata_tidy(CPI_ANL)
CPI_ANL_LONG <- econdata_tidy(CPI_ANL, wide = FALSE, combine = TRUE)
CPI_ANL_ALLMETA <- econdata_tidy(CPI_ANL, wide = FALSE, allmeta = TRUE) # v2.0 has some 0-obs series
CPI_ANL_WIDE <- tidy_data(CPI_ANL)
CPI_ANL_LONG <- tidy_data(CPI_ANL, wide = FALSE, combine = TRUE)
CPI_ANL_ALLMETA <- tidy_data(CPI_ANL, wide = FALSE, allmeta = TRUE) # v2.0 has some 0-obs series

# Can query a specific version by adding e.g. version = "2.0.0" to the call

Expand Down
60 changes: 60 additions & 0 deletions man/read_registry.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
\name{read_registry}
\alias{read_registry}
\alias{tidy_data.eds_registry}
\title{
read_registry
}
\description{
Returns the data for the given registry structure - agencyid:id(version).
}
\usage{
read_registry(structure, tidy = FALSE, \dots)

\method{tidy_data}{eds_registry}(x, \dots)
}
\arguments{
\item{structure}{structure type. One of \code{category-scheme}, \code{codelist}, \code{concept-scheme}, \code{dataflow}, \code{data-structure}, \code{memberlist}, \code{consumption-agreement}, or \code{provision-agreement}}
\item{x}{A raw API return object to be tidied. Can also be done directly in \code{read_registry()} by setting \code{tidy = TRUE}. See \code{tidy} below for tidying options.}

\item{\dots}{Further \emph{Optional} arguments:
\tabular{llll}{
\code{agencyid} \tab\tab character. Defaults to \code{ECONDATA}. Agency responsible for the metadata creation/maintenance. \cr
\code{version} \tab\tab character. Version(s) of the data (different versions will have different metadata), or 'all' to return all available versions. \cr
\code{file} \tab\tab character. File name for retrieving structures stored as JSON data from disk (output of \code{read_registry()}). \cr
\code{username} \tab\tab character. Web username. \cr
\code{password} \tab\tab character. Web password. \cr
}
}

\item{tidy}{logical. Return data and metadata in tidy \emph{data.table}'s (see Value), by passing the result through \code{tidy_data}. Currently not used.
}
}
\details{
An EconData account (http://econdata.co.za) is required to use this function. The user must provide their credentials either through the function arguments, or by setting the ECONDATA_CREDENTIALS environment variable using the syntax: "username;password", e.g. \code{Sys.setenv(ECONDATA_CREDENTIALS="username;password")}. If credentials are not supplied by the aforementioned methods a GUI dialog will prompt the user for credentials.
}
\value{
%% ~Describe the value returned
If \code{tidy = FALSE}, a list detailing at a minimum the structure's agencyid, id, version, name, and description, as well as a data.frame with rows containing the data of any child structures.

%% \item{comp1 }{Description of 'comp1'}
%% \item{comp2 }{Description of 'comp2'}
%% ...
}

\seealso{
%% ~~objects to See Also as \code{\link{help}}, ~~~
\code{\link{write_registry}}
}
\examples{
\dontrun{
# library(econdatar)
# Sys.setenv(ECONDATA_CREDENTIALS="username;password")

# Frequency codelist
CL_FREQ <- read_registry("codelist", id = "CL_FREQ")
}
}
% Add one or more standard keywords, see file 'KEYWORDS' in the
% R documentation directory.
\keyword{ load }% use one of RShowDoc("KEYWORDS")
\keyword{ download }% __ONLY ONE__ keyword per line
6 changes: 4 additions & 2 deletions man/read_release.Rd
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
\name{read_release}
\alias{read_release}
\alias{tidy_data.eds_release}
\title{
read_release
}
\description{
Returns the data for the given data set - ECONDATA:id(version), as a list, or as tidy \emph{data.table}'s. Tidying can be done directly within \code{read_econdata()}, or ex-post using \code{econdata_tidy()}.
}
\usage{
read_release(id, \dots, tidy = FALSE)
read_release(id, tidy = FALSE, \dots)
\method{tidy_data}{eds_release}(x, \dots)
}
\arguments{
\item{id}{Data identifier.}
\item{x}{A raw API return object to be tidied. Can also be done directly in \code{read_release()} by setting \code{tidy = TRUE}. See \code{tidy} below for tidying options.}
\item{\dots}{Further \emph{Optional} arguments:
\tabular{llll}{
\code{agencyid} \tab\tab Agency responsible for the data definition. \cr
\code{agencyid} \tab\tab character. Defaults to \code{ECONDATA}. Agency responsible for the metadata creation/maintenance. \cr
\code{version} \tab\tab Version of the data definition. \cr
\code{before} \tab\tab datetime. Returns all releases before a certain date. \cr
\code{after} \tab\tab datetime. Returns all releases after a certain date. \cr
Expand Down
46 changes: 46 additions & 0 deletions man/write_database.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
\name{write_database}
\alias{write_database}
\title{
write_database
}
\description{
Create or update a data set (metadata including series and their metadata).
}
\usage{
write_database(x, method = "update", \dots)
}
\arguments{
\item{x}{Data set to upload.}
\item{method}{Desired method. One of \code{create} or \code{update}. \code{create} will create a new data set with the provided metadata. \code{update} (default) will update an existing data set in the database.}

\item{\dots}{Further \emph{Optional} arguments:
\tabular{llll}{
\code{file} \tab\tab character. File name for saving data set as JSON data to disk. \cr
\code{username} \tab\tab character. EconData username. \cr
\code{password} \tab\tab character. EconData password. \cr
}
}
}
\details{
An EconData account (http://econdata.co.za) is required to use this function. The user must provide their credentials either through the function arguments, or by setting the ECONDATA_CREDENTIALS environment variable using the syntax: "username;password". If credentials are not supplied by the aforementioned methods a GUI dialog will prompt the user for credentials.

The functionality provided by \emph{write_database} is to save the data set according to the function arguments. As this makes modifications to the database the user calling this function requires higher privileges than needed for other \emph{econdatar} functions - the user requires \emph{membership} with the relevant data provider.
}

%% ~Make other sections like Warning with \section{Warning }{....} ~

\seealso{
%% ~~objects to See Also as \code{\link{help}}, ~~~
\code{\link{read_database}}
}
\examples{
\dontrun{
x <- read_database("MINING")

write_database(x)
}
}
% Add one or more standard keywords, see file 'KEYWORDS' in the
% R documentation directory.
\keyword{ save }% use one of RShowDoc("KEYWORDS")
\keyword{ upload }% __ONLY ONE__ keyword per line
4 changes: 2 additions & 2 deletions man/write_dataset.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ write_dataset
Saves the data set. Available data sets can be looked up from the web platform (http://econdata.co.za).
}
\usage{
write_dataset(x, method = c("stage", "validate"), \dots)
write_dataset(x, method = "stage", \dots)
}
\arguments{
\item{x}{Data set to upload.}
\item{method}{Desired method. "stage" will stage the given data making it ready for release. "validate" will validate the given data against the schema derived from the data structure definition.}
\item{method}{Desired method. One of \code{stage} or \code{validate}. \code{stage} will stage the given data making it ready for release. \code{validate} will validate the given data against the schema derived from the data structure definition.}

\item{\dots}{Further \emph{Optional} arguments:
\tabular{llll}{
Expand Down
Loading

0 comments on commit 5a242bb

Please sign in to comment.