Skip to content

Commit

Permalink
replace assignment depending on codetools
Browse files Browse the repository at this point in the history
  • Loading branch information
kohske committed Jul 2, 2012
1 parent 8bf5d81 commit a2692d5
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
# faster than require() but less rigorous
has_package = function(pkg) pkg %in% .packages(TRUE)

# aux for replace `=` by `<-`
replace_assignment <- function(exp) {
library(codetools)
ret <-
lapply(as.list(exp),
function(ee) {
walkCode(e = ee,
w = makeCodeWalker(
call = function (e, w) {
ca <- walkCode(e[[1]], w)
arg <- lapply(as.list(e[-1]),
function(a) {
if (missing(a)) NA
else walkCode(a, w)
})
as.call(c(list(ca), arg))
},
leaf = function(e, w) {
if (length(e) == 0) e <- NULL
else if (is.null(e)) e <- NULL
else if (inherits(e, "srcref")) e <- NULL
else if (identical(e, as.name("="))) e <- as.name("<-")
e
}))
}
)
lapply(ret, function(r) paste(deparse(r), collapse = " "))
}

## replace inline comments to cheat R

# rules: if you do not have parser installed, a literal # must be writen in
Expand All @@ -14,8 +43,9 @@ mask.inline = function(x, replace.assign) {
x[idx] = gsub('\\{\\s*(#.*)$', p, x[idx])
}
if (replace.assign) {
e <- parse(text = paste(x, collapse = "\n"))
x <- replace_assignment(e)
warning('replace.assign=TRUE may not be reliable without the parser package!')
x = gsub('^(\\s*[[:alnum:]_\\.]+\\s*)=(\\s*[^,]+)$', '\\1 <- \\2', x)
}
return(gsub('(#[^"]*)$', ' %InLiNe_IdEnTiFiEr% "\\1"', x))
}
Expand Down

0 comments on commit a2692d5

Please sign in to comment.