[R] write.xls

Spencer Graves spencer.graves at structuremonitoring.com
Mon May 21 07:07:13 CEST 2012


On 5/20/2012 5:37 PM, Gabor Grothendieck wrote:

<snip>
> Unfortunately this is getting increasingly complex due to the
> non-standard evaluation done by dataframes2xls but if you want to do
> it then this will do it.  We copy dataframes2xls to the current
> environment and reset write.xls's environment so that it finds the
> arguments.
>
>
> run<- function(x1, x2, file){
>
>     df.names<- paste0(deparse(substitute(x1)), ":::", deparse(substitute(x2)))
>
>     # copy dataframe2xls namespace contents here&  reset environment of write.xls
>     here<- environment()
>     ns<- asNamespace("dataframes2xls")
>     for(nm in ls(ns)) here[[nm]]<- ns[[nm]]
>     environment(write.xls)<- here
>
>     x23<- quote(c(x1, x2))
>     do.call("write.xls", list(x23, file, sh.names = df.names))
> }
>
> library(dataframes2xls)
>
> df1<- data.frame(c1=1:2, c2=3:4, c3=5:6)
> df2<- data.frame(c21=c(10.10101010101,20, 3), c22=c(50E50,60, 3) )
> outFile<- 'df12.xls'
>
> run(df1, df2, outFile)

Thanks very much.  I got this to work.  However, I discovered three 
other problems with write.xls:


       (1) With "\n" in a character string in the data.frame, it fails 
to write a file, noting, "Infile name is not a valid csv file" without 
throwing an error.


       (2) With "," in a character string in the data.frame, it splits 
that element over multiple columns, thereby corrupting what is written.


       (3) It insists on quoting all all character strings.  I don't 
need or want those extraneous quotes, but I don't know how to get rid of 
them.


       I've modified the code to gsub('\n|,', ' ', strings);  this 
provides an acceptable fix to the first two problems.  I plan to ignore 
the third if I can't fix it.


       Thanks again,
       Spencer



More information about the R-help mailing list