When writing a package, one sometimes would like to both avoid running examples through #' \dontrun{) and avoid showing some operations with #' \dontshow{}. Naturally, it is also good practice to style the code using styler:::style_active_file() or styler::tidyverse_style().

When used separately, both dontrun and dontshow work well with styler. However, when used jointly, it seems to create a bug. Reproducible example below:

#' @title Test
#' @examples
#' \dontrun{
#' \dontshow{
#' .old_wd <- setwd("man")
#' }
#' test()
#' \dontshow{
#' setwd(.old_wd)
#' }
#' }
#' @export

test <- function() {
  "test"
}
> styler:::style_active_file()
Using style transformers `styler::tidyverse_style()`
Error in `handlers[[1L]]()`:
! ℹ In index: 1.
Run `rlang::last_trace()` to see where the error occurred.

> rlang::last_trace()
<error/rlang_error>
Error in `handlers[[1L]]()`:
! ℹ In index: 1.
---
Backtrace:
      ▆
   1. ├─styler:::style_active_file()
   2. │ └─styler:::try_transform_as_r_file(context, transformer)
   3. │   ├─rlang::try_fetch(...)
   4. │   │ ├─base::tryCatch(...)
   5. │   │ │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
   6. │   │ │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
   7. │   │ │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
   8. │   │ └─base::withCallingHandlers(...)
   9. │   └─styler (local) transformer(context$contents)
  10. │     └─styler:::parse_transform_serialize_roxygen(...)
  11. │       ├─... %>% flatten_chr()
  12. │       └─purrr::map_at(...)
  13. │         └─purrr::map(.x[where], .f, ..., .progress = .progress)
  14. │           └─purrr:::map_("list", .x, .f, ..., .progress = .progress)
  15. │             ├─purrr:::with_indexed_errors(...)
  16. │             │ └─base::withCallingHandlers(...)
  17. │             ├─purrr:::call_with_cleanup(...)
  18. │             └─styler (local) .f(.x[[i]], ...)
  19. │               ├─... %>% flatten_chr()
  20. │               └─purrr::map(...)
  21. │                 └─purrr:::map_("list", .x, .f, ..., .progress = .progress)
  22. │                   ├─purrr:::with_indexed_errors(...)
  23. │                   │ └─base::withCallingHandlers(...)
  24. │                   ├─purrr:::call_with_cleanup(...)
  25. │                   └─styler (local) .f(.x[[i]], ...)
  26. │                     ├─... %>% flatten_chr()
  27. │                     └─purrr::map(...)
  28. │                       └─purrr:::map_("list", .x, .f, ..., .progress = .progress)
  29. │                         ├─purrr:::with_indexed_errors(...)
  30. │                         │ └─base::withCallingHandlers(...)
  31. │                         ├─purrr:::call_with_cleanup(...)
  32. │                         └─styler (local) .f(.x[[i]], ...)
  33. │                           ├─... %>% flatten_chr()
  34. │                           └─purrr::map2(...)
  35. │                             └─purrr:::map2_("list", .x, .y, .f, ..., .progress = .progress)
  36. │                               ├─purrr:::with_indexed_errors(...)
  37. │                               │ └─base::withCallingHandlers(...)
  38. │                               ├─purrr:::call_with_cleanup(...)
  39. │                               └─styler (local) .f(.x[[i]], .y[[i]], ...)
  40. │                                 └─code_snippet %>% ...
  41. ├─purrr::flatten_chr(.)
  42. ├─purrr::flatten_chr(.)
  43. ├─purrr::flatten_chr(.)
  44. ├─purrr::flatten_chr(.)
  45. ├─styler:::parse_transform_serialize_r(...)
  46. │ └─styler::compute_parse_data_nested(text, transformers, more_specs)
  47. │   └─styler:::text_to_flat_pd(text, transformers, more_specs = more_specs)
  48. │     ├─... %>% shallowify()
  49. │     └─styler:::tokenize(text)
  50. │       ├─... %>% enhance_mapping_special()
  51. │       └─styler:::get_parse_data(text, include_text = TRUE)
  52. │         └─styler:::parse_safely(text, keep.source = TRUE)
  53. │           └─rlang::abort(tried_parsing$message)
  54. │             └─rlang:::signal_abort(cnd, .file)
  55. │               └─base::signalCondition(cnd)
  56. ├─styler:::shallowify(.)
  57. │ └─base::order(pd$line1, pd$col1, -pd$line2, -pd$col2, as.integer(pd$terminal))
  58. ├─styler:::add_attributes_caching(., transformers, more_specs = more_specs)
  59. │ └─base::nrow(pd_flat)
  60. ├─styler:::add_stylerignore(.)
  61. │ └─base::trimws(pd_flat$text)
  62. │   ├─base (local) mysub(...)
  63. │   │ └─base::sub(re, "", x, perl = TRUE)
  64. │   │   └─base::is.factor(x)
  65. │   └─base (local) mysub(paste0("^", whitespace, "+"), x)
  66. │     └─base::sub(re, "", x, perl = TRUE)
  67. │       └─base::is.factor(x)
  68. ├─styler:::add_terminal_token_after(.)
  69. │ └─pd_flat %>% filter(terminal) %>% arrange_pos_id()
  70. ├─styler:::arrange_pos_id(.)
  71. ├─styler:::filter(., terminal)
  72. │ └─base::subset(.data, ...)
  73. ├─styler:::add_terminal_token_before(.)
  74. │ └─pd_flat %>% filter(terminal) %>% arrange_pos_id()
  75. ├─styler:::arrange_pos_id(.)
  76. ├─styler:::filter(., terminal)
  77. │ └─base::subset(.data, ...)
  78. ├─styler:::enhance_mapping_special(.)
  79. ├─styler:::ensure_correct_txt(., text)
  80. │ └─styler:::is_insufficiently_parsed_string(pd)
  81. │   └─base::grepl("^\\[", pd$text)
  82. │     └─base::is.factor(x)
  83. ├─purrr (local) `<fn>`(`<rlng_rrr>`)
  84. │ └─cli::cli_abort(...)
  85. │   └─rlang::abort(...)
  86. │     └─rlang:::signal_abort(cnd, .file)
  87. │       └─base::signalCondition(cnd)
  88. ├─purrr (local) `<fn>`(`<prrr_rr_>`)
  89. │ └─cli::cli_abort(...)
  90. │   └─rlang::abort(...)
  91. │     └─rlang:::signal_abort(cnd, .file)
  92. │       └─base::signalCondition(cnd)
  93. ├─purrr (local) `<fn>`(`<prrr_rr_>`)
  94. │ └─cli::cli_abort(...)
  95. │   └─rlang::abort(...)
  96. │     └─rlang:::signal_abort(cnd, .file)
  97. │       └─base::signalCondition(cnd)
  98. ├─purrr (local) `<fn>`(`<prrr_rr_>`)
  99. │ └─cli::cli_abort(...)
 100. │   └─rlang::abort(...)
 101. │     └─rlang:::signal_abort(cnd, .file)
 102. │       └─base::signalCondition(cnd)
 103. └─rlang (local) `<fn>`(`<prrr_rr_>`)
 104.   └─handlers[[1L]](cnd)
Run rlang::last_trace(drop = FALSE) to see 1 hidden frame.

I'm sure there is an easy, obvious solution to this, but I cannot find it online. Any suggestion to handle this properly?

0

There are 0 best solutions below