| inputPanel( | inputPanel( | ||||
| width = "100%;", | width = "100%;", | ||||
| selectInput('regexFn', label = 'Apply Function', | selectInput('regexFn', label = 'Apply Function', | ||||
| choices = regexFn_choices) | |||||
| choices = regexFn_choices), | |||||
| uiOutput("output_sub") | |||||
| ), | ), | ||||
| # verbatimTextOutput('output_result', placeholder = TRUE) | # verbatimTextOutput('output_result', placeholder = TRUE) | ||||
| tags$pre( | tags$pre( | ||||
| toHTML(paste(error_message, warning_message, res)) | toHTML(paste(error_message, warning_message, res)) | ||||
| }) | }) | ||||
| regexFn_replacement_val <- NULL | |||||
| output$output_sub <- renderUI({ | |||||
| req(input$regexFn) | |||||
| if (!input$regexFn %in% regexFn_substitute) return(NULL) | |||||
| textInputCode('regexFn_replacement', 'Subsitution', | |||||
| value = regexFn_replacement_val, | |||||
| placeholder = "Replacement Text") | |||||
| }) | |||||
| replacement <- reactive({ | |||||
| req(input$regexFn) | |||||
| if (!input$regexFn %in% regexFn_substitute) { | |||||
| NULL | |||||
| } else { | |||||
| regexFn_replacement_val <<- input$regexFn_replacement | |||||
| sanitize_text_input(input$regexFn_replacement) | |||||
| } | |||||
| }) | |||||
| output$output_result <- renderPrint({ | output$output_result <- renderPrint({ | ||||
| req(input$regexFn) | req(input$regexFn) | ||||
| regexPkg <- get_pkg_namespace(input$regexFn) | regexPkg <- get_pkg_namespace(input$regexFn) | ||||
| regexFn <- getFromNamespace(input$regexFn, regexPkg) | regexFn <- getFromNamespace(input$regexFn, regexPkg) | ||||
| req_sub_arg <- input$regexFn %in% regexFn_substitute | |||||
| x <- if (regexPkg == "base") { | x <- if (regexPkg == "base") { | ||||
| regexFn(pattern(), rtext()) | |||||
| if (req_sub_arg) { | |||||
| req(replacement()) | |||||
| regexFn(pattern(), replacement(), rtext()) | |||||
| } else { | |||||
| regexFn(pattern(), rtext()) | |||||
| } | |||||
| } else if (regexPkg == "stringr") { | } else if (regexPkg == "stringr") { | ||||
| regexFn(rtext(), pattern()) | |||||
| if (req_sub_arg) { | |||||
| req(replacement()) | |||||
| regexFn(rtext(), pattern(), replacement()) | |||||
| } else { | |||||
| regexFn(rtext(), pattern()) | |||||
| } | |||||
| } else { | } else { | ||||
| "Um. Not sure how I got here." | "Um. Not sure how I got here." | ||||
| } | } | ||||
| } | } | ||||
| sanitize_text_input <- function(x) { | sanitize_text_input <- function(x) { | ||||
| if (is.null(x) || !nchar(x)) return(x) | |||||
| if (grepl("\\u|\\x|\\N|\\a|\\o", x)) { | if (grepl("\\u|\\x|\\N|\\a|\\o", x)) { | ||||
| try({ | try({ | ||||
| y <- stringi::stri_unescape_unicode(x) | y <- stringi::stri_unescape_unicode(x) | ||||
| base = c( | base = c( | ||||
| "grep", | "grep", | ||||
| "grepl", | "grepl", | ||||
| "sub", #<< | |||||
| "gsub", #<< | |||||
| "regexpr", | "regexpr", | ||||
| "gregexpr", | "gregexpr", | ||||
| "regexec" | "regexec" | ||||
| "str_extract_all", | "str_extract_all", | ||||
| "str_match", | "str_match", | ||||
| "str_match_all", | "str_match_all", | ||||
| "str_replace", #<< | |||||
| "str_replace_all", #<< | |||||
| "str_split" | "str_split" | ||||
| ) | ) | ||||
| ) | ) | ||||
| regexFn_substitute <- c( | |||||
| paste0(c("", "g"), "sub"), | |||||
| paste0("str_replace", c("", "_all")) | |||||
| ) | |||||
| get_pkg_namespace <- function(fn) { | get_pkg_namespace <- function(fn) { | ||||
| x <- names(purrr::keep(regexFn_choices, ~ (fn %in% .))) | x <- names(purrr::keep(regexFn_choices, ~ (fn %in% .))) | ||||
| if (length(x) > 1) warning(fn, " matches multiple functions in regexFn_choices, please review.") | if (length(x) > 1) warning(fn, " matches multiple functions in regexFn_choices, please review.") |