Skip to content

Commit

Permalink
Issues/98 (#99)
Browse files Browse the repository at this point in the history
* edits based on lintr checks

* changes based on lintr

* suppressMessages when loading magrittr

* Don't print logs
  • Loading branch information
shntnu authored Sep 13, 2017
1 parent b9a6178 commit 649ba28
Show file tree
Hide file tree
Showing 28 changed files with 119 additions and 105 deletions.
2 changes: 1 addition & 1 deletion R/correlation_threshold.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#'
#' @examples
#'
#' library(magrittr)
#' suppressMessages(suppressWarnings(library(magrittr)))
#' sample <- tibble::data_frame(
#' x = rnorm(30),
#' y = rnorm(30)/1000
Expand Down
1 change: 0 additions & 1 deletion R/count_na_rows.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,4 @@ count_na_rows <- function(population, variables) {
dplyr::summarize_at(variables, dplyr::funs(sum)) %>%
dplyr::collect() %>%
data.frame()

}
8 changes: 4 additions & 4 deletions R/drop_na_columns.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@
#' @export
drop_na_columns <- function(population, variables, cutoff = 0.05) {
cutoff <- rlang::enquo(cutoff)

nrows <-
population %>%
dplyr::tally() %>%
dplyr::collect() %>%
magrittr::extract2("n")

count <- rlang::sym("count")

feature <- rlang::sym("feature")

percent <- rlang::sym("percent")

population %>%
Expand All @@ -44,6 +44,6 @@ drop_na_columns <- function(population, variables, cutoff = 0.05) {
dplyr::collect() %>%
tidyr::gather(!!feature, !!count, !!!variables) %>%
dplyr::mutate(!!percent := (!!count) / nrows) %>%
dplyr::filter((!!percent) > (!!cutoff)) %>%
dplyr::filter( (!!percent) > (!!cutoff)) %>%
magrittr::extract2("feature")
}
14 changes: 7 additions & 7 deletions R/drop_na_rows.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
drop_na_rows <- function(population, variables) {

key <- rlang::sym("key")

value <- rlang::sym("value")

rowname_temp <- rlang::sym("rowname_temp")

if (is.data.frame(population)) {
Expand All @@ -37,13 +37,13 @@ drop_na_rows <- function(population, variables) {
dplyr::filter(!is.na(!!value)) %>%
tidyr::spread(!!key, !!value) %>%
dplyr::select(-!!rowname_temp)
} else {

} else {

# Coalesce() must have at least 2 arguments.
if(length(variables) == 1)
if (length(variables) == 1)
variables <- c(variables, variables)

population %>%
dplyr::filter_(.dots =
sprintf("!is.null(coalesce(%s))",
Expand Down
4 changes: 2 additions & 2 deletions R/generalized_log.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ generalized_log <- function(population, variables, offset = 1) {
x <- rlang::sym(variable)

population %<>%
dplyr::mutate(!!x := log( ((!!x) + ((!!x) ^ 2 + (!!offset) ^ 2) ^ 0.5 ) / 2))
dplyr::mutate(!!x :=
log( ( (!!x) + ( (!!x) ^ 2 + (!!offset) ^ 2) ^ 0.5 ) / 2))
}

population
}

7 changes: 4 additions & 3 deletions R/normalize.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#' @importFrom stats cor mad median sd setNames
#'
#' @examples
#' library(magrittr)
#' suppressMessages(suppressWarnings(library(magrittr)))
#' population <- tibble::data_frame(
#' Metadata_group = c("control", "control", "control", "control",
#' "experiment", "experiment", "experiment", "experiment"),
Expand All @@ -30,7 +30,8 @@
#' cytominer::normalize(population, variables, strata, sample, operation = "standardize")
#'
#' @export
normalize <- function(population, variables, strata, sample, operation = "standardize", ...) {
normalize <- function(population, variables, strata, sample,
operation = "standardize", ...) {
scale <- function(data, location, dispersion, variables) {
if (is.data.frame(data)) {
futile.logger::flog.debug(paste0("\t\tUsing base::scale (data is ",
Expand Down Expand Up @@ -59,7 +60,7 @@ normalize <- function(population, variables, strata, sample, operation = "standa
s <- dispersion[[variable]]

data %<>%
dplyr::mutate(!!x := ((!!x) - m) / s )
dplyr::mutate(!!x := ( (!!x) - m) / s )

}

Expand Down
6 changes: 3 additions & 3 deletions R/replicate_correlation.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ replicate_correlation <-
strata <- c(strata, replicate_by)
}

foreach::foreach(variable = variables, .combine = rbind) %dopar%
{
foreach::foreach(variable = variables, .combine = rbind) %dopar% {

sample %>%
split(.[split_by]) %>%
Expand All @@ -94,7 +93,8 @@ replicate_correlation <-
dplyr::arrange_(.dots = strata) %>%
dplyr::select_(.dots = c(strata, variable, replicate_by)) %>%
tidyr::spread_(replicate_by, variable) %>%
dplyr::select_(~-dplyr::one_of(setdiff(strata, replicate_by))) %>%
dplyr::select_(~-dplyr::one_of(setdiff(strata,
replicate_by))) %>%
stats::cor()
median(correlation_matrix[upper.tri(correlation_matrix)])
}) %>%
Expand Down
3 changes: 2 additions & 1 deletion R/transform.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
#' @importFrom magrittr %>%
#' @importFrom magrittr %<>%
#' @export
transform <- function(population, variables, operation = "generalized_log", ...) {
transform <- function(population, variables,
operation = "generalized_log", ...) {
if (operation == "generalized_log") {
generalized_log(population, variables, ...)
} else {
Expand Down
7 changes: 4 additions & 3 deletions R/variable_importance.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,18 @@
#' @importFrom magrittr %>%
#' @importFrom magrittr %<>%
#' @export
variable_importance <- function(sample, variables, operation = "replicate_correlation", ...) {
variable_importance <- function(sample, variables,
operation = "replicate_correlation", ...) {
if (operation == "replicate_correlation") {
importance <- replicate_correlation(sample, variables, ...)

} else {
error <- paste0("undefined operation `", operation, "'")

futile.logger::flog.error(msg = error)

stop(error)

}

importance
Expand Down
4 changes: 3 additions & 1 deletion R/variable_select.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#' # In this example, we use `correlation_threshold` as the operation for
#' # variable selection.
#'
#' library(magrittr)
#' suppressMessages(suppressWarnings(library(magrittr)))
#' population <- tibble::data_frame(
#' x = rnorm(100),
#' y = rnorm(100)/1000
Expand All @@ -35,6 +35,8 @@
#'
#' head(population)
#'
#' futile.logger::flog.threshold(futile.logger::ERROR)
#'
#' variable_select(population, variables, sample, operation) %>% head()
#'
#' @importFrom magrittr %>%
Expand Down
3 changes: 2 additions & 1 deletion R/variance_threshold.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ variance_threshold <- function(variables, sample) {

lunique <- apply(x, 2, function(data) length(unique(data[!is.na(data)])))

which((ratio > 19 & (100 * lunique / apply(x, 2, length)) <= 10) | (lunique == 1) | apply(x, 2, function(data) all(is.na(data))))
which( (ratio > 19 & (100 * lunique / apply(x, 2, length)) <= 10) |
(lunique == 1) | apply(x, 2, function(data) all(is.na(data))))
}

excluded_indexes <-
Expand Down
2 changes: 1 addition & 1 deletion docs/articles/cytominer-pipeline.html

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

2 changes: 1 addition & 1 deletion docs/reference/correlation_threshold.html

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

2 changes: 1 addition & 1 deletion docs/reference/normalize.html

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

6 changes: 3 additions & 3 deletions docs/reference/variable_select.html

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

2 changes: 1 addition & 1 deletion man/correlation_threshold.Rd

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

2 changes: 1 addition & 1 deletion man/normalize.Rd

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

4 changes: 3 additions & 1 deletion man/variable_select.Rd

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

7 changes: 4 additions & 3 deletions tests/testthat/test-aggregate.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ test_that("`aggregate` aggregates data", {
)

db <- dplyr::src_sqlite(":memory:", create = T)

data <- dplyr::copy_to(db, data)

expect_equal(
Expand All @@ -31,7 +31,7 @@ test_that("`aggregate` aggregates data", {
dplyr::collect(),
data %>%
dplyr::group_by(g) %>%
dplyr::summarise_at(.funs =dplyr::funs(median), .vars = c("x", "y"))
dplyr::summarise_at(.funs = dplyr::funs(median), .vars = c("x", "y"))
)

expect_equal(
Expand All @@ -42,6 +42,7 @@ test_that("`aggregate` aggregates data", {
dplyr::collect(),
data %>%
dplyr::group_by(g) %>%
dplyr::summarise_at(.funs =c(dplyr::funs(mean), dplyr::funs(sd)), .vars = c("x", "y"))
dplyr::summarise_at(.funs = c(dplyr::funs(mean), dplyr::funs(sd)),
.vars = c("x", "y"))
)
})
5 changes: 3 additions & 2 deletions tests/testthat/test-correlation_threshold.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
context("correlation_threshold")

test_that("`correlation_threshold` selects variables that are not highly correlated", {
test_that(
"`correlation_threshold` selects variables that are not highly correlated", {

set.seed(123)
data <- data.frame(x = rnorm(30))
data$y <- rnorm(30) / 1000
data$z <- data$x + rnorm(30) / 1000

db <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")

data <- dplyr::copy_to(db, data)

expect_equal(
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-count_na_rows.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ test_that("`count_na_rows` returns the frequency of NAs per variable", {
data[c(2, 3, 5), "y"] <- NA

db <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")

data <- dplyr::copy_to(db, data)

expect_equal(
count_na_rows(population = data,
variables = c("x", "y", "z")),
Expand Down
Loading

0 comments on commit 649ba28

Please sign in to comment.