ms1 <- remove_row_annotation(student_results, class, teacher)
ms <- join_row_info(ms1, student_results)
ms <- join_row_info(ms1, student_results, by = c(".rowname", "previous_year_score"))
# This will throw an error
ms2 <- remove_row_annotation(filter_row(student_results, class %in% c("classA", "classC")),
class, teacher, previous_year_score)
ms <- tryCatch(join_row_info(ms2, student_results, type = "full"),
error = function(e) e)
is(ms, "error") # TRUE
ms$message
# Now it works.
ms <- join_row_info(ms2, student_results, type = "full", adjust = TRUE)
dim(ms2)
dim(ms)
matrix_elm(ms, 1)
# Similarly, this will fail because tag names are no longer unique
meta <- tibble::tibble(sample = c("student 2", "student 2"),
msr = c("height", "weight"),
value = c(145, 32))
ms <- tryCatch(join_row_info(student_results, meta, by = c(".rowname"="sample")),
error = function(e) e)
is(ms, "error") # TRUE
ms$message
# This works, by forcing the tag names to be unique. Notice that we suppress
# the warning for now. We'll come back to it.
suppressWarnings(
join_row_info(student_results, meta, by = c(".rowname"="sample"),
adjust = TRUE, names_glue = TRUE)
)
# Here's the warning: we're being told there was a change in tag names
(purrr::quietly(join_row_info)(student_results, meta,
by = c(".rowname"="sample"), adjust = TRUE,
names_glue = TRUE))$warnings
# You can have better control on how the tag change occurs, for instance by
# appending the msr value to the name
suppressWarnings(
join_row_info(student_results, meta, by = c(".rowname"="sample"),
adjust = TRUE, names_glue = "{.tag}_{msr}")
)
# In this specific example, the {.tag} was superfluous, since the default is
# to append after the tag name
suppressWarnings(
join_row_info(student_results, meta, by = c(".rowname"="sample"),
adjust = TRUE, names_glue = "{msr}")
)
# But the keyword is useful if you want to shuffle order
suppressWarnings(
join_row_info(student_results, meta, by = c(".rowname"="sample"),
adjust = TRUE, names_glue = "{msr}.{.tag}")
)
# You are warned when there is a change in traits
meta <- tibble::tibble(sample = c("student 2", "student 2"),
class = c("classA", "classA"),
msr = c("height", "weight"),
value = c(145, 32))
(purrr::quietly(join_row_info)(student_results, meta,
by = c(".rowname"="sample"), adjust = TRUE,
names_glue = TRUE))$warnings[2]
# Groups are automatically adjusted
sr_gr <- row_group_by(student_results, class)
gr_orig <- row_group_meta(row_group_by(student_results, class)) |> tidyr::unnest(.rows)
suppressWarnings(
new_gr <- join_row_info(sr_gr, meta, by = c(".rowname" = "sample", "class"),
adjust = TRUE, names_glue = TRUE) |>
row_group_meta() |> tidyr::unnest(.rows)
)
list(gr_orig, new_gr)
# In the example above, the join operation changed the class of 'class',
# which in turn changed the grouping meta info. You are warned of both.
(purrr::quietly(join_row_info)(sr_gr, meta,
by = c(".rowname"="sample", "class"),
adjust = TRUE, names_glue = TRUE))$warnings
# A change in trait name that was used for grouping will result in losing the
# grouping. You are warning of the change in grouping structure.
(purrr::quietly(join_row_info)(sr_gr, meta,
by = c(".rowname"="sample"),
adjust = TRUE, names_glue = TRUE))$warnings
Run the code above in your browser using DataLab