Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mutate.R and setLabels.R #24

Merged
merged 39 commits into from
Jun 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
73296e5
Add mutate.R and setLabels.R
eoduniyi Jun 9, 2020
0c00995
Remove hy_update_labels.R and transmute.R
eoduniyi Jun 12, 2020
47f9328
Update testthat.R
eoduniyi Jun 12, 2020
2e1e496
Remove hy_update_labels from select.R
eoduniyi Jun 12, 2020
ff76912
Update select.R
eoduniyi Jun 12, 2020
0e02161
Add setLabels.R
eoduniyi Jun 12, 2020
16fe4e2
Add mutate.R
eoduniyi Jun 12, 2020
8393dcb
Update documentation to reference hySpc.dplyr
eoduniyi Jun 12, 2020
17dcf89
Add rlang::enquos dependency
eoduniyi Jun 12, 2020
3aa5af6
Update vignette for mutate.R
eoduniyi Jun 12, 2020
e808960
Remove hy_update_labels.R and transmute.R
eoduniyi Jun 12, 2020
791b7c5
Import fauxCell
eoduniyi Jun 12, 2020
2690a54
Replace `fauxCell` with `chondro`
eoduniyi Jun 12, 2020
361d649
Replace `fauxCell` with `chondro` in vignette
eoduniyi Jun 12, 2020
cb9df84
merge devel and resolve conflicts
cbeleites Jun 12, 2020
bb4dbcf
move transmute to its own file
cbeleites Jun 12, 2020
f3e4499
delete hy_update_labels
cbeleites Jun 12, 2020
4c7748d
Update unittest.R
eoduniyi Jun 12, 2020
ba03871
Merge branch 'feature/12-mutate-setLabels' of https://github.com/r-hy…
eoduniyi Jun 12, 2020
765a8de
adapt unit test to dplyr now accepting matrices in columns.
cbeleites Jun 12, 2020
7ec4d4e
bump dependeny to dplyr >= 1.0.0 for matrix-in-column functionality.
cbeleites Jun 12, 2020
af34823
bump dependeny to dplyr >= 1.0.0 for matrix-in-column functionality.
cbeleites Jun 12, 2020
a00023b
Merge branch 'feature/12-mutate-setLabels' of github.com:r-hyperspec/…
cbeleites Jun 12, 2020
5ce4928
fix dplyr version requirement
cbeleites Jun 12, 2020
f10fc70
Update mutate.R with dplyr 1.0.0
eoduniyi Jun 13, 2020
60a5525
Update transmute.R with dplyr 1.0.0
eoduniyi Jun 13, 2020
d5d1bd6
Merge branch 'develop' into feature/12-mutate-setLabels
eoduniyi Jun 13, 2020
826e7fc
Update README.md
eoduniyi Jun 15, 2020
c665465
Add "arguments are correctly passed" unit test
eoduniyi Jun 16, 2020
ffa47a5
Merge branch 'develop' into feature/12-mutate-setLabels
eoduniyi Jun 16, 2020
bcd5645
Update DESCRIPTION
eoduniyi Jun 16, 2020
3b93ab6
Update NAMESPACE
eoduniyi Jun 16, 2020
9af9424
Update unit test in mutate.R
eoduniyi Jun 16, 2020
cdfc366
Update unit test in transmute.R
eoduniyi Jun 16, 2020
c6a6f35
Update setLabels.R
eoduniyi Jun 17, 2020
07fe9a3
Update select.R
eoduniyi Jun 17, 2020
dad2cc1
Update rename.R
eoduniyi Jun 17, 2020
b2daaa9
Update transmute.R
eoduniyi Jun 17, 2020
7aabfda
Update hySpc_dplyr.Rmd
eoduniyi Jun 17, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ Collate:
'unittest.R'
'between.R'
'filter.R'
'hy_update_labels.R'
'setLabels.R'
'mutate.R'
'rename.R'
'select.R'
'slice.R'
Expand Down
8 changes: 7 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
# Generated by roxygen2: do not edit by hand

