🔍 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.

875 lines
36KB

  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. #' @import miniUI
  8. #' @import shiny
  9. #' @param text Text to explore in gadget (editable using interface)
  10. #' @param start_page Open gadget to this tab, one of `"Text"`, `"RegEx"`,
  11. #' `"Output"`, or `"Help"`
  12. #' @export
  13. regex_gadget <- function(
  14. text = NULL,
  15. start_page = if (is.null(text)) "Text" else "RegEx"
  16. ) {
  17. stopifnot(requireNamespace("miniUI"), requireNamespace("shiny"))
  18. update_available <- check_version()
  19. # ---- UI ----
  20. ui <- miniPage(
  21. shiny::includeCSS(system.file("styles", "style.css", package = "regexplain")),
  22. shiny::includeCSS(system.file("styles", "gadget.css", package = "regexplain")),
  23. gadgetTitleBar(
  24. "RegExplain",
  25. right = miniTitleBarButton("done", "Send RegEx To Console", TRUE)
  26. ),
  27. miniTabstripPanel(
  28. selected = match.arg(start_page, c("Text", "RegEx", "Output", "Help")),
  29. # --- UI - Tab - Text ----
  30. miniTabPanel(
  31. "Text", icon = icon('file-text-o'),
  32. miniContentPanel(
  33. fillCol(
  34. textAreaInputAlt('text',
  35. label = "Text to search or parse",
  36. value = paste(text, collapse = "\n"),
  37. resize = "both",
  38. width = "100%",
  39. height="90%",
  40. placeholder = "Paste, enter, or edit your sample text here.")
  41. )
  42. )
  43. ),
  44. # ---- UI - Tab - Regex ----
  45. miniTabPanel(
  46. "RegEx", icon = icon('terminal'),
  47. miniContentPanel(
  48. fillCol(
  49. flex = c(1, 3),
  50. fillCol(
  51. flex = c(1, 1),
  52. fillRow(
  53. flex = c(6, 1),
  54. textInputCode('pattern', 'RegEx', width = "100%",
  55. placeholder = "Standard RegEx, e.g. \\w+_\\d{2,4}\\s+"),
  56. tags$div(style = "margin-top: 23px; margin-left:6px;",
  57. actionButton("library_show", "Library", class = "btn-success"))
  58. ),
  59. checkboxGroupInput(
  60. 'regex_options',
  61. label = HTML(
  62. '<div style="font-size: 1.25rem;">',
  63. 'Option Groups: ',
  64. '<span style="color: #337ab7;">RegExplain</span>,',
  65. '<span style="color: #5cb85c;">All</span>, ',
  66. '<span style="color: #f0ad4e;">Base only</span>',
  67. '</div>'
  68. ),
  69. inline = TRUE,
  70. width = "90%",
  71. choiceValues = list(
  72. "text_break_lines",
  73. "ignore.case",
  74. "fixed",
  75. "perl",
  76. "useBytes"),
  77. choiceNames = list(
  78. HTML('<span style="color: #337ab7;">Break Lines</span>'),
  79. HTML('<span style="color: #5cb85c;">Ignore Case</span>'),
  80. HTML('<span style="color: #5cb85c;">Fixed/Literal</span>'),
  81. HTML('<span style="color: #f0ad4e;">Perl Style</span>'),
  82. HTML('<span style="color: #f0ad4e;">Use Bytes</span>')),
  83. selected = c('text_break_lines', 'perl')
  84. )
  85. ),
  86. tags$div(
  87. class = "gadget-result",
  88. style = "overflow-y: scroll; height: 100%;",
  89. htmlOutput('result')
  90. )
  91. )
  92. )
  93. ),
  94. # ---- UI - Tab - Output ----
  95. miniTabPanel(
  96. "Output", icon = icon("table"),
  97. miniContentPanel(
  98. fillCol(
  99. flex = c(1, 3),
  100. inputPanel(
  101. tags$div(
  102. width = "100%;",
  103. selectInput('regexFn', label = 'Apply Function',
  104. choices = regexFn_choices),
  105. tags$span(class = "help-block",
  106. style = "font-size:1.25rem; margin-top:-10px; margin-bottom:0px; margin-left:4px;",
  107. "Adjust options in RegEx tab")
  108. ),
  109. uiOutput("output_sub")
  110. ),
  111. # verbatimTextOutput('output_result', placeholder = TRUE)
  112. tags$pre(
  113. id = "output_result",
  114. class = "shiny-text-output",
  115. style = "overflow-y: scroll; height: 100%;"
  116. )
  117. )
  118. )
  119. ),
  120. # ---- UI - Tab - Help ----
  121. miniTabPanel(
  122. "Help", icon = icon("support"),
  123. generate_help_ui(cheatsheet_only = FALSE)
  124. )
  125. )
  126. )
  127. # ---- Server ----
  128. server <- function(input, output, session) {
  129. if (!is.null(update_available)) {
  130. showModal(
  131. modalDialog(
  132. title = "Update Available \U1F389",
  133. easyClose = TRUE,
  134. footer = modalButton("OK"),
  135. tagList(
  136. tags$p(
  137. "Version", update_available$version, "is",
  138. tags$a(href = update_available$link,
  139. "available on GitHub.")
  140. ),
  141. if ("devtools" %in% installed.packages()) tags$p(
  142. "The fastest way to update is with devtools:",
  143. tags$pre(
  144. "devtools::update_packages(\"regexplain\")"
  145. )
  146. ),
  147. tags$p(
  148. class = 'help-block',
  149. "This message won't be shown again during this R session."
  150. )
  151. )
  152. )
  153. )
  154. }
  155. # ---- Server - Global ----
  156. rtext <- reactive({
  157. x <- if ('text_break_lines' %in% input$regex_options) {
  158. strsplit(input$text, "\n")[[1]]
  159. } else input$text
  160. x
  161. })
  162. pattern <- reactive({
  163. sanitize_text_input(input$pattern)
  164. })
  165. observe({
  166. if (getOption('regexplain.debug.gadget.text', FALSE)) {
  167. cat("\ntext :", rtext())
  168. }
  169. if (getOption('regexplain.debug.gadget.pattern', FALSE)) {
  170. cat("\npattern:", pattern())
  171. }
  172. if (getOption('regexplain.debug.gadget.replacement', FALSE)) {
  173. cat("\nreplace:", replacement())
  174. }
  175. cat("\n")
  176. })
  177. alert_result <- function(msg, type = "danger") {
  178. msg <- gsub("\n", "<br>", msg)
  179. msg <- gsub("\t", "&nbsp;&nbsp;", msg)
  180. paste0("<pre class='alert alert-", type, "' ",
  181. "style='padding: 4px; margin-top: 1px; margin-bottom: 4px;'>",
  182. paste(msg, collapse = "<br>"),
  183. "</pre>")
  184. }
  185. # ---- Server - Tab - Regex ----
  186. output$result <- renderUI({
  187. if (is.null(rtext())) return(NULL)
  188. delay <- getOption('regexplain.input_delay_ms', NULL)
  189. if (!is.null(delay)) invalidateLater(delay, session)
  190. if (pattern() == "") {
  191. return(toHTML(paste('<p class="results">', escape_html(rtext()), "</p>", collapse = "")))
  192. }
  193. res <- NULL
  194. error_message <- NULL
  195. warning_message <- NULL
  196. tryCatch({
  197. res <- paste(
  198. view_regex(
  199. rtext(),
  200. pattern(),
  201. ignore.case = 'ignore.case' %in% input$regex_options,
  202. perl = 'perl' %in% input$regex_options,
  203. fixed = 'fixed' %in% input$regex_options,
  204. useBytes = 'useBytes' %in% input$regex_options,
  205. # invert = 'invert' %in% input$regex_options,
  206. render = FALSE,
  207. escape = TRUE,
  208. exact = FALSE),
  209. collapse = ""
  210. )
  211. },
  212. error = function(e) {
  213. error_message <<- alert_result(e$message, "danger")
  214. },
  215. warning = function(w) {
  216. warning_message <<- alert_result(w$message, "warning")
  217. })
  218. if (is.null(res)) res <- toHTML(
  219. paste('<p class="results">', escape_html(rtext()), "</p>", collapse = "")
  220. )
  221. toHTML(paste(error_message, warning_message, res))
  222. })
  223. # ---- Server - Tab - RegEx - Library ----
  224. library_patterns <- get_regex_library()
  225. this_pattern <- reactive({
  226. req(input$library_pattern)
  227. purrr::keep(library_patterns, ~ .$name == input$library_pattern) %>%
  228. purrr::flatten()
  229. })
  230. observeEvent(input$library_show, {
  231. showModal(
  232. modalDialog(
  233. title = "Regex Library",
  234. easyClose = TRUE,
  235. footer = tagList(
  236. modalButton("Cancel"),
  237. actionButton("library_apply_pattern", "Use Pattern", class = "btn-success")
  238. ),
  239. selectInput("library_pattern", "Pattern",
  240. choices = c("Choose pattern" = "",
  241. purrr::set_names(purrr::map_chr(library_patterns, 'name')))),
  242. uiOutput("library_pattern_info")
  243. )
  244. )
  245. })
  246. output$library_pattern_info <- renderUI({
  247. req(this_pattern())
  248. tp <- this_pattern()
  249. rx_url <- "((https?|ftp|file)://)?([[:alnum:].-]+)\\.([a-zA-Z.]{2,6})([/[[:alpha:].-]*)*/?"
  250. tagList(
  251. tags$h5("Description"),
  252. tags$p(HTML(tp$description)),
  253. tags$h5("Pattern"),
  254. tags$pre(tp$regex),
  255. if (!is.null(tp$source)) tags$p(
  256. "Source:",
  257. if (grepl(rx_url, tp$source)) tags$a(href = tp$source, tp$source) else HTML(tp$source)
  258. )
  259. )
  260. })
  261. observeEvent(input$library_apply_pattern, {
  262. updateTextInput(session, "pattern", value = this_pattern()$regex, placeholder = "")
  263. updateSelectInput(session, "template", selected = "")
  264. removeModal()
  265. })
  266. observe({
  267. is_empty <- input$pattern == ""
  268. if (is_empty) updateTextInput(
  269. session, "pattern",
  270. placeholder = "Standard RegEx, e.g. \\w+_\\d{2,4}\\s+")
  271. })
  272. # ---- Server - Tab - Output ----
  273. regexFn_replacement_val <- NULL
  274. output$output_sub <- renderUI({
  275. req(input$regexFn)
  276. if (!input$regexFn %in% regexFn_substitute) return(NULL)
  277. textInputCode('regexFn_replacement', 'Subsitution',
  278. value = regexFn_replacement_val,
  279. placeholder = "Replacement Text")
  280. })
  281. replacement <- reactive({
  282. req(input$regexFn)
  283. if (!input$regexFn %in% regexFn_substitute) {
  284. NULL
  285. } else {
  286. regexFn_replacement_val <<- input$regexFn_replacement
  287. sanitize_text_input(input$regexFn_replacement)
  288. }
  289. })
  290. output$output_result <- renderPrint({
  291. req(input$regexFn)
  292. regexPkg <- get_pkg_namespace(input$regexFn)
  293. if (!requireNamespace(regexPkg, quietly = TRUE)) {
  294. return(cat(
  295. paste0(
  296. "The package `", regexPkg, "` is not installed.\n",
  297. "To preview results from this package, please run\n\n",
  298. " install.packages(\"", regexPkg, "\")"
  299. )
  300. ))
  301. }
  302. regexFn <- getFromNamespace(input$regexFn, regexPkg)
  303. req_sub_arg <- input$regexFn %in% regexFn_substitute
  304. x <- if (regexPkg == "base") {
  305. if (req_sub_arg) {
  306. req(replacement())
  307. regexFn(pattern(), replacement(), rtext(),
  308. ignore.case = 'ignore.case' %in% input$regex_options,
  309. perl = 'perl' %in% input$regex_options,
  310. fixed = 'fixed' %in% input$regex_options,
  311. useBytes = 'useBytes' %in% input$regex_options)
  312. } else {
  313. regexFn(pattern(), rtext(),
  314. ignore.case = 'ignore.case' %in% input$regex_options,
  315. perl = 'perl' %in% input$regex_options,
  316. fixed = 'fixed' %in% input$regex_options,
  317. useBytes = 'useBytes' %in% input$regex_options)
  318. }
  319. } else if (regexPkg == "stringr") {
  320. if (req_sub_arg) {
  321. req(replacement())
  322. regexFn(
  323. rtext(),
  324. stringr::regex(
  325. pattern(),
  326. ignore_case = 'ignore.case' %in% input$regex_options,
  327. literal = 'fixed' %in% input$regex_options
  328. ),
  329. replacement()
  330. )
  331. } else {
  332. regexFn(
  333. rtext(),
  334. stringr::regex(
  335. pattern(),
  336. ignore_case = 'ignore.case' %in% input$regex_options,
  337. literal = 'fixed' %in% input$regex_options
  338. )
  339. )
  340. }
  341. } else if (regexPkg == "rematch2") {
  342. regexFn(rtext(), pattern(),
  343. ignore.case = 'ignore.case' %in% input$regex_options,
  344. perl = 'perl' %in% input$regex_options,
  345. fixed = 'fixed' %in% input$regex_options,
  346. useBytes = 'useBytes' %in% input$regex_options)
  347. } else {
  348. "Um. Not sure how I got here."
  349. }
  350. print(x)
  351. })
  352. # ---- Server - Tab - Help ----
  353. HELP_DEFAULT_TEXT <- c(
  354. "<h3>Welcome to RegExplain</h3>",
  355. "<p>If you're new to regular expressions, one of the best places to start is <a href=\"http://stringr.tidyverse.org/articles/regular-expressions.html\">the regular expressions vignette</a> from <code>stringr</code>. The chapter on strings in <a href=\"http://r4ds.had.co.nz/strings.html\">R for Data Science</a> is also an excellent first resource.</p>",
  356. "<p><strong>Exploring or looking for a challenge?</strong> Click on <i>Try These Examples</i> to see what you can do with this addin.</p>",
  357. "<h4>Getting Started</h4>",
  358. "<ul>",
  359. "<li><p><i class=\"fa fa-file-text-o\"></i> Enter or edit the <strong>Text</strong> you want to search.</p></li>",
  360. "<li><p><i class=\"fa fa-terminal\"></i> Edit your <strong>RegEx</strong> and view matches in real time.</p></li>",
  361. "<li><p><i class=\"fa fa-table\"></i> Test the <strong>Output</strong> of your regular expression with common functions, including <i>search and replace</i> functions.</p></li>",
  362. "<li><p><i class=\"fa fa-support\"></i> Get <strong>Help</strong> and look up the regular expression syntax.</p></li>",
  363. "</ul>",
  364. "<h4>Escaping characters</h4>",
  365. "<p>In order to store a backslash (<code>\\</code>) as a character in R, backslashes need to be escaped...with another backslash! To write a literal <code>\\</code> in an R character string, you need to actually store <code>&quot;\\\\&quot;</code>.</p>",
  366. "<p>In regular expressions, <code>\\w</code> stands for any alphabetical character, but to store it in a string in R you need <code>&quot;\\\\w&quot;</code>.</p>",
  367. "<p>Inside <strong>RegExplain</strong>, however, standard regular expressions can be used so that you can easily copy patterns from other places. When you click on the <span class=\"btn btn-xs btn-primary\">Send RegEX to Console</span> button, the necessary extra <code>\\</code> will be included.</p>",
  368. "<p>An extra backslash is still needed to match a literal <code>\\</code> in standard regular expressions. This means that you will need to enter <code>\\\\</code> in the <strong>RegEx</strong> tab, and the output to R will be <code>&quot;\\\\\\\\&quot;</code>.</p>"
  369. )
  370. # avoid CRAN check NOTES
  371. help_text <- NULL # in help_server.R
  372. make_help_tab_text <- NULL # in help_server.R
  373. source(system.file("shiny", "help_server.R", package = "regexplain"), local = TRUE)
  374. observeEvent(input$help_resources, {
  375. tagList(
  376. tags$h3("Resources"),
  377. tags$p("There are lots of great resources available for learning and working with regular expressions."),
  378. tags$h4("Regular Expressions in R"),
  379. tags$ul(
  380. tags$li(tags$p("The", tags$a(href = "http://stringr.tidyverse.org/articles/regular-expressions.html", "Regular Expressions vignette"),
  381. "from", tags$code("stringr"), "is an excellent first introduction to regular expressions in R.")),
  382. tags$li(tags$p("The", tags$a(href = "http://r4ds.had.co.nz/strings.html", "chapter on strings"),
  383. "in", tags$a(href = "http://r4ds.had.co.nz/", "R for Data Science"),
  384. "is also a great overall introduction.")),
  385. tags$li(tags$p("RStudio's",
  386. tags$a(href = "https://www.rstudio.com/wp-content/uploads/2016/09/RegExCheatsheet.pdf", "RegEx CheatSheet"),
  387. "is a good pocket reference.")),
  388. tags$li(tags$p("Or try the", tags$strong("Regexplain Cheatsheet"), "addin installed with this package."))
  389. ),
  390. tags$h4("Online Resources"),
  391. tags$ul(
  392. tags$li(tags$a(href = "https://github.com/aloisdg/awesome-regex", "Awesome RegEx")),
  393. tags$li(tags$a(href = "https://www.regular-expressions.info", "Regular-Expressions.info")),
  394. tags$li(tags$a(href = "https://projects.lukehaas.me/regexhub", "Regex Hub"), "- common regex patterns"),
  395. tags$li(tags$a(href = "http://regexlib.com/DisplayPatterns.aspx", "RegExLib.com"), "- large collection of searchable patterns")
  396. ),
  397. tags$h4("Live Preview and Explanations"),
  398. tags$ul(
  399. tags$li(tags$a(href = "https://regexr.com", "https://regexr.com")),
  400. tags$li(tags$a(href = "https://regexper.com/", "https://regexper.com")),
  401. tags$li(tags$a(href = "https://debuggex.com", "https://debuggex.com")),
  402. tags$li(tags$a(href = "https://regex101.com", "https://regex101.com")),
  403. tags$li(tags$a(href = "http://rick.measham.id.au/paste/explain", "http://rick.measham.id.au/paste/explain")),
  404. tags$li(tags$a(href = "http://www.mactechnologies.com/index.php?page=downloads#regexrx", "RegexRx"), "(app)"),
  405. tags$li(tags$a(href = "https://www.regexbuddy.com/", "Regex Buddy"), "(paid app)")
  406. ),
  407. tags$h4("Regex and String R Packages"),
  408. tags$dl(
  409. tags$dt(tags$a(href = "https://stringr.tidyverse.org/", "stringr")),
  410. tags$dd("A cohesive set of functions designed to make working with strings as easy as possible"),
  411. tags$dt(tags$a(href = "http://www.gagolewski.com/software/stringi/", "stringi")),
  412. tags$dd("THE R package for very fast, correct, consistent, and convenient string/text processing"),
  413. tags$dt(tags$a(href = "https://github.com/trinker/regexr", "regexr")),
  414. tags$dd("An R framework for constructing and managing human readable regular expressions"),
  415. tags$dt(tags$a(href = "https://github.com/kevinushey/rex", "rex")),
  416. tags$dd("Friendly Regular Expressions: complex regular expressions from human readable expressions"),
  417. tags$dt(tags$a(href = "https://github.com/richierocks/rebus", "rebus")),
  418. tags$dd("Build regular expressions in a human readable way"),
  419. tags$dt(tags$a(href = "https://www.r-pkg.org/pkg/qdapRegex", "qdapRegex")),
  420. tags$dd("A collection of regular expression tools for extraction/removal/replacement of common patterns in text documents"),
  421. tags$dt(tags$a(href = "https://github.com/AdamSpannbauer/r_regex_tester_app", "R Regex Tester")),
  422. tags$dd("A Shiny app for testing regular expressions")
  423. )
  424. ) %>%
  425. as.character() %>%
  426. help_text()
  427. })
  428. load_buttons <- function(..., extra_btns = NULL) {
  429. prefix <- paste(..., sep = "_")
  430. btns <- c(
  431. list(c("text", "Load Text", "btn-success"),
  432. c("pattern", "Load Pattern", "btn-primary")),
  433. extra_btns
  434. )
  435. tags$span(
  436. style = "display: inline-block;",
  437. purrr::map(
  438. btns,
  439. ~ actionButton(paste0(prefix, "_", .[1]), .[2], class = paste("btn-xs", if (!is.na(.[3])) .[3]))
  440. )
  441. )
  442. }
  443. observeEvent(input$help_try_this, {
  444. tagList(
  445. tags$h3("Try These Examples"),
  446. tags$p("Here are a couple interesting text extraction challenges you can try",
  447. "with this gadget."),
  448. tags$h4("Harvard Sentences"),
  449. tags$p("These examples come from the",
  450. tags$a(href = "http://r4ds.had.co.nz/strings.html", "R for Data Science"),
  451. "book and are based on a collection of short sentences called the Harvard Sentences."),
  452. tags$ol(
  453. tags$li(tags$p(
  454. "Find sentences that contain a color (i.e. red, orange, yellow, green, blue, purple).",
  455. load_buttons("help_try_this", "hs", "colors"))),
  456. tags$li(tags$p(
  457. "Use the text from Exercise 1 and make sure that only full words that are colors are found.",
  458. HTML("E.g. <code>red</code> and not <code>flickered</code>."),
  459. tags$span(style = "display: inline-block;",
  460. actionButton("help_try_this_hs_colors_word", "Load Pattern", class = "btn-xs btn-primary"),
  461. actionButton("help_try_this_hs_colors_hint", "Show Hint", class = "btn-xs"))
  462. )),
  463. tags$li(tags$p(
  464. "Extract nouns from sentences by finding any word that comes after \"a\" or \"the\".",
  465. "Use", actionLink("help_try_this_hs_words_go2_groups", "Groups"),
  466. "to extract the article and possible noun separately and check your results with",
  467. HTML("<code>stringr::str_match()</code>:"),
  468. load_buttons("help_try_this", "hs", "words",
  469. extra_btns = list(c("output", "Check str_match()")))
  470. )),
  471. tags$li(tags$p(
  472. "Switch the order of the two words following the articles", '"a" or "the"',
  473. "using", actionLink("help_try_this_hs_refs_go2_groups", "backreferences,"),
  474. "so that", tags$code("the birch canoe"), "would read",
  475. HTML("<code>the canoe birch</code>. Use <code>sub</code>"),
  476. "in the", tags$strong("Output"), "tab to replace the matched pattern.",
  477. load_buttons("help_try_this", "hs", "refs",
  478. extra_btns = list(c("output", "Load Replacement")))
  479. ))
  480. ),
  481. tags$h4("Phone Numbers"),
  482. tags$p("This example is also from the",
  483. tags$a(href = "http://r4ds.had.co.nz/strings.html#other-types-of-pattern",
  484. "R for Data Science"),
  485. "book. Phone numbers in the United States start with a 3-digit area code,",
  486. "followed by another 3 digits and a final 4-digit group.",
  487. "Sometimes the area code is wrapped in parenthesis, or sometimes dots or dashes",
  488. "are used to separate the digit groups. Try to extract each digit group from these phone numbers:",
  489. load_buttons("help_try_this", "phone",
  490. extra_btns = list(c("output", "Check str_match()")))),
  491. tags$h4("CSS Unit Validation"),
  492. tags$p("This example is used in", tags$code("validateCssUnit()"),
  493. "in the", tags$a(href="https://www.r-pkg.org/pkg/htmltools", "htmltools package."),
  494. "CSS units can be integer or decimal numbers with units such as",
  495. "in, cm, mm, em, ex, pt, px, etc. (see the list",
  496. HTML('<a href="https://www.w3.org/Style/Examples/007/units.en.html">here</a>).'),
  497. "Try to extract the number and unit from these units:",
  498. load_buttons("help_try_this", "css")),
  499. tags$h4("Parse Github Repos"),
  500. tags$p("This example is from the",
  501. tags$a(href = "https://www.r-pkg.org/pkg/rematch2", "rematch2 package."),
  502. "Github repositories are often specified in like",
  503. HTML("<code>user/repo/subdir@ref*release</code> or <code>user/repo/subdir#PR</code>"),
  504. "where only", tags$code("user"), "and", tags$code("repo"), "are required elements.",
  505. "Try to extract each piece of the repo text and use",
  506. tags$code("rematch2::re_match()"), "to extract a tidy tibble of matches:",
  507. load_buttons("help_try_this", "github",
  508. extra_btns = list(c("output", "Check re_match()"))))
  509. ) %>%
  510. as.character() %>%
  511. help_text()
  512. })
  513. observeEvent(input$help_try_this_hs_colors_text, {
  514. color_match <- "\\b(red|orange|yellow|green|blue|purple)\\b|red"
  515. color_text <- stringr::sentences[grepl(color_match, stringr::sentences)]
  516. color_text <- sample(color_text, 25)
  517. updateTextAreaInput(session, "text", value = paste(color_text, collapse = "\n"))
  518. showNotification("Text loaded! View it in Text tab", type = 'message')
  519. })
  520. observeEvent(input$help_try_this_hs_colors_pattern, {
  521. color_match <- "red|orange|yellow|green|blue|purple"
  522. updateTextInput(session, 'pattern', value = color_match)
  523. updateCheckboxGroupInput(session, "regex_options", selected = c('text_break_lines', 'perl'))
  524. showNotification("Pattern loaded! View it in RegEx tab", type = 'message')
  525. })
  526. observeEvent(input$help_try_this_hs_colors_word, {
  527. color_match <- "\\b(red|orange|yellow|green|blue|purple)\\b"
  528. updateTextInput(session, 'pattern', value = color_match)
  529. updateCheckboxGroupInput(session, "regex_options", selected = c('text_break_lines', 'perl'))
  530. showNotification("Pattern loaded! View it in RegEx tab", type = 'message')
  531. })
  532. observeEvent(input$help_try_this_hs_colors_hint, {
  533. showModal(
  534. modalDialog(title = "Hint \U0001f575", footer = NULL, easyClose = TRUE,
  535. tags$p("Try using the", tags$strong("word boundary"), "anchor."))
  536. )
  537. })
  538. observeEvent(input$help_try_this_hs_words_go2_groups, {
  539. make_help_tab_text("groups")
  540. })
  541. observeEvent(input$help_try_this_hs_words_output, {
  542. updateSelectInput(session, 'regexFn', selected = 'str_match')
  543. showNotification("Go to Output tab to see results from str_match()", type = "message")
  544. })
  545. observeEvent(input$help_try_this_hs_words_text, {
  546. hs_text <- sample(stringr::sentences, 25)
  547. updateTextAreaInput(session, "text", value = paste(hs_text, collapse = "\n"))
  548. showNotification("Text loaded! View it in Text tab", type = 'message')
  549. })
  550. observeEvent(input$help_try_this_hs_words_pattern, {
  551. noun_pattern <- "(a|the) ([^ ]+)"
  552. updateTextInput(session, 'pattern', value = noun_pattern)
  553. updateCheckboxGroupInput(session, "regex_options", selected = c('text_break_lines'))
  554. updateSelectInput(session, 'regexFn', selected = "str_match")
  555. showNotification("Pattern loaded! View it in RegEx and Output tabs", type = 'message')
  556. })
  557. observeEvent(input$help_try_this_hs_refs_go2_groups, {
  558. make_help_tab_text("groups")
  559. })
  560. observeEvent(input$help_try_this_hs_refs_output, {
  561. regexFn_replacement_val <<- "\\1 \\3 \\2"
  562. updateSelectInput(session, 'regexFn', selected = 'sub')
  563. showNotification("Replacement loaded! Go to Output tab to see results", type = "message")
  564. })
  565. observeEvent(input$help_try_this_hs_refs_text, {
  566. hs_text <- sample(stringr::sentences, 25)
  567. updateTextAreaInput(session, "text", value = paste(hs_text, collapse = "\n"))
  568. showNotification("Text loaded! View it in Text tab", type = 'message')
  569. })
  570. observeEvent(input$help_try_this_hs_refs_pattern, {
  571. noun_pattern <- "(a|the) ([^ ]+) ([^ ]+)"
  572. updateTextInput(session, 'pattern', value = noun_pattern)
  573. updateCheckboxGroupInput(session, "regex_options", selected = c('text_break_lines'))
  574. showNotification("Pattern loaded! View it in RegEx tab", type = 'message')
  575. })
  576. observeEvent(input$help_try_this_phone_output, {
  577. updateSelectInput(session, 'regexFn', selected = 'str_match')
  578. showNotification("Go to Output tab to see results from str_match()", type = "message")
  579. })
  580. observeEvent(input$help_try_this_phone_text, {
  581. phone_number <- function() {
  582. first <- function() sample(2:9, 1)
  583. others <- function(n) sample(1:9, n, replace = TRUE)
  584. wrap_types <- c("parens", "dash", "space", "dot", "nothing")
  585. wrap <- function(x, type) {
  586. switch(
  587. match.arg(type, choices = wrap_types),
  588. parens = paste0("(", x, ")"),
  589. dash = paste0(x, "-"),
  590. space = paste0(x, " "),
  591. dot = paste0(x, "."),
  592. x
  593. )
  594. }
  595. area_code <- paste0(c(first(), others(2)), collapse = "")
  596. group1 <- paste0(c(first(), others(2)), collapse = "")
  597. group2 <- paste0(c(first(), others(3)), collapse = "")
  598. area_wrap <- sample(wrap_types, 1)
  599. other_wrap <- if (area_wrap == "parens") sample(wrap_types[-1], 1) else area_wrap
  600. paste0(wrap(area_code, area_wrap), wrap(group1, other_wrap), group2)
  601. }
  602. phone_numbers <- replicate(25, phone_number())
  603. updateTextAreaInput(session, "text", value = paste(phone_numbers, collapse = "\n"))
  604. showNotification("Text loaded! View it in Text tab", type = 'message')
  605. })
  606. observeEvent(input$help_try_this_phone_pattern, {
  607. phone_pattern <- "\\(?(\\d{3})[-). ]?(\\d{3})[- .]?(\\d{4})"
  608. updateTextInput(session, 'pattern', value = phone_pattern)
  609. updateCheckboxGroupInput(session, "regex_options", selected = c('text_break_lines'))
  610. showNotification("Pattern loaded! View it in RegEx tab", type = 'message')
  611. })
  612. observeEvent(input$help_try_this_github_text, {
  613. github_repos <- c(
  614. "metacran/crandb",
  615. "jeroenooms/curl@v0.9.3",
  616. "jimhester/covr#47",
  617. "hadley/dplyr@*release",
  618. "r-lib/remotes@550a3c7d3f9e1493a2ba"
  619. )
  620. updateTextAreaInput(session, "text", value = paste(github_repos, collapse = "\n"))
  621. showNotification("Text loaded! Go to RegEx Tab", type = 'message')
  622. })
  623. observeEvent(input$help_try_this_github_pattern, {
  624. owner_rx <- "(?:(?<owner>[^/]+)/)?"
  625. repo_rx <- "(?<repo>[^/@#]+)"
  626. subdir_rx <- "(?:/(?<subdir>[^@#]*[^@#/]))?"
  627. ref_rx <- "(?:@(?<ref>[^*].*))"
  628. pull_rx <- "(?:#(?<pull>[0-9]+))"
  629. release_rx <- "(?:@(?<release>[*]release))"
  630. subtype_rx <- sprintf("(?:%s|%s|%s)?", ref_rx, pull_rx, release_rx)
  631. github_rx <- sprintf(
  632. "^(?:%s%s%s%s|(?<catchall>.*))$",
  633. owner_rx, repo_rx, subdir_rx, subtype_rx
  634. )
  635. updateTextInput(session, 'pattern', value = github_rx)
  636. updateCheckboxGroupInput(session, "regex_options", selected = c('text_break_lines', 'perl'))
  637. showNotification("Pattern loaded! Go to RegEx Tab", type = 'message')
  638. })
  639. observeEvent(input$help_try_this_github_output, {
  640. updateSelectInput(session, 'regexFn', selected = 're_match')
  641. showNotification("Go to Output tab to see results from re_match()", type = "message")
  642. })
  643. observeEvent(input$help_try_this_css_text, {
  644. css_units <- c(
  645. "125%","16pt","2cm","7em","3ex","24pt",
  646. ".15in","20pc","5.9vw","3.0vh","2vmin"
  647. )
  648. showNotification("Example text loaded! Go to RegEx tab", type = "message")
  649. updateTextAreaInput(session, "text", value = paste(css_units, collapse = "\n"))
  650. })
  651. observeEvent(input$help_try_this_css_pattern, {
  652. pattern <- "^(auto|inherit|((\\.\\d+)|(\\d+(\\.\\d+)?))(%|in|cm|mm|em|ex|pt|pc|px|vh|vw|vmin|vmax))$"
  653. updateTextInput(session, "pattern", value = pattern)
  654. updateCheckboxGroupInput(session, 'regex_options', selected = c('text_break_lines', 'perl'))
  655. showNotification("Pattern loaded! Go to RegEx tab", type = "message")
  656. })
  657. # ---- Server - Tab - Exit ----
  658. observeEvent(input$done, {
  659. if (pattern() != "") {
  660. pattern <- paste0('pattern <- "', escape_backslash(pattern()), '"')
  661. if (any(c("perl", "fixed", "ignore.case", "useBytes") %in% input$regex_options)) {
  662. options <- input$regex_options[input$regex_options != "text_break_lines"]
  663. options <- paste0(options, "=TRUE", collapse = ", ")
  664. pattern <- paste(pattern, "#", options)
  665. }
  666. if ("regexFn_replacement" %in% names(input) && isTruthy(replacement())) {
  667. pattern <- paste0(
  668. pattern, "\n",
  669. 'replacement <- "', escape_backslash(replacement()), '"'
  670. )
  671. }
  672. rstudioapi::sendToConsole(pattern, FALSE)
  673. }
  674. stopApp()
  675. })
  676. observeEvent(input$cancel, {
  677. stopApp()
  678. })
  679. }
  680. viewer <- shiny::paneViewer(minHeight = 800)
  681. runGadget(ui, server, viewer = viewer)
  682. }
  683. # ---- Gadget Helper Functions and Variables ----
  684. sanitize_text_input <- function(x) {
  685. if (is.null(x) || !nchar(x)) return(x)
  686. rx_unicode <- "\\\\u[0-9a-f]{4,8}"
  687. rx_hex <- "\\\\x[0-9a-f]{2}|\\\\x\\{[0-9a-f]{1,6}\\}"
  688. rx_octal <- "\\\\[0][0-7]{1,3}"
  689. rx_escape <- paste(rx_unicode, rx_hex, rx_octal, sep = "|")
  690. if (grepl(rx_escape, x, ignore.case = TRUE)) {
  691. try({
  692. y <- stringi::stri_unescape_unicode(x)
  693. }, silent = TRUE)
  694. if (!is.na(y)) x <- y
  695. }
  696. # x <- gsub("\u201C|\u201D", '"', x)
  697. # x <- gsub("\u2018|\u2019", "'", x)
  698. x
  699. }
  700. toHTML <- function(...) {
  701. x <- paste(..., collapse = "")
  702. x <- gsub("\n", "\\\\n", x)
  703. x <- gsub("\t", "\\\\t", x)
  704. x <- gsub("\r", "\\\\r", x)
  705. HTML(x)
  706. }
  707. regexFn_choices <- list(
  708. "Choose a function" = "",
  709. base = c(
  710. "grep",
  711. "grepl",
  712. "sub", #<<
  713. "gsub", #<<
  714. "regexpr",
  715. "gregexpr",
  716. "regexec"
  717. ),
  718. stringr = c(
  719. "str_detect",
  720. "str_locate",
  721. "str_locate_all",
  722. "str_extract",
  723. "str_extract_all",
  724. "str_match",
  725. "str_match_all",
  726. "str_replace", #<<
  727. "str_replace_all", #<<
  728. "str_split"
  729. ),
  730. "rematch2" = c(
  731. "re_match",
  732. "re_match_all",
  733. "re_exec",
  734. "re_exec_all"
  735. )
  736. )
  737. regexFn_substitute <- c(
  738. paste0(c("", "g"), "sub"),
  739. paste0("str_replace", c("", "_all"))
  740. )
  741. get_pkg_namespace <- function(fn) {
  742. x <- names(purrr::keep(regexFn_choices, ~ (fn %in% .)))
  743. if (length(x) > 1) warning(fn, " matches multiple functions in regexFn_choices, please review.")
  744. x
  745. }
  746. #' Check if an updated version is available
  747. #'
  748. #' I included this because it can be difficult to tell if your RStudio Addins
  749. #' are up to date. I may add new features that you want but you won't hear about
  750. #' the updates. This function checks if an update is available, using GitHub
  751. #' tags. If an update is available, a modal dialog is shown when you start
  752. #' the regexplain gadget. This only happens once per R session, though, so feel
  753. #' free to ignore the message.
  754. #'
  755. #' @param gh_user GitHub user account
  756. #' @param gh_repo GitHub repo name
  757. #' @param this_version The currently installed version of the package
  758. #' @keywords internal
  759. check_version <- function(
  760. gh_user = "gadenbuie",
  761. gh_repo = "regexplain",
  762. this_version = packageVersion('regexplain')
  763. ) {
  764. ok_to_check <- getOption("regexplain.no.check.version", TRUE)
  765. if (!ok_to_check) return(NULL)
  766. if (!requireNamespace('jsonlite', quietly = TRUE)) return(NULL)
  767. get_json <- purrr::possibly(jsonlite::fromJSON, NULL)
  768. gh_tags <- get_json(
  769. paste0("https://api.github.com/repos/", gh_user, "/", gh_repo, "/git/refs/tags"),
  770. simplifyDataFrame = TRUE
  771. )
  772. if (!is.null(gh_tags)) {
  773. gh_tags$tag <- sub("refs/tags/", "", gh_tags$ref, fixed = TRUE)
  774. gh_tags$version <- sub("^v\\.?", "", gh_tags$tag)
  775. }
  776. if (!is.null(gh_tags) && any(gh_tags$version > this_version)) {
  777. max_version <- max(gh_tags$version)
  778. max_tag <- gh_tags$tag[gh_tags$version == max_version]
  779. options(regexplain.no.check.version = FALSE)
  780. return(
  781. list(
  782. version = max_version,
  783. link = paste("https://github.com", gh_user, gh_repo, "releases/tag", max_tag, sep = "/")
  784. )
  785. )
  786. } else return(NULL)
  787. }
  788. #' Loads Regex Pattern Library
  789. #'
  790. #' Patterns sourced from [Regex Hub](https://projects.lukehaas.me/regexhub)
  791. #' are available at <https://github.com/lukehaas/RegexHub> and are copyright
  792. #' Luke Haas licensed under the MIT license available at
  793. #' <https://github.com/lukehaas/RegexHub/commit/3ab87b5a4fd2817b42e2e45dcf040d4f0164ea37>.
  794. #' Patterns source from [qdapRegex](https://github.com/trinker/qdapRegex) are
  795. #' copyright Tyler Rinker and Jason Gray, licensed under the GPL-2 license.
  796. #'
  797. #' @keywords internal
  798. get_regex_library <- function() {
  799. if (!requireNamespace("jsonlite")) {
  800. warning("Please install the `jsonlite` package to use template features")
  801. return(NULL)
  802. }
  803. f_patterns <- system.file("extdata", "patterns.json", package = "regexplain")
  804. if (!file.exists(f_patterns)) return(NULL)
  805. patterns <- jsonlite::fromJSON(
  806. f_patterns,
  807. simplifyVector = FALSE,
  808. simplifyDataFrame = FALSE,
  809. simplifyMatrix = FALSE
  810. )
  811. patterns <- purrr::keep(patterns, ~ .$name != "")
  812. patterns[order(purrr::map_chr(patterns, 'name'))]
  813. }