# WARNING - Generated by {fusen} from dev/flat_dupl_show.Rmd: do not edit by hand

#' Show observations of all duplicated values of a variable or a combination of variables
#' @param data_frame Data.frame. Input data frame. Must be in the Global Environment and has a data.frame class
#' @param vars Character. Vector of variable or combination of variables
#' from which duplicates are checked
#' @return Data.frame. The part of inputted data frame with all observations of duplicated values of
#' indicated variable or combination of variables
#' @importFrom magrittr %>%
#' @export
#'
#' @examples
#' # A fictional data with duplicated values:
#' df <- data.frame("person_id" = c(1, 1, 2, 3,
#'                                  2, 4, 5, 5 ,1),
#'                  "person_age" = c(25, 25, 21, 32,
#'                                   21, 48, 50, 50, 52),
#'                  "survey_month" = c("jan", "feb", "mar", "apr",
#'                                     "apr", "may", "jun", "jul", "jan"),
#'                  "survey_answer" = c("no", "yes", "no", "yes",
#'                                      "yes", "yes", "no", "yes", NA))
#'
#' # Shuffling observations and columns to make duplicates difficult to see:
#' set.seed(1)
#' df <- df[sample(1:nrow(df)),
#'          sample(1:ncol(df))]
#' df
#'
#' # See all of the rows where person_id has more than an unique possible value: 
#' dupl_show(data = df, var = "person_id")
#'
#' # See all of the rows where the combination of person_id and survey_month variables has 
#' # more than an unique possible value : 
#' dupl_show(data = df, var = c("person_id", "survey_month"))
dupl_show <- function (data_frame, vars) {

  requireNamespace("magrittr")
  # Insert element-wise combination of vars input components within the data:
  # idvarsdupl
  data <- dplyr::mutate(data_frame,
                        idvarsdupl =
                          do.call(what = paste,
                                  purrr::map(vars, function (x)
                                    data_frame[[x]])))

  # Filter rows with values of idvarsdupl that appears
  # at least for the second time:
  data %>% dplyr::filter(duplicated(idvarsdupl)) %>%

    # extract idvarsdupl variable from the filtered data:
    dplyr::pull(idvarsdupl) %>%

    # extract all corresponding observations from data input:
    (function (p) dplyr::filter(data, idvarsdupl %in% p)) %>%

    # arrange values to visualize the duplicated cases:
    dplyr::arrange(!!dplyr::sym("idvarsdupl")) %>%

    # put vars input to first variable(s) location(s):
    dplyr::relocate(dplyr::all_of(vars)) %>%

    # remove idvarsdupl:
    dplyr::select(- !!dplyr::sym("idvarsdupl"))
}
