Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. # Load the relevant packages
  2. library(NCmisc)
  3. library(stringr)
  4.  
  5. # Load the script and parse lines
  6. file_name <- "example_script.R"
  7. file_lines <- readLines(file_name)
  8.  
  9. # Save the function and package names
  10. functions_list <- list.functions.in.file(file_name)
  11. names(functions_list) <- paste(str_remove(names(functions_list), "package:"), "::", sep = "")
  12.  
  13. # Initialise new file
  14. new_file <- file_lines
  15.  
  16. # Exclude lines with comments or package loads
  17. indices <- which(str_detect(new_file, "#|library"))
  18. new_file[indices] <- ""
  19.  
  20. # Create nested loop to prepend each function with package name
  21. for(i in 1:length(functions_list)) {
  22. for(j in 1:length(functions_list[[i]])) {
  23. new_file <<- str_replace_all(new_file,
  24. functions_list[[i]][j],
  25. paste(names(functions_list)[i], functions_list[[i]][j], sep = ""))
  26. }
  27. }
  28.  
  29. # Replace lines with comments or package loads
  30. new_file[indices] <- file_lines[indices]
  31.  
  32. # Overwrite the old file
  33. cat(new_file, file = file_name, sep = "\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement