Skip to content

Commit

Permalink
Merge branch 'release-0.2.12'
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewHeun committed Jan 7, 2019
2 parents be008c2 + 2092f7b commit 13e032b
Show file tree
Hide file tree
Showing 50 changed files with 1,301 additions and 512 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
^Meta$
^doc$
^docs$
^_pkgdown\.yml$
^.*\.Rproj$
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Meta
doc
# History files
.Rhistory
.Rapp.history
Expand Down
7 changes: 4 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
Package: matsindf
Type: Package
Title: Work with matrices in data frames
Version: 0.2.11
Date: 2018-11-13
Version: 0.2.12
Date: 2018-01-07
Authors@R: c(person("Matthew", "Heun", role = c("aut", "cre"),
comment = c(ORCID = "0000-0002-7438-214X"),
email = "[email protected]"))
Author: Matthew Heun [aut, cre]
Maintainer: Matthew Heun <[email protected]>
Description: Provides functions to collapse a tidy data frame into matrices in a data frame
and expand a data frame of matrices into a tidy data frame.
License: MIT + file LICENSE
Language: en-US
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.1.1
Expand All @@ -22,6 +22,7 @@ Imports:
magrittr,
purrr,
rlang,
rlist,
tibble,
tidyr
Suggests:
Expand Down
23 changes: 21 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
YEAR: 2017
COPYRIGHT HOLDER: Matthew Kuperus Heun
MIT License

Copyright (c) 2019

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export("%>%")
export(collapse_to_matrices)
export(expand_to_tidy)
export(index_column)
Expand All @@ -25,7 +26,7 @@ importFrom(dplyr,summarise)
importFrom(dplyr,ungroup)
importFrom(magrittr,"%>%")
importFrom(matsbyname,coltype)
importFrom(matsbyname,elementquotient_byname)
importFrom(matsbyname,quotient_byname)
importFrom(matsbyname,rowtype)
importFrom(matsbyname,setcoltype)
importFrom(matsbyname,setrowtype)
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# matsindf 0.2.12 (2018-01-07)

* Now importing the `magrittr` pipe at a high level.


# matsindf 0.2.11 (2018-11-13)

