-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
use_here() #64
Labels
help wanted ❤️
we'd love your help!
Comments
Here is my attempt : use_here <- function(){
# get the working directory
# used here::: just to test
wd <- here:::.root_env$root$wd
# get the R(md) files in the folder tree
files <- list.files(wd, pattern="\\.R(md)?$", ignore.case=TRUE, recursive=TRUE)
# if it's an R package remove the files in the R/ directory
# and the ones in the test directory
is_pkg <- tryCatch({is_r_package$make_fix_file(); T}, error= function(...) F)
if(is_pkg){
files <- files[!grepl("^(R|tests|vignettes)", files)]
}
# get the files that were modified
files <- sapply(files, prepend_i_am)
# print them
cat(unlist(files), sep="\n")
}
prepend_i_am <- function(x, is_rmarkdown = grepl("\\.Rmd$", x, ignore.case = TRUE)){
# read the lines
lines <- readLines(x)
# check if a here::i_am call is made
has_i_am <- grepl("here::i_am\\(.+?\\)", lines)
# generate the i_am call
here_string <- sprintf('here::i_am("%s", uuid = "%s")', x, uuid::UUIDgenerate())
if(any(has_i_am)){
# get the line of the first call
which(has_i_am)[1] -> line
# if it's already in the first line return
if(line==1) return()
# grab the here_string defined by the user might not contain a uuid
here_string <- sub("(here::i_am\\(.+?\\) *;?)", "\\1", lines[line])
# remove the call
lines[line] <- sub(here_string, "", lines[line], fixed=T)
}
if(is_rmarkdown){
# get the first code chunk
add_here <- grep("``` *\\{ *r.+?\\}", lines, ignore.case=TRUE)[1]
# if it's already on the first line bail
if(any(has_i_am) && line==add_here+1) return()
# add it directly after the opening of the chunk
lines <- c(lines[1:add_here], here_string, lines[-(1:add_here)])
}else{
# add it to the first line
lines <- c(here_string, lines)
}
# write the modified lines
writeLines(lines, con = x)
x
} what does this code do:
@krlmlr thoughts? |
Thanks for your contribution, it looks very useful. A few suggestions:
Would you like to submit a PR? I'll take this when I'll next work on {here}. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A helper function that adds
here::i_am()
calls to all files in a project or to the active file.DESCRIPTION
is present, files in theR/
subdirectory should be ignoreduuid
argument withuuid::UUIDgenerate()
here::i_am()
is found, does it need to be moved to the top?The text was updated successfully, but these errors were encountered: