Skip to content

Commit

Permalink
Merge ed469ab into 28f9ffb
Browse files Browse the repository at this point in the history
  • Loading branch information
Lindsay Carr committed Mar 21, 2017
2 parents 28f9ffb + ed469ab commit 58e3043
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 25 deletions.
26 changes: 7 additions & 19 deletions R/function_args.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,11 @@ function_args <- function(package, name, object, ..., use.default=paste0(name,'.
params <- list(...)

if (!missing(object)) {
# params <- append_params(object, params)
if (!is.null(names(object))){
params <- append(object, params)
} else {
params <- append(list(object), params)
}
params <- append_params(object, params)
} else {
object=c()
}

if (length(params) == 0)
return(list())


# // is there a method for this class?
defFun <- getS3method(name,class(object),optional=TRUE) # will be NULL when object is missing
if (is.null(defFun)){
Expand All @@ -36,7 +27,9 @@ function_args <- function(package, name, object, ..., use.default=paste0(name,'.

arg.names = names(formals(defFun))[which(!names(formals(defFun)) %in% names(params))]

if (is.null(names(params))){
# need to check length(params) > 0 because if params is an empty list,
# indexing arg.names based on length(params) will fail.
if (is.null(names(params)) & length(params) > 0){
# // all are unnamed
if (arg.names[seq_len(length(params))][1] == "..."){
# // special case where unnamed args go to ..., and should remain as characters (such as par("usr"))
Expand All @@ -47,14 +40,6 @@ function_args <- function(package, name, object, ..., use.default=paste0(name,'.
names(params)[which(names(params) == "")] <- arg.names[seq_len(sum(names(params) == ""))]
}

if(name %in% c('points', 'lines')){
if(!is.null(params[['x']]) & is.null(params[['y']])){
xy_args <- list(x = seq_along(params[['x']]),
y = params[['x']])
params <- append_replace(params, xy_args)
}
}

# // re-order
sort.i <- seq_len(length(params))
match.i <- match(names(params), names(formals(defFun)))
Expand All @@ -76,6 +61,9 @@ append_params.NULL <- function(object, params){
}

append_params.list <- function(object, params){
if(is.null(names(object))){
object <- list(object)
}
append(object, params)
}

Expand Down
7 changes: 6 additions & 1 deletion R/modify_legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,12 @@ modify_legend <- function(object, location="topright", legend_offset=0.3, draw=F
arguments <- filter_arguments("legend", ..., custom.config = object[["global"]][["config"]][["config.file"]])

arguments <- arguments$call.args$legend
arguments <- arguments[!unlist(lapply(arguments, is.null))]

null.args <- unlist(lapply(arguments, is.null))
if(!is.null(null.args)){
arguments <- arguments[!null.args]
}

legend.config <- append_replace(arguments, legend.config)
# auto is used when "legend" arg comes from "legend.name" in gsplot calls
legend.index <- ifelse("legend" %in% names(legend.config),length(grep("legend.\\d+", names(object$legend))) + 1, "auto")
Expand Down
8 changes: 8 additions & 0 deletions R/set_args.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ set_args <- function(fun.name, ..., custom.config = FALSE, package='graphics'){
config_args <- config(fun.name, custom.config = custom.config)
user_args <- function_args(name=fun.name, package=package, ...)

if(fun.name %in% c('points', 'lines')){
if(!is.null(user_args[['x']]) & is.null(user_args[['y']])){
xy_args <- list(x = seq_along(user_args[['x']]),
y = user_args[['x']])
user_args <- append_replace(user_args, xy_args)
}
}

indicesToAdd <- !(names(config_args) %in% names(user_args))
arguments <- append(user_args, config_args[indicesToAdd])
return(arguments)
Expand Down
2 changes: 1 addition & 1 deletion inst/doc/gsplotIntro.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions tests/testthat/test-arguments.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
context("lining up default arguments")
test_that("setting params with other default works as expected",{
expect_equal(gsplot:::function_args("grDevices","points", 5:10, y = NULL, 'label', use.default='xy.coords'),
list(x=1:6, y=5:10,xlab='label'))
test_that("leaving y NULL results in x as indices and y as values",{
expect_equal(gsplot:::set_args("points", 5:10, y = NULL, package="graphics"),
list(x=1:6, y=5:10, pch=6, col="red"))
})

test_that("setting params with class match works as expected",{
Expand Down
7 changes: 6 additions & 1 deletion tests/testthat/tests-points.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ test_that("graphics examples work", {

context("points arguments")
test_that("setting params works as expected",{
expect_equal(gsplot:::function_args("graphics","points", 5, y = NULL), list(x=1, y=5))
#not specifying y in points call works (passed through function_args, fixed in set_args)
expect_equal(gsplot:::function_args("graphics","points", 5, y = NULL), list(x=5, y=NULL))
expect_equal(gsplot:::set_args("points", 5, y = NULL, package="graphics"),
list(x=1, y=5, pch=6, col="red"))

#function_args gives back values of x and y unchanged
expect_equal(gsplot:::function_args("graphics","points", y=5, x=0), list(x=0, y=5))
})

Expand Down

0 comments on commit 58e3043

Please sign in to comment.