🔍 An RStudio addin slash regex utility belt
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 satır
5.6KB

  1. #' RegExplain gadget
  2. #'
  3. #' The function behind the RegExplain Selection and RegExplain File
  4. #' addins. Opens the RegExplain gadget interface in an RStudio viewer
  5. #' pane.
  6. #'
  7. #' @examples
  8. #' \dontrun{
  9. #' regexplain_gadget(text = month.name, pattern = "(Ma|Ju)|(er)")
  10. #' regexplain_web(text = month.name, pattern = "(Ma|Ju)|(er)")
  11. #' regexplain_file()
  12. #' }
  13. #'
  14. #' @import miniUI
  15. #' @import shiny
  16. #' @param text Text to explore in gadget (editable using interface)
  17. #' @param pattern Regular Expression to edit or visualize using RegExplain
  18. #' @param start_page Open gadget to this tab, one of `"Text"`, `"RegEx"`,
  19. #' `"Output"`, or `"Help"`
  20. #' @export
  21. regexplain_gadget <- function(
  22. text = NULL,
  23. pattern = NULL,
  24. start_page = if (is.null(text)) "Text" else "RegEx"
  25. ) {
  26. stopifnot(requireNamespace("miniUI"), requireNamespace("shiny"))
  27. viewer <- shiny::paneViewer(minHeight = 800)
  28. runGadget(
  29. regexplain_gadget_ui(text, pattern, start_page),
  30. regexplain_gadget_server(check_version()),
  31. viewer = viewer)
  32. }
  33. #' @describeIn regexplain_gadget Launches the RegExplain gadget in a browser or an
  34. #' RStduio viewer pane.
  35. #' @inheritDotParams shiny::shinyApp
  36. #' @export
  37. regexplain_web <- function(text = NULL, pattern = NULL, start_page = "Text", ...) {
  38. stopifnot(requireNamespace("miniUI"), requireNamespace("shiny"))
  39. shinyApp(
  40. regexplain_gadget_ui(text, pattern, start_page),
  41. regexplain_gadget_server(check_version()), ...)
  42. }
  43. # ---- Gadget Helper Functions and Variables ----
  44. sanitize_text_input <- function(x) {
  45. if (is.null(x) || !nchar(x)) return(x)
  46. rx_unicode <- "\\\\u[0-9a-f]{4,8}"
  47. rx_hex <- "\\\\x[0-9a-f]{2}|\\\\x\\{[0-9a-f]{1,6}\\}"
  48. rx_octal <- "\\\\[0][0-7]{1,3}"
  49. rx_escape <- paste(rx_unicode, rx_hex, rx_octal, sep = "|")
  50. if (grepl(rx_escape, x, ignore.case = TRUE)) {
  51. try({
  52. y <- stringi::stri_unescape_unicode(x)
  53. }, silent = TRUE)
  54. if (!is.na(y)) x <- y
  55. }
  56. # x <- gsub("\u201C|\u201D", '"', x)
  57. # x <- gsub("\u2018|\u2019", "'", x)
  58. x
  59. }
  60. toHTML <- function(...) {
  61. x <- paste(..., collapse = "")
  62. x <- gsub("\n", "\\\\n", x)
  63. x <- gsub("\t", "\\\\t", x)
  64. x <- gsub("\r", "\\\\r", x)
  65. HTML(x)
  66. }
  67. regexFn_choices <- list(
  68. "Choose a function" = "",
  69. base = c(
  70. "grep",
  71. "grepl",
  72. "sub", #<<
  73. "gsub", #<<
  74. "regexpr",
  75. "gregexpr",
  76. "regexec"
  77. ),
  78. stringr = c(
  79. "str_detect",
  80. "str_locate",
  81. "str_locate_all",
  82. "str_extract",
  83. "str_extract_all",
  84. "str_match",
  85. "str_match_all",
  86. "str_replace", #<<
  87. "str_replace_all", #<<
  88. "str_split"
  89. ),
  90. "rematch2" = c(
  91. "re_match",
  92. "re_match_all",
  93. "re_exec",
  94. "re_exec_all"
  95. )
  96. )
  97. regexFn_substitute <- c(
  98. paste0(c("", "g"), "sub"),
  99. paste0("str_replace", c("", "_all"))
  100. )
  101. get_pkg_namespace <- function(fn) {
  102. x <- names(purrr::keep(regexFn_choices, ~ (fn %in% .)))
  103. if (length(x) > 1) warning(fn, " matches multiple functions in regexFn_choices, please review.")
  104. x
  105. }
  106. #' Check if an updated version is available
  107. #'
  108. #' I included this because it can be difficult to tell if your RStudio Addins
  109. #' are up to date. I may add new features that you want but you won't hear about
  110. #' the updates. This function checks if an update is available, using GitHub
  111. #' tags. If an update is available, a modal dialog is shown when you start
  112. #' the regexplain gadget. This only happens once per R session, though, so feel
  113. #' free to ignore the message.
  114. #'
  115. #' @param gh_user GitHub user account
  116. #' @param gh_repo GitHub repo name
  117. #' @param this_version The currently installed version of the package
  118. #' @keywords internal
  119. check_version <- function(
  120. gh_user = "gadenbuie",
  121. gh_repo = "regexplain",
  122. this_version = packageVersion('regexplain')
  123. ) {
  124. ok_to_check <- getOption("regexplain.no.check.version", TRUE)
  125. if (!isTRUE(ok_to_check)) return(NULL)
  126. if (!requireNamespace('jsonlite', quietly = TRUE)) return(NULL)
  127. get_json <- purrr::possibly(jsonlite::fromJSON, NULL)
  128. gh_tags <- get_json(
  129. paste0("https://api.github.com/repos/", gh_user, "/", gh_repo, "/git/refs/tags"),
  130. simplifyDataFrame = TRUE
  131. )
  132. if (!is.null(gh_tags)) {
  133. gh_tags$tag <- sub("refs/tags/", "", gh_tags$ref, fixed = TRUE)
  134. gh_tags$version <- sub("^v\\.?", "", gh_tags$tag)
  135. }
  136. if (!is.null(gh_tags) && any(gh_tags$version > this_version)) {
  137. max_version <- max(gh_tags$version)
  138. max_tag <- gh_tags$tag[gh_tags$version == max_version]
  139. options(regexplain.no.check.version = FALSE)
  140. return(
  141. list(
  142. version = max_version,
  143. link = paste("https://github.com", gh_user, gh_repo, "releases/tag", max_tag, sep = "/")
  144. )
  145. )
  146. } else return(NULL)
  147. }
  148. #' Loads Regex Pattern Library
  149. #'
  150. #' Patterns sourced from [Regex Hub](https://projects.lukehaas.me/regexhub)
  151. #' are available at <https://github.com/lukehaas/RegexHub> and are copyright
  152. #' Luke Haas licensed under the MIT license available at
  153. #' <https://github.com/lukehaas/RegexHub/commit/3ab87b5a4fd2817b42e2e45dcf040d4f0164ea37>.
  154. #' Patterns source from [qdapRegex](https://github.com/trinker/qdapRegex) are
  155. #' copyright Tyler Rinker and Jason Gray, licensed under the GPL-2 license.
  156. #'
  157. #' @keywords internal
  158. get_regex_library <- function() {
  159. if (!requireNamespace("jsonlite")) {
  160. warning("Please install the `jsonlite` package to use template features")
  161. return(NULL)
  162. }
  163. f_patterns <- system.file("extdata", "patterns.json", package = "regexplain")
  164. if (!file.exists(f_patterns)) return(NULL)
  165. patterns <- jsonlite::fromJSON(
  166. f_patterns,
  167. simplifyVector = FALSE,
  168. simplifyDataFrame = FALSE,
  169. simplifyMatrix = FALSE
  170. )
  171. patterns <- purrr::keep(patterns, ~ .$name != "")
  172. patterns[order(purrr::map_chr(patterns, 'name'))]
  173. }