sink() isn't behaving as i expect, when used inside a function, eg: x<-data.frame(F=c("O","O")) f<-"foo.txt" sink(f); format(x); sink(); # foo.txt looks great! foo<-function(x,f) { sink(f); format(x); sink(); } foo(x,f=f) # foo.txt is empty! why is this, and how can i successfully sink() within a function? my real function does some rearrangement and formatting, but the above illustrates the problem i'm encountering. thx in advance for any help! [MacOS 10.3, R 2.0.1] -- +--------------------------------------------------------------+ | Jon Stearley (505) 845-7571 (FAX 844-9297) | | Sandia National Laboratories Scalable Systems Integration | +--------------------------------------------------------------+ one more clue: library(debug); mtrace(foo); foo(x,f=f); ... # foo.txt is not empty!
On Jun 1, 2005, at 10:23 AM, Rolf Turner wrote:> > foo<-function(x,f) { sink(f); print(format(x)); sink(); }many thanks! doh ;) ?format is unclear on return value imho. -- -jon
Duncan Murdoch
2005-Jun-01 20:56 UTC
[Rd] format.data.frame (was: [R] sink() within a function?)
Jon Stearley wrote:> On Jun 1, 2005, at 11:22 AM, Duncan Murdoch wrote: > >> These functions convert their first argument to a vector (or >> array) of character strings which have a common format (as is done >> by 'print'), fulfilling 'length(format*(x, *)) == length(x)'. The >> trimming with 'trim = TRUE' is useful when the strings are to be >> used for plot 'axis' annotation. > > > i saw this but > class(x) # [1] "data.frame" > y<-format(x) > class(y) # [1] "data.frame" > confused me, let alone y<-as.character(format(x)). i'm still an R > newbie... > > >>I'll try to make it clearer. >I think you've got a right to be confused, newbie or not. format.data.frame() doesn't seem to follow the documentation, either before or after my change to the docs. The result of format(x) is not a vector or array or even a data.frame of character strings, it's a data.frame of factors. I'm not sure this is a reasonable thing to do. Does anyone else have an opinion on this? My initial feeling is that format() on a data.frame should return a data.frame of character vectors, it shouldn't convert them to factors. One should be able to expect that format(x)[1,1] gives a character value, rather than the underlying factor encoding as it does in this example: > x <- data.frame(a=rnorm(5), b=rnorm(5)) > y <- format(x, digits=3) > y a b 1 -1.007 -0.525 2 -0.570 1.128 3 0.162 1.729 4 -1.642 -0.485 5 0.381 0.621 > cat(y[1,1],"\n") 2 Duncan Murdoch