Open slodge opened 3 years ago
Hello,
The license is GPL-3, I've standardized the file, thanks. Does this works for you ? What's your use-case ?
Victor
Sorry - I missed the update on this.
I'm happy with whatever license you choose - so thanks for clearing up the license)
The use case I was playing with (and will return to one day) was going through a file inside a console and prompting for each change with a list of numbered options (plus I wondered about adding "change all" and "change none" options).
The motivation at the time was I'd inherited a big package and I wanted to go through all the files and update them and I found the shiny UI a bit too mouse-driven and slow...
Here's the sort of code I was playing with...
devtools::load_all()
library(tidyverse)
library(R.Pax)
library(R.PriceTools)
library(R.Quant)
library(dplyr)
library(ggplot2)
library(lubridate)
path <- ".... what I was changing... "
files <- list.files(path = path, pattern = "*.[Rr]$", recursive = T)
text <- readr::read_file(paste(path, files[1], sep="/"))
to_script <-
tibble(Path = paste(path, files, sep="/")) %>%
mutate(
Text = map_chr(Path, ~readr::read_file(.x))
) %>%
pmap(list)
auto_choose <- list(
"filter" = "dplyr",
"lag" = "dplyr",
"tibble" = "tibble",
"as_tibble" = "tibble")
never_modify <- c(
"C",
"Date",
"date")
never_use_packages <- c(
"stats",
"utils",
"graphics")
auto_singles <- TRUE
changes <- tibble(
Path = character(),
Funs = character(),
Package = character(),
From = character(),
To = character()
)
to_script %>%
walk(function(row) {
parsed <- purrr::safely(get_script_funs)(row$Text, highlight=FALSE)
if (!is.null(parsed$error)) {
message("Skipping ", row$Path, " - failed to parse any functions")
message("Error was ", parsed$error$message)
return()
}
functions <-
parsed$result %>%
as_tibble()
if (nrow(functions) == 0) {
message("Skipping ", row$Path, " - nothing to do")
return(NULL)
}
message("Starting ", row$Path)
text <- row$Text %>% str_remove_all("\r")
text_lines <- row$Text %>% str_split("\n") %>% pluck(1)
functions %>%
filter(!(funs %in% never_modify)) %>%
filter(!(package %in% never_use_packages)) %>%
group_by(numrow, desc_start = desc(start), start, funs, fun_context) %>%
group_walk(.data = ., .keep = TRUE, .f = function(options, key) {
if (nrow(key) == 0) {
message("Nothing to do")
return(NULL)
}
message("Line ", key$numrow ,": ", key$funs, " in ", key$fun_context)
message("")
message("0 - do nothing")
available_options <-
options %>%
pull("package") %>%
imap(function(p, i) {
message(i, " - use ", p, " so ", p, "::", key$funs)
i
})
selected_index <- -1
if (length(available_options) == 1 &&
auto_singles) {
selected_index <- 1
}
if (!is.null(auto_choose[[key$funs]])) {
auto_package <- auto_choose[[key$funs]]
selected_index <- which(options$package == auto_package) %>% head(1)
if (length(selected_index) == 0) selected_index <- -1
}
available_options <- c(0, available_options)
while (selected_index < 0) {
choice <-
readline("Enter choice:") %>%
quietly(as.numeric)() %>%
pluck("result")
if (choice %in% available_options) {
selected_index <- choice
} else {
message("Invalid")
}
}
# do selected option
if (selected_index > 0) {
selected_option <-
options %>%
slice(selected_index)
row_text <- text_lines[[key$numrow]]
new_row_text <- paste0(
str_sub(row_text, end=key$start - 1),
selected_option$package,
"::",
str_sub(row_text, start=key$start)
)
message("updating to: ", new_row_text)
changes <<- changes %>% bind_rows(list(
Path = row$Path,
Funs = key$funs,
Package = selected_option$package,
From = row_text,
To = new_row_text)
)
text_lines[[key$numrow]] <<- new_row_text
message()
}
})
new_path <- paste0(row$Path)
readr::write_lines(text_lines, new_path, append = F)
})
changes %>%
count(Funs, Package) %>%
View()
Sorry again for missing the update from you - and thanks for clarifying the license 👍
I'd like to try to use this functionality from code. However, I'm not sure what open source license I can use for this. Currently the license file is very non-standard. If possible, could you possibly pick a license from https://opensource.org/licenses/ or clarify that this is closed source? Thanks Stuart