Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Load the relevant packages
- library(NCmisc)
- library(stringr)
- # Load the script and parse lines
- file_name <- "example_script.R"
- file_lines <- readLines(file_name)
- # Save the function and package names
- functions_list <- list.functions.in.file(file_name)
- names(functions_list) <- paste(str_remove(names(functions_list), "package:"), "::", sep = "")
- # Initialise new file
- new_file <- file_lines
- # Exclude lines with comments or package loads
- indices <- which(str_detect(new_file, "#|library"))
- new_file[indices] <- ""
- # Create nested loop to prepend each function with package name
- for(i in 1:length(functions_list)) {
- for(j in 1:length(functions_list[[i]])) {
- new_file <<- str_replace_all(new_file,
- functions_list[[i]][j],
- paste(names(functions_list)[i], functions_list[[i]][j], sep = ""))
- }
- }
- # Replace lines with comments or package loads
- new_file[indices] <- file_lines[indices]
- # Overwrite the old file
- cat(new_file, file = file_name, sep = "\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement