Marianne Promberger
2011-Feb-02 11:51 UTC
[R] pass nrow(x) to dots in function(x){plot(x,...)}
Dear Rers, I have a function to barplot() a matrix, eg myfun <- function(x, ...) { barplot(x , ... )} (The real function is more complicated, it does things to the matrix first.) So I can do: m1 <- matrix(1:20,4) myfun(m1) myfun(m1, main="My title") I'd like to be able to add the number of rows of the matrix passed to the function to the "..." argument, eg myfun(m1, main=paste("n=",ns)) where 'ns' would be nrow(m1) I've tried this but it doesn't work: myfun <- function(x, ...) { ns <- nrow(x) barplot(x , ... ) } myfun(m1, main=paste("n = ",ns) ) ns is not found So, basically, how do I assign an object inside a function that I can then access in the dots when executing the function? Many thanks Marianne -- Marianne Promberger PhD, King's College London http://promberger.info R version 2.12.0 (2010-10-15) Ubuntu 9.04
rex.dwyer at syngenta.com
2011-Feb-04 17:06 UTC
[R] pass nrow(x) to dots in function(x){plot(x,...)}
Hi Marianne, The quick-and-dirty solution is to add one character and make ns global: ns <<- nrow(x) Poor practice, but OK temporarily if you're just debugging. This is an issue of "scope". You are assuming dynamic scope, whereas R uses static scope. 'ns' was not defined when you said paste("n=",ns); it doesn't matter what its value is later. Even though R delays evaluation of the argument until it is first needed, if it ends up being evaluated, the result is the same as if you evaluated it in the environment where it appeared in the text. You can do something like this: myfun <- function(x, title.fun, ...) { ns <- nrow(x) title = title.fun(ns) barplot(x , main=title, ... ) } myfun(m1, title.fun=function(n) paste("n = ",n) ) Then the paste isn't evaluated until title.fun is *called*. If you don't want to always supply title.fun, you give a default: myfun <- function(x, title.fun=paste, ...) { ... or myfun <- function(x, title.fun=function(...){""}, ...) { ... or myfun <- function(x, title.fun=function(...){main}, main="", ...) { ... # (I think) Rex ----------------------- Message: 4 Date: Wed, 2 Feb 2011 11:51:50 +0000 From: Marianne Promberger <marianne.promberger at kcl.ac.uk> To: "r-help at r-project.org" <r-help at r-project.org> Subject: [R] pass nrow(x) to dots in function(x){plot(x,...)} Message-ID: <20110202115150.GD8598 at lauren> Content-Type: text/plain; charset=us-ascii Dear Rers, I have a function to barplot() a matrix, eg myfun <- function(x, ...) { barplot(x , ... )} (The real function is more complicated, it does things to the matrix first.) So I can do: m1 <- matrix(1:20,4) myfun(m1) myfun(m1, main="My title") I'd like to be able to add the number of rows of the matrix passed to the function to the "..." argument, eg myfun(m1, main=paste("n=",ns)) where 'ns' would be nrow(m1) I've tried this but it doesn't work: myfun <- function(x, ...) { ns <- nrow(x) barplot(x , ... ) } myfun(m1, main=paste("n = ",ns) ) ns is not found So, basically, how do I assign an object inside a function that I can then access in the dots when executing the function? Many thanks Marianne -- Marianne Promberger PhD, King's College London http://promberger.info R version 2.12.0 (2010-10-15) Ubuntu 9.04 ------------------------------ message may contain confidential information. If you are not the designated recipient, please notify the sender immediately, and delete the original and any copies. Any use of the message by you is prohibited.