[R] R function, sink() and empty file

1 view
Skip to first unread message

Laura Poggio

unread,
Aug 10, 2008, 11:00:34 AM8/10/08
to r-h...@r-project.org
Dear all,
I wrote a simple script in order to put together some functions and method
to be executed on various files
I am trying to have to possibility to call the script changing few
parameters in order to use the different files.
I succeeded partly using the function method.
However in my script I call the sink() function in order to output the
results to a .txt file. When using the function the file is empty, when
running the script manually the file is fine. How can I solve that?
Is there any other way to run a R script on different files with different
names?

Thank you very much in advance

Laura

=============================
EXAMPLE OF CODE
=============================
km = function(a,o) {

fld <- system.file("G:/ALRPC/JRC/IMG/", package="rgdal")
filename = as.name(paste (a, "-", o, sep=""))
file <- read.table(paste("R_out/", filename, ".txt", sep=""))

obj <- kmenas(file, 5)

sink(paste("R_out/res", filename, ".txt", sep=""))
"Kluster centers"
obj$centers
"Within size for clusters"
obj$withinss
"Cluster size"
obj$size
sink()
}

[[alternative HTML version deleted]]

______________________________________________
R-h...@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.

Prof Brian Ripley

unread,
Aug 10, 2008, 12:00:28 PM8/10/08
to Laura Poggio, r-h...@r-project.org
This is what Rscript and R CMD BATCH are for.

Your problem is that you forgot to print the objects in your function:
auto-printing only occurs at the top level.

--
Brian D. Ripley, rip...@stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595

Reply all
Reply to author
Forward
0 new messages