S3method(filter,hyperSpec)
S3method(mutate,hyperSpec)
S3method(rename,hyperSpec)
S3method(select,hyperSpec)
S3method(slice,hyperSpec)
S3method(transmute,hyperSpec)
export(between)
export(hySpc.dplyr.unittest)
export(hy_update_labels)
export(setLabels)
export(setLabels_rename)
export(setLabels_select)
export(setLabels_transmute)
exportMethods(between)
import(magrittr)
importFrom(dplyr,filter)
importFrom(dplyr,mutate)
importFrom(dplyr,rename)
importFrom(dplyr,select)
importFrom(dplyr,slice)
Expand All @@ -20,6 +25,7 @@ importFrom(hyperSpec,.fix_spc_colnames)
importFrom(hyperSpec,chk.hy)
importFrom(hyperSpec,labels)
importFrom(methods,setMethod)
importFrom(rlang,enquos)
importFrom(rlang,quo_get_expr)
importFrom(rlang,quo_name)
importFrom(testthat,ListReporter)
Expand Down
20 changes: 0 additions & 20 deletions R/hy_update_labels.R

This file was deleted.

60 changes: 60 additions & 0 deletions R/mutate.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#' mutate for hyperSpec object
#'
#' `mutate` adds new variables and preserves all the existing variables.
#' Special column `$spc` contains the spectra matrix.
#'
#' @inheritParams dplyr::mutate
#'
#' @return hyperSpec object.
#' @include unittest.R
#' @include setLabels.R
#' @seealso [dplyr::mutate()]
#' @importFrom hyperSpec chk.hy
#' @importFrom hyperSpec labels labels<-
#' @importFrom rlang enquos
#' @importFrom rlang quo_name
#' @importFrom rlang quo_get_expr
#' @importFrom dplyr mutate
#' @export
#'
#' @examples
#' laser %>%
#' mutate (t, filename)
#' head # => results in a hyperSpec object
#' laser %>%
#' mutate (-spc) # => results in a hyperSpec object
#' laser %>%
#' mutate (spc2 = spc*2) %>%
#' mutate (spc2) %>%
#' mutate (spc2*2) # => results in a hyperSpec object
mutate.hyperSpec <- function(.data, ...) {

# Check if user passed in a hyperSpec object
chk.hy(.data)

# Pass mutate arguments to dplyr::mutate
res <- mutate(.data@data, ...)
.data@data <- res
.data
}

