Skip to content

Commit

Permalink
Version 0.0.2
Browse files Browse the repository at this point in the history
Fixes #4. Handles other types of deps. Moves comments.
  • Loading branch information
MilesMcBain committed Aug 8, 2017
1 parent aefba07 commit 14f9ecf
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
@@ -1,8 +1,8 @@
Package: packup
Title: Collect and Arrange library() Calls
Version: 0.0.1
Version: 0.0.2
Authors@R: person("Miles", "McBain", email = "miles.mcbain@gmail.com", role = c("aut", "cre"))
Description: An RStudio addin that moves all library() calls up to top of
Description: An RStudio addin that moves all library(), require(), and pload() calls up to top of
document and arrange in alphabetical order. Depending on what convention the
user has used more, all packages are homogenised to quoted or unquoted names.
The exact location differs from . R to .Rmd. In .R the list starts at document
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
@@ -0,0 +1,5 @@
# Version 0.0.2

* Now packs up `require()` and `pload()` calls.
* Comments appearing with the library invoccation are packed up with it.
* Handles libraries with `.`'s in their names.
25 changes: 21 additions & 4 deletions R/packup.R
@@ -1,5 +1,5 @@
#' Packup
#' @description Moves all library() calls up to top of document and arrange in alphabetical order.
#' @description Moves all library(), require(), and pload() calls up to top of document and arrange in alphabetical order.
#' Depending on what convention the user has used more, all packages are homogenised to quoted or unquoted names.
#' The exact location differs from .R to .Rmd. In .R the list starts at document position 0,0. In .Rmd it is the first line
#' of the first chunk.
Expand All @@ -20,11 +20,28 @@ packup <- function(){
else{
insertion_target <- rstudioapi::document_position(0,0)
}
lib_matches <- regexec(pattern = "^\\s*library\\(\"*[a-zA-Z0-9]+\\\"*)", text = doc$contents)
line_matches <- which(unlist(lib_matches) == 1)
# Filter comment lines
comments <- "^\\s*#"
comment_lines <- grepl(pattern = comments, x = doc)
doc <- doc[!comment_lines]

# Search for library matches
lib_patterns <- list(
library = "(?:^\\s*library\\s*\\(\"*[a-zA-Z0-9.]+\"*\\).*$)",
require = "(?:^\\s*require\\s*\\(\"*[a-zA-Z0-9.]+\"*\\).*$)",
p_load = "(?:^\\s*p_load\\s*\\(\"*[a-zA-Z0-9.]+\"*[\\s*\\,\\s*\"*[a-zA-Z0-9.]+\"*]*\\).*$)"
)

#lib_matches <- regexec(pattern = "^\\s*library\\(\"*[a-zA-Z0-9]+\\\"*)", text = doc$contents)
lib_matches <- regexec(pattern = paste0(lib_patterns, collapse = "|"),
text = doc$contents,
perl = TRUE)

line_matches <- which(unlist(lib_matches) >= 1)
line_lengths <- unlist(lapply(lib_matches[line_matches], attr, "match.length"))

replace_location_starts <- mapply(rstudioapi::document_position, line_matches, 1, SIMPLIFY = FALSE)
replacte_location_ends <- mapply(rstudioapi::document_position, line_matches, line_lengths + 1, SIMPLIFY = FALSE)
replacte_location_ends <- mapply(rstudioapi::document_position, line_matches , line_lengths +1, SIMPLIFY = FALSE)
replace_range_list <- mapply(rstudioapi::document_range, replace_location_starts, replacte_location_ends, SIMPLIFY = FALSE)
rstudioapi::modifyRange(location = replace_range_list, "")

Expand Down
6 changes: 4 additions & 2 deletions tests/testthat/test.Rmd
Expand Up @@ -12,8 +12,8 @@ library(tidyverse)
library(purrr)
library(ggalt)
library(ggthemes)
library(leaflet)
library(knitr)
#library(leaflet)
#library(knitr)
library(visdat)
library(readr)
library(readxl)
Expand All @@ -27,6 +27,8 @@ library(stringr)
# load the utility functions
library(smaed)
library(rprojroot)
require(datapasta)
pload("mgcv.helper")
packup::packup()
doc <- rstudioapi::getActiveDocumentContext()
```

0 comments on commit 14f9ecf

Please sign in to comment.