Преглед изворни кода

Updates to selection processing in addin

- Use global option regexplain.addin.max_lines (default 100)
- Give hint on warning about option
- Always use unique lines in selection
tags/v0.2.0
Garrick Aden-Buie пре 8 година
родитељ
комит
3083a5d3f2
4 измењених фајлова са 44 додато и 15 уклоњено
  1. +1
    -1
      R/regex_gadget.R
  2. +32
    -10
      R/rstudio_addins.R
  3. +8
    -2
      man/regexplain_addin.Rd
  4. +3
    -2
      man/regexplain_file.Rd

+ 1
- 1
R/regex_gadget.R Прегледај датотеку

}) })
} }


viewer <- shiny::paneViewer(minHeight = 1000)
viewer <- shiny::paneViewer(minHeight = 800)
runGadget(ui, server, viewer = viewer) runGadget(ui, server, viewer = viewer)
} }



+ 32
- 10
R/rstudio_addins.R Прегледај датотеку

#' regexplain_addin
#' RegExplain Addin
#'
#' Addin for regexplain. If the selection does not contain multiple lines, the
#' addin tries to evaluate the selection and coerce the result to a character
#' string. Only unique lines are passed to the gadget to maximize screen space.
#' The max number of lines returned is set by the option
#' `regexplain.addin.max_lines`, with a default value of 100 lines.
#' If evaluating the selection does not produce a character vector without
#' errors, the original selection is returned.
#' #'
#' @keywords internal #' @keywords internal
regexplain_addin <- function() { regexplain_addin <- function() {
# Get context text # Get context text
ctx_text <- context$selection[[1]]$text ctx_text <- context$selection[[1]]$text


max_lines <- getOption("rexeplain.addin.max_lines", 100)

# If it is one line and evaluates to something, use that # If it is one line and evaluates to something, use that
# Otherwise treat as text # Otherwise treat as text
obj <- tryCatch({ obj <- tryCatch({
if (grepl("\n", ctx_text)) { if (grepl("\n", ctx_text)) {
ctx_text[1:min(length(ctx_text), 100)]
ctx_text[1:min(length(ctx_text), max_lines)]
} else { } else {
x <- eval(parse(text = ctx_text)) x <- eval(parse(text = ctx_text))
if (inherits(x, "list")) x <- unlist(x)
x <- as.character(x) x <- as.character(x)
if (length(x) == 1 && grepl("\n", x)) if (length(x) == 1 && grepl("\n", x))
x <- strsplit(x, "\n")[[1]] x <- strsplit(x, "\n")[[1]]
if (length(x) > 10) {
message(ctx_text, " gave ", length(x), " lines, limiting to first 10 unique lines.")
x <- unique(x)
if (length(x) > max_lines) {
message(ctx_text, " gave ", length(x), " lines, limiting to first ",
max_lines, " unique lines. Set ",
"options('regexplain.addin.max_lines') to a higher value to ",
"increase the number of lines.")
x <- unique(x) x <- unique(x)
x[1:min(length(x), 10)]
x[1:min(length(x), max_lines)]
} else x } else x
} }
}, },
error = function(e) {as.character(ctx_text[1:min(length(ctx_text), 100)])})
error = function(e) {as.character(ctx_text[1:min(length(ctx_text), max_lines)])})


regex_gadget(if (length(obj) && obj != "") obj) regex_gadget(if (length(obj) && obj != "") obj)


} }


#' regexplain file loader
#' Regexplain File Addin
#'
#' See [regexplain_addin]. Opens file chooser to pick file, reads lines, returns
#' first `regexplain.addin.max_lines` (default 100).
#' #'
#' @keywords internal #' @keywords internal
regexplain_file <- function() { regexplain_file <- function() {
fname <- file.choose() fname <- file.choose()
x <- readLines(fname) x <- readLines(fname)
if (length(x) > 100) {
message("There were ", format(length(x), big.mark = ","), " lines in ", fname, "\nUsing only first 100.")
x <- x[1:100]
max_lines <- getOption("rexeplain.addin.max_lines", 100)
if (length(x) > max_lines) {
message("There were ", format(length(x), big.mark = ","), " lines in ",
fname, "\nUsing only first ", max_lines, ".\n",
"Set options('regexplain.addin.max_lines') to a higher value to ",
"increase the number of lines.")
x <- x[1:max_lines]
} }
regex_gadget(x, "RegEx") regex_gadget(x, "RegEx")
} }

+ 8
- 2
man/regexplain_addin.Rd Прегледај датотеку

% Please edit documentation in R/rstudio_addins.R % Please edit documentation in R/rstudio_addins.R
\name{regexplain_addin} \name{regexplain_addin}
\alias{regexplain_addin} \alias{regexplain_addin}
\title{regexplain_addin}
\title{RegExplain Addin}
\usage{ \usage{
regexplain_addin() regexplain_addin()
} }
\description{ \description{
regexplain_addin
Addin for regexplain. If the selection does not contain multiple lines, the
addin tries to evaluate the selection and coerce the result to a character
string. Only unique lines are passed to the gadget to maximize screen space.
The max number of lines returned is set by the option
\code{regexplain.addin.max_lines}, with a default value of 100 lines.
If evaluating the selection does not produce a character vector without
errors, the original selection is returned.
} }
\keyword{internal} \keyword{internal}

+ 3
- 2
man/regexplain_file.Rd Прегледај датотеку

% Please edit documentation in R/rstudio_addins.R % Please edit documentation in R/rstudio_addins.R
\name{regexplain_file} \name{regexplain_file}
\alias{regexplain_file} \alias{regexplain_file}
\title{regexplain file loader}
\title{Regexplain File Addin}
\usage{ \usage{
regexplain_file() regexplain_file()
} }
\description{ \description{
regexplain file loader
See \link{regexplain_addin}. Opens file chooser to pick file, reads lines, returns
first \code{regexplain.addin.max_lines} (default 100).
} }
\keyword{internal} \keyword{internal}

Loading…
Откажи
Сачувај