.test(mutate.hyperSpec) <- function() {
context("mutate.hyperSpec")

test_that("non hyperSpec objects are rejected", {
df <- data.frame(a = NA, b = NA)
expect_error(mutate.hyperSpec(df))
})

test_that("arguments are correctly passed onto mutate/transmute", {
hy_tmp <- .testdata
hy_tmp@data <- dplyr::mutate(hy_tmp@data, spc*2, spc*3, spc*0)
expect_equivalent(mutate.hyperSpec(.testdata, spc*2, spc*3, spc*0), hy_tmp)
})

test_that("$spc can be used for mutation", {
hy_tmp <- .testdata
hy_tmp@data$spc2 <- hy_tmp@data$spc*2
expect_identical(mutate.hyperSpec(.testdata, spc2 = spc*2), hy_tmp)
})
}
31 changes: 13 additions & 18 deletions R/rename.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#'
#'
#' @inheritParams dplyr::rename
#' @include setLabels.R
#' @seealso [dplyr::rename()]
#' @return hyperSpec object. If `$spc` is renamed, the result is an error.
#' @include unittest.R
Expand All @@ -20,48 +21,42 @@
#'
#' @examples
#'
#' chondro %>%
#' rename (region = clusters) %>%
#' laser %>%
#' rename (filename2 = filename) %>%
#' head # results in a hyperSpec object
rename.hyperSpec <- function(.data, ...){
rename.hyperSpec <- function(.data, ...) {

# Check if user passed in a hyperSpec object
chk.hy(.data)

# Use dplyr::rename() to rename hyperSpec object data slot
res <- rename(.data@data, ...)

# Check if $spc was renamed
if(!'spc' %in% colnames(res)){
if (!'spc' %in% colnames(res)) {

# Throw an error
stop("$spc cannot be renamed")
}else{
# Get new and current column names
labels.to.update <- setdiff(colnames(res), colnames(.data))
labels.to.remove <- setdiff(colnames(.data), colnames(res))
# Update the data slot with newly renamed data frame
.data@data <- res
# Update labels of hyperSpec object
new.labels <- lapply(labels(.data, labels.to.update), as.expression)
labels(.data)[c(labels.to.remove)] <- NULL
labels(.data)[c(labels.to.update)] <- new.labels
.data
} else {

# Set labels
setLabels_rename(.data, res)
}
}

# Begin unit testing (UT)
.test(rename.hyperSpec) <- function(){
context("rename.hyperSpec")

# UT1
test_that("non hyperSpec objects are rejected", {
df <- data.frame(a = NA, b = NA)
expect_error(rename.hyperSpec(df))
})

# UT2
test_that("renaming $spc throws an error", {
expect_error(rename.hyperSpec(chondro, spc_newname = spc))
})

# UT3
test_that("labels attribute when returning hyperSpec object", {
tmp <- rename.hyperSpec(chondro, region = clusters)
tmp <- rename.hyperSpec(tmp, clusters = region)
Expand Down
3 changes: 1 addition & 2 deletions R/select.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#' @seealso [dplyr::select()]
#' @return hyperSpec object or data.frame with the selected columns. If the `$spc` is not included in the selection, the result will be a data.frame.
#' @include unittest.R
#' @include hy_update_labels.R
#' @importFrom dplyr select
#' @importFrom hyperSpec labels labels<-
#' @export
Expand All @@ -34,7 +33,7 @@
select.hyperSpec <- function(.data, ...) {
res <- select(.data@data, ...)
# Update labels
hy_update_labels(.data, res)
setLabels_select(.data, res)
}

.test(select.hyperSpec) <- function() {
Expand Down
102 changes: 102 additions & 0 deletions R/setLabels.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#' Ensure that hyperSpec and non hyperSpec objects have the correct labels

#' @param .data hyperSpec object
#' @param ... resulting data frame
#' @return Object with the correct labels
#' @md
#'
#' @importFrom hyperSpec chk.hy
#' @importFrom hyperSpec labels labels<-
#' @importFrom rlang enquos
#' @importFrom rlang quo_name
#' @importFrom rlang quo_get_expr
#'
#' @examples
#' flu %>%
#' setLabels(.wavelength = "f / THz", c = "c / ml")
#'
#' @export
setLabels <- function(.data, ...) {

# Check if user passed in a hyperSpec object
chk.hy(.data)
args <- enquos(...)
args_names <- names(args)
labels2update <- args_names[args_names %in% names(labels(.data))]
if (length(args) == 0L) {
return(NULL)
}
for (i in seq_along(args)) {
setlabels2 <- quo_name(quo_get_expr(args[[i]]))
if (grepl('expr', setlabels2)) {
setlabels2 <- as.expression(setlabels2)
}
labels(.data, labels2update[i]) <- setlabels2
}
.data
}

#' @rdname setLabels
#' @aliases setLabels
#' @param .data hyperSpec object
#' @param ... list of columns to update on
#' @export
setLabels_transmute <- function(.data, ...) {
setLabels(.data, ...)
}

#' @rdname setLabels
#' @aliases setLabels
#' @param .data hyperSpec object
#' @param res resulting data frame
#' @export
setLabels_select <- function(.data, res) {

# Update labels
labels <- labels(.data)[c(".wavelength", colnames(res))]
if (is.null(res$spc)) {

# use attribute to have correct labels when piping into `as.hyperSpec`
attr(res, "labels") <- labels
res
} else {
.data@data <- res
labels(.data) <- labels
.data
}
}

#' @rdname setLabels
#' @aliases setLabels
#' @param .data hyperSpec object
#' @param res resulting data frame
#' @export
setLabels_rename <- function(.data, res) {
labels.to.update <- setdiff(colnames(res), colnames(.data))
labels.to.remove <- setdiff(colnames(.data), colnames(res))

# Update the data slot with newly renamed data frame
.data@data <- res

# Update labels of hyperSpec object
new.labels <- lapply(labels(.data, labels.to.update), as.expression)
labels(.data)[c(labels.to.remove)] <- NULL
labels(.data)[c(labels.to.update)] <- new.labels
.data
}

.test(setLabels) <- function() {
context("setLabels")

test_that("non hyperSpec objects are rejected", {
df <- data.frame(a = NA, b = NA)
expect_error(setLabels(df))
})

test_that("labels are correctly set" , {
tmp <- laser
labels(tmp, ".wavelength") <- "f / THz"
expect_equivalent(setLabels(laser, .wavelength = "f /THz"), tmp)

})
}
Loading