data-raw/error-test.R

# g <- function(tbl, i) {
#   # print(i)
#   dplyr::enquo(i)
#   # i <- dplyr::quo_name(dplyr::enquo(i))
#   # print(i)
#   # tbl %>% select(!!id_var)
#   0
# }

# map2(.x = list(asm), .y = list('id'), .f = g)
# f <- function(i) { i }
# g <- function(i) {
#   print(quo(i))
#   i <- i # frig, that's an annoying little thin.g
#   dplyr::enquo(i)
#   }

# ok, well, try not to use enquo for anything. try to use unscoped things for everything?
# so, I learned you can't tell the difference between something, because enquo evaluates it,
# and you can't use it if it's already been evaluated before that?
# oh, right, also the list/lapply/map thing. evaluate it but

# f("id")
# map(list(id), .f = f)
#
# g("id")
# map(list('id'), .f = g)
# g(list('id'))

# xxx
# # map('id', .f = g)
#
# xxx
f <- function(tbl, i) {
  # i <- enquo(i) #%>% quo_name()
  # print(str(i))
  # print('end')
  print(rlang::get_env(i))
  print(i %>% str())
  print(typeof(i)) # check if character
  print(i %>% str())
  print(rlang::get_env(i))
  # print(class(i))
  print(enquo(i))

  # group_by(.data[[!!homeworld]])
  # x <- typeof(i)
  # print(typeof(i))
  # y <- enquo(i)
  # z <- enquo(i)
  #   j <- ifelse(typeof(i) == "character", 0, dplyr::enquo(i))
  #   print(j)
  #   i <- ifelse(typeof(i) == "character", rlang::sym(i), dplyr::enquo(i))
  # print(i)
  # line to check if it's a string or not? if string, use rlang::sym, if not, enquo?

  # i <- quo_name(enquo(i))

  # quo(tbl %>% filter(!!i == 1)) %>% print()
  # quo(tbl %>% select(!!i)) %>% print()
  # quo(tbl %>% group_by(!!i)) %>% print()
  # tbl %>% select(!!i)
  # neither work. create new var = "id" in all cases.
  # tbl %>% group_by(!!i)
  # tbl %>%
  #   group_by(.data[[!!i]]) %>%
}
# asm %>% f("id")
# f(asm, id)

# xxx
# asm %>% f(id)
# so just use enquo and never strings?
# but then that messes with the way I've set up the other things.



# library(dplyr)
# library(purrr)
#
# data <- tibble(id_a = c(1,1,2,2,3),
#                id_b = 991,
#                id_c = c(45,45,45,1,80),
#                units_sold = c(21,20,24,4,5))
# id_types <- c("id_a", "id_b", "id_c")
#
# f <- function(id_type) enquo(id_type)
#
# g <- function(df, id_type) {
#   # print(id_type)
#   # id_type <- id_type
#   print(enquo(id_type))
#   # quo_id_type <- dplyr::quo_name(dplyr::enquo(id_type))
#   # print(id_type)
#   # filter(df, .data[[!!quo_id_type]] == 1)
# }
# filter_data <- function(df, id_type) {
#   id_type <- id_type
#   quo_id_type <- enquo(id_type)
#   print(quo(filter(df, !!quo_id_type == 1)))
#   print(quo(filter(df, (!!quo_id_type) == 1)))
#
#   filter(df, (!!quo_id_type) == 1)
# }
#
#
# filter_data <- function(df, id_type) {
#   id_type <- id_type
#   quo_id_type <- enquo(id_type)
#   print(quo(filter(df, !!quo_id_type == 1)))
#
#   filter(df, !!quo_id_type == 1)
# }
#
# filter_data <- function(df, id_type) {
#   filter(df, .data[[id_type]] == 1)
# }
#
# filter_data <- function(df, id_type) {
#   id_type <- id_type
#   quo_id_type <- enquo(id_type)
#   print(quo(filter(df, !!quo_id_type == 1)))
#
#   filter(df, !!quo_id_type == 1)
# }
#
# # 1. the argument that is enquo()'d is id_types[[1]], not "id_a", really.
# # You can skip that to see what happens by 'id_type <- id_type', just to get to the next step.
# # 2. !!quo_id_type == 1 is like !!(quo_id_type == 1) which is !!("id_a" == 1) which is FALSE,
# # which you can see by
# # print(quo(filter(df, !!quo_id_type == 1)))
# # 3.
#
# # had not considered that. passing a list changes input.
# filter_at(data, vars(paste0(id_types, collapse = "|")), all_vars(. == 1))
# filter_at(data, vars("id_a"), any_vars(. == 1))
# filter_at(data, vars("id_a", "id_b"), all_vars(. == 1))
# filter_at(data, vars(id_types), any_vars(. == 1))
# filter_at(data, vars(id_a, id_b), all_vars(. == 1))
# filter_at(data, vars(one_of(id_types)), all_vars(. == 1))
# filter_at(data, vars(one_of(c("id_a", "id_c"))), any_vars(. == 1))
#
# # f(id_types[[1]])
# # f("id_a")
# # xxx
# bind_rows(pmap(list(list(data),
#                     id_types),
#                filter_data))
#

# tbl <- tibble(point = c('SE', 'NW'))
#
# f <- function(dat, point) {
#   if (!(point %in% c("NW", "SE", "SW")))
#     stop("point must be one of 'SW', 'SE', 'NW'")
#
#   filter(dat, point == !!point)
# }
#
# f(tbl, 'SE')
# f(tbl, 'XX')
#

# t_groupedfreqprop <- function(df, groupvar, grouplab) {
#   # print(grouplab)
#   groupvar <- enquo(groupvar)
#   grouplab <- enquo(grouplab)
#   # print(grouplab)
#   t <- df %>% rename( !!grouplab := !!rlang::sym(groupvar))
# }
# t_groupedfreqprop(df = mtcars, groupvar = gear, grouplab = "Vehicle Gears")
tweed1e/idiosyncratics documentation built on May 29, 2019, 10:51 a.m.