Skip to content

Commit

Permalink
close #63
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Feb 14, 2020
1 parent ab873a8 commit cb148b4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
2 changes: 0 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ export(table_long)
export(table_short)
export(text_long)
export(text_short)
importFrom(dplyr,group_vars)
importFrom(dplyr,ungroup)
importFrom(insight,find_algorithm)
importFrom(insight,format_ci)
importFrom(insight,format_table)
Expand Down
5 changes: 2 additions & 3 deletions R/report.data.frame.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,10 @@ report.data.frame <- function(model, median = FALSE, centrality = TRUE, dispersi



#' @importFrom dplyr group_vars ungroup
#' @export
report.grouped_df <- function(model, median = FALSE, centrality = TRUE, dispersion = TRUE, range = TRUE, distribution = FALSE, levels_percentage = FALSE, n_entries = 3, missing_percentage = FALSE, ...) {
groups <- dplyr::group_vars(model)
ungrouped_x <- dplyr::ungroup(model)
groups <- .group_vars(model)
ungrouped_x <- as.data.frame(model)
xlist <- split(ungrouped_x, ungrouped_x[groups], sep = " - ")


Expand Down
40 changes: 36 additions & 4 deletions R/utils_dataframe.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#' Safe Dataframe Modification
#' Safe data frame Modification
#'
#' Rename/remove a variable in a dataframe if exists.
#' Rename/remove a variable in a data frame if exists.
#'
#' @param x Dataframe.
#' @param x Data frame.
#' @param pattern Variable to replace.
#' @param replacement New variable name.
#' @param cols Order of columns.
Expand All @@ -25,7 +25,7 @@ rename_if_possible <- function(x, pattern, replacement) {
#' @rdname rename_if_possible
#' @export
remove_if_possible <- function(x, pattern) {
x[!names(x) %in% c(pattern)]
x[!names(x) %in% pattern]
}


Expand All @@ -37,3 +37,35 @@ reorder_if_possible <- function(x, cols) {
remaining_columns <- setdiff(colnames(x), cols)
x[, c(cols, remaining_columns)]
}


# returns the row-indices for grouped data frames
#' @keywords internal
.group_indices <- function(x) {
# dplyr < 0.8.0 returns attribute "indices"
grps <- attr(x, "groups", exact = TRUE)

# dplyr < 0.8.0?
if (is.null(grps)) {
attr(x, "indices", exact = TRUE)
} else {
grps[[".rows"]]
}
}



# returns the variables that were used for grouping data frames (dplyr::group_var())
#' @keywords internal
.group_vars <- function(x) {
# dplyr < 0.8.0 returns attribute "indices"
grps <- attr(x, "groups", exact = TRUE)

# dplyr < 0.8.0?
if (is.null(grps)) {
## TODO fix for dplyr < 0.8
attr(x, "vars", exact = TRUE)
} else {
setdiff(colnames(grps), ".rows")
}
}
6 changes: 3 additions & 3 deletions man/rename_if_possible.Rd

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

0 comments on commit cb148b4

Please sign in to comment.