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

46 satır
1.2KB

  1. #' regexplain_addin
  2. #'
  3. #' @keywords internal
  4. regexplain_addin <- function() {
  5. # Get the document context.
  6. context <- rstudioapi::getActiveDocumentContext()
  7. # Get context text
  8. ctx_text <- context$selection[[1]]$text
  9. # If it is one line and evaluates to something, use that
  10. # Otherwise treat as text
  11. obj <- tryCatch({
  12. if (grepl("\n", ctx_text)) {
  13. ctx_text[1:min(length(ctx_text), 100)]
  14. } else {
  15. x <- eval(parse(text = ctx_text))
  16. x <- as.character(x)
  17. if (length(x) == 1 && grepl("\n", x))
  18. x <- strsplit(x, "\n")[[1]]
  19. if (length(x) > 10) {
  20. message(ctx_text, " gave ", length(x), " lines, limiting to first 10 unique lines.")
  21. x <- unique(x)
  22. x[1:min(length(x), 10)]
  23. } else x
  24. }
  25. },
  26. error = function(e) {as.character(ctx_text[1:min(length(ctx_text), 100)])})
  27. regex_gadget(if (length(obj) && obj != "") obj)
  28. }
  29. #' regexplain file loader
  30. #'
  31. #' @keywords internal
  32. regexplain_file <- function() {
  33. fname <- file.choose()
  34. x <- readLines(fname)
  35. if (length(x) > 100) {
  36. message("There were ", format(length(x), big.mark = ","), " lines in ", fname, "\nUsing only first 100.")
  37. x <- x[1:100]
  38. }
  39. regex_gadget(x, "Regex")
  40. }