[R] function to export data

Paul Hiemstra p.hiemstra at geo.uu.nl
Mon Jun 30 11:22:27 CEST 2008


Hi Daniel,

You did not make clear why you want to export the data. If you want to 
export data to another program the usual way to export data to a text 
file is through the use of write.table(). Something along the lines of:

summary(cars) # cars is a standard R dataset
write.table(summary(cars), file = "dataexp.txt")
?write.table

If you want to store R data to read it back into R again I would 
recommend the save() option in combination with the load option. Example:

x = summary(cars)
save(x, file = "dataexp.rda") # The extension of the file is not really 
important

# Load a new R session
load("dataexp.rda") # This makes the object "x" available in the new session

cheers,

Paul

Daniel Pires wrote:
>  
>
> Hi,
>
>  
>
> I’m trying to built a simple  function that allows me to export
> automatically some results to a text file. I’ve tried the two following
> approaches but none worked. 
>
>  
>
>   
>> exportdata<-function(x) {
>>     
>
> + dataexp<-summary(x)
>
> +  export(dataexp,type="ascii",file="dataexp.txt")
>
> +  }
>
>   
>> exportdata(glm.poisson0)
>>     
>
> Error in eval(expr, envir, enclos) : object "dataexp" not found
>
>  
>
>  
>
>  
>
> exportdata2<-function(x) {
>
> sink("exportdata2.txt") 
>
> summary(x)  
>
> sink() 
>
> }
>
>  
>
>   
>> exportdata2(glm.poisson0) # the file is created but with no data in it.
>>     
>
>  
>
>  
>
> Can anyone give me some hints on what I’m missing here? I would be very
> grateful for that.
>
>  
>
> Thanks.
>
>  
>
>  
>
> Daniel Pires
>
>  
>
>  
>
> Daniel Pires
>
> Faculdade de Ciências da Universidade de Lisboa
>
> Dep. Biologia Animal
>
> Ed. C2, piso 2
>
> Campo Grande 1749-016 Lisboa
>
> Portugal
>
>  <http://ffishgul.fc.ul.pt/> http://ffishgul.fc.ul.pt/
>
>  
>
>
> 	[[alternative HTML version deleted]]
>
>   
> ------------------------------------------------------------------------
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>   


-- 
Drs. Paul Hiemstra
Department of Physical Geography
Faculty of Geosciences
University of Utrecht
Heidelberglaan 2
P.O. Box 80.115
3508 TC Utrecht
Phone: 	+31302535773
Fax:	+31302531145
http://intamap.geo.uu.nl/~paul



More information about the R-help mailing list