ggmice
ggmice copied to clipboard
vrb in `plot_trace` with external vector not working
Hello,
When using the plot_trace function, one cannot use a vector even though it has a string. The documentation mentions one can use a vector, however.
Here is my code:
var<-"measure1"
plot_trace(data = imp, vrb = var)
This yields an error:
Error in dplyr::select()
:
! Can't subset columns that don't exist.
✖ Column var
doesn't exist.
When checking the plot_trace function itself, I noticed it uses substitute inside. I think that is the problem, although not entirely sure. It is probably my lack of familiarity with the substitute function.
Any help would be appreciated!
Hi @martinmacias, thanks for pointing this out! I have reproduced the error, but didn't find a solution so far. So I provided some alternatives for now, see below.
@pepijnvink could you please take a look at the issue?
library(mice)
library(ggmice)
imp <- mice(nhanes, print = FALSE)
# reproduce the error
x <- "bmi"
plot_trace(imp, x)
#> Error in `dplyr::select()` at ggmice package/R/plot_trace.R:29:4:
#> ! Can't subset columns that don't exist.
#> ✖ Column `x` doesn't exist.
#> Backtrace:
#> ▆
#> 1. ├─ggmice::plot_trace(imp, x)
#> 2. │ ├─dplyr::select(...) at ggmice package/R/plot_trace.R:29:4
#> 3. │ └─dplyr:::select.data.frame(...)
#> 4. │ └─tidyselect::eval_select(expr(c(...)), data = .data, error_call = error_call)
#> 5. │ └─tidyselect:::eval_select_impl(...)
#> 6. │ ├─tidyselect:::with_subscript_errors(...)
#> 7. │ │ └─rlang::try_fetch(...)
#> 8. │ │ └─base::withCallingHandlers(...)
#> 9. │ └─tidyselect:::vars_select_eval(...)
#> 10. │ └─tidyselect:::walk_data_tree(expr, data_mask, context_mask)
#> 11. │ └─tidyselect:::eval_c(expr, data_mask, context_mask)
#> 12. │ └─tidyselect:::reduce_sels(node, data_mask, context_mask, init = init)
#> 13. │ └─tidyselect:::walk_data_tree(new, data_mask, context_mask)
#> 14. │ └─tidyselect:::as_indices_sel_impl(...)
#> 15. │ └─tidyselect:::as_indices_impl(...)
#> 16. │ └─tidyselect:::chr_as_locations(x, vars, call = call, arg = arg)
#> 17. │ └─vctrs::vec_as_location(...)
#> 18. └─vctrs (local) `<fn>`()
#> 19. └─vctrs:::stop_subscript_oob(...)
#> 20. └─vctrs:::stop_subscript(...)
#> 21. └─rlang::abort(...)
# some failed potential solutions
plot_trace(imp, eval(rlang::expr(x)))
#> Error in `dplyr::select()` at ggmice package/R/plot_trace.R:29:4:
#> ! Problem while evaluating `eval(rlang::expr(x))`.
#> Caused by error:
#> ! could not find function "eval"
#> Backtrace:
#> ▆
#> 1. ├─ggmice::plot_trace(imp, eval(rlang::expr(x)))
#> 2. │ ├─dplyr::select(...) at ggmice package/R/plot_trace.R:29:4
#> 3. │ └─dplyr:::select.data.frame(...)
#> 4. │ └─tidyselect::eval_select(expr(c(...)), data = .data, error_call = error_call)
#> 5. │ └─tidyselect:::eval_select_impl(...)
#> 6. │ ├─tidyselect:::with_subscript_errors(...)
#> 7. │ │ └─rlang::try_fetch(...)
#> 8. │ │ └─base::withCallingHandlers(...)
#> 9. │ └─tidyselect:::vars_select_eval(...)
#> 10. │ └─tidyselect:::walk_data_tree(expr, data_mask, context_mask)
#> 11. │ └─tidyselect:::eval_c(expr, data_mask, context_mask)
#> 12. │ └─tidyselect:::reduce_sels(node, data_mask, context_mask, init = init)
#> 13. │ └─tidyselect:::walk_data_tree(new, data_mask, context_mask)
#> 14. │ └─tidyselect:::eval_context(expr, context_mask, call = error_call)
#> 15. │ ├─tidyselect:::with_chained_errors(...)
#> 16. │ │ └─rlang::try_fetch(...)
#> 17. │ │ ├─base::tryCatch(...)
#> 18. │ │ │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#> 19. │ │ │ └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#> 20. │ │ │ └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#> 21. │ │ └─base::withCallingHandlers(...)
#> 22. │ └─rlang::eval_tidy(as_quosure(expr, env), context_mask)
#> 23. └─base::.handleSimpleError(...)
#> 24. └─rlang (local) h(simpleError(msg, call))
#> 25. └─handlers[[1L]](cnd)
#> 26. └─rlang::abort(msg, call = call, parent = cnd)
plot_trace(imp, !!x)
#> Error: object 'x' not found
# some alternatives that do work
plot_trace(imp, bmi)
plot_trace(imp, "bmi")
plot_trace(imp, c("bmi"))
Created on 2024-02-20 with reprex v2.0.2