* Now creating README.md from README.Rmd for `pkgdown` documentation.
Expand Down
3 changes: 1 addition & 2 deletions R/collapse.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
#' @importFrom tidyr spread
#'
#' @examples
#' library(magrittr)
#' library(dplyr)
#' library(tidyr)
#' library(tibble)
Expand Down Expand Up @@ -112,7 +111,7 @@ collapse_to_matrices <- function(.DF, matnames = "matnames", matvals = "matvals"
if (any(c(matvals, rownames, colnames, rowtypes, coltypes) %in% groups(.DF))) {
cant_group <- c(rownames, colnames, rowtypes, coltypes, matvals)
violator <- which(cant_group %in% groups(.DF))
stop(paste(cant_group[[violator]], " are grouping variables.",
stop(paste(cant_group[[violator]], "is/are grouping variable/s.",
"Cannot group on rownames, colnames,",
"rowtypes, coltypes, or matvals in argument .DF of collapse_to_matrices."))
}
Expand Down
1 change: 0 additions & 1 deletion R/expand.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
#' @export
#'
#' @examples
#' library(magrittr)
#' library(dplyr)
#' library(matsbyname)
#' ptype <- "Products"
Expand Down
42 changes: 38 additions & 4 deletions R/matsindf_apply.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#' \code{...} takes precedence.
#'
#' \code{NULL} arguments in ... are ignored for the purposes of deciding whether
#' all arguments are numbers, matrices, lists of numbers of matrieces, or named character strings.
#' all arguments are numbers, matrices, lists of numbers of matrices, or named character strings.
#' However, all \code{NULL} arguments are passed to \code{FUN},
#' so \code{FUN} should be able to deal with \code{NULL} arguments appropriately.
#'
Expand Down Expand Up @@ -96,6 +96,13 @@
#' # Warning is issued when an output item has same name as an input item.
#' \dontrun{matsindf_apply(list(a = 1, b = 2, c = 10), FUN = example_fun, a = "c", b = "b")}
matsindf_apply <- function(.dat = NULL, FUN, ...){
if (!is.null(.dat)) {
if (!is.list(.dat)) {
# If we get here, we have a value for .dat that doesn't make sense.
# Throw an error.
stop(".dat must be a data frame or a list in matsindf_apply, was ", class(.dat))
}
}
types <- matsindf_apply_types(...)
# Note that is.list(.dat) covers the cases where .dat is either a list or a data frame.
if (is.list(.dat) & types$dots_present & !types$all_dots_char) {
Expand Down Expand Up @@ -169,9 +176,36 @@ matsindf_apply <- function(.dat = NULL, FUN, ...){
if (is.list(.dat)) {
return(c(.dat, result))
}
# If we get here, we have a value for .dat that doesn't make sense.
# Throw an error.
stop(".dat must be a data frame or a list in matsindf_apply, was ", class(.dat))
}

# Some arguments could be coming in as strings while all other arguments are of same type.
# This outcome is possible when
# (1) .dat is missing and
# (2) an outer function has string defaults for names and
# (3) some of the arguments are missing.
# To put it another way, when there is no .dat and some arguments are specified as strings,
# it means that the string arguments are unavailable (missing).
# In a last-ditch effort, let's try to
# * elimiante all strings from ...
# * re-call this function with the remaining arguments
# This approach will, in effect, call FUN with missing arguments.
# If FUN can handle the missing arguments,
# we'll get a result.
# If FUN can't handle the missing arguments,
# an error will occur.
if (is.null(.dat)) {
dots <- list(...)
chars <- lapply(dots, function(x) is.character(x)) %>% as.logical()
dots <- rlist::list.remove(dots, range = which(chars))
if (length(dots) == 0) {
# We have eliminated all of the arguments.
# This is most certainly an error.
# And calling ourselves again would result in a stack overflow.
stop(".dat was missing and all arguments were strings")
}
# Now that we have eliminated the missing string arguments,
# call ourselves again.
return(do.call(matsindf_apply, args = c(list(.dat = NULL, FUN = FUN), dots)))
}

# If we get here, we don't know how to deal with our inputs.
Expand Down
9 changes: 3 additions & 6 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#' @importFrom matsbyname coltype
#'
#' @examples
#' library(magrittr)
#' library(matsbyname)
#' data <- data.frame(Country = c("GH", "GH", "GH"),
#' rows = c( "c1", "c1", "c2"),
Expand Down Expand Up @@ -103,7 +102,6 @@ mat_to_rowcolval <- function(.matrix, matvals = "matvals",
#' @importFrom matsbyname coltype
#'
#' @examples
#' library(magrittr)
#' library(matsbyname)
#' library(dplyr)
#' data <- data.frame(Country = c("GH", "GH", "GH"),
Expand Down Expand Up @@ -226,7 +224,7 @@ rowcolval_to_mat <- function(.DF, matvals = "matvals",
#' and one additional column containing indexed \code{var_to_index}
#' named with the value of \code{indexed_var}.
#'
#' @importFrom matsbyname elementquotient_byname
#' @importFrom matsbyname quotient_byname
#' @importFrom dplyr inner_join
#' @importFrom dplyr rename
#' @importFrom dplyr right_join
Expand All @@ -235,7 +233,6 @@ rowcolval_to_mat <- function(.DF, matvals = "matvals",
#'
#' @examples
#' library(dplyr)
#' library(magrittr)
#' library(tidyr)
#' DF <- data.frame(Year = c(2000, 2005, 2010), a = c(10, 15, 20), b = c(5, 5.5, 6)) %>%
#' gather(key = name, value = var, a, b) %>%
Expand Down Expand Up @@ -278,6 +275,7 @@ index_column <- function(.DF, var_to_index, time_var = "Year", index_time = NULL
) %>%
inner_join(.DF, by = c(group_vars(.DF), as.character(time_var)))
} else {
# We have an index_time and should use it.
# Set IndexYearData to data from index year for each group.
IndexYearData <- .DF %>%
filter(!!time_var == index_time)
Expand All @@ -295,7 +293,7 @@ index_column <- function(.DF, var_to_index, time_var = "Year", index_time = NULL
right_join(IndexYearData, by = group_vars(.DF)) %>%
mutate(
# !!indexed_var := !!var_to_index / !!var_to_index_init
!!indexed_var := elementquotient_byname(!!var_to_index, !!var_to_index_init)
!!indexed_var := quotient_byname(!!var_to_index, !!var_to_index_init)
) %>%
# Remove var_to_index_init
select(-(!!var_to_index_init))
Expand Down Expand Up @@ -428,7 +426,6 @@ add_UKEnergy2000_matnames <- function(.DF,
#' \code{rowtype_colname}, and \code{coltype_colname}.
#'
#' @examples
#' library(magrittr)
#' UKEnergy2000 %>%
#' matsindf:::add_UKEnergy2000_matnames(.) %>%
#' matsindf:::add_UKEnergy2000_row_col_meta(.)
Expand Down
11 changes: 11 additions & 0 deletions R/utils-pipe.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#' Pipe operator
#'
#' See \code{magrittr::\link[magrittr]{\%>\%}} for details.
#'
#' @name %>%
#' @rdname pipe
#' @keywords internal
#' @export
#' @importFrom magrittr %>%
#' @usage lhs \%>\% rhs
NULL
48 changes: 34 additions & 14 deletions docs/LICENSE-text.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 13e032b

Please sign in to comment.