Hi List, I am trying to write unsuccessfully to a logfile with cat. Here my example code: letters[1:5]->x logf<-"test.txt" cat('%%%%%%%%%%%%%%%%%%%%%%%%%%\n',file=logf) catf<-function(x,...,logfile='log.txt', append=TRUE){ cat(x,'\n', file=logfile, append=append)} testit<-function(x,...){ paste0('this is x: ',x)->y return(y) catf("++++++++++++++++++test=============",...) } sapply(x, function(x) testit(x, logfile=logf)) Any suggestions appreciated. Thanks Herry
testit<-function(x,...){ paste0('this is x: ',x)->y return(y) catf("++++++++++++++++++test=============",...) } You return from the function before calling catf(). Remove the 'return(y)' and make 'y' the last expression in the function. Bill Dunlap TIBCO Software wdunlap tibco.com On Wed, Feb 14, 2018 at 4:16 PM, <Alexander.Herr at csiro.au> wrote:> Hi List, > I am trying to write unsuccessfully to a logfile with cat. Here my example > code: > > letters[1:5]->x > logf<-"test.txt" > cat('%%%%%%%%%%%%%%%%%%%%%%%%%%\n',file=logf) > catf<-function(x,...,logfile='log.txt', append=TRUE){ cat(x,'\n', > file=logfile, append=append)} > testit<-function(x,...){ > paste0('this is x: ',x)->y > return(y) > catf("++++++++++++++++++test=============",...) > } > sapply(x, function(x) testit(x, logfile=logf)) > > Any suggestions appreciated. > > Thanks > Herry > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]
Your call to catf in testit is after the return, so it is never called. FWIW my antibugging strategy (and readability strategy) is to never use the return function... I structure my logic to end up at the end with my desired function result in a variable and I simply put that variable on the last line of the function. -- Sent from my phone. Please excuse my brevity. On February 14, 2018 4:16:19 PM PST, Alexander.Herr at csiro.au wrote:>Hi List, >I am trying to write unsuccessfully to a logfile with cat. Here my >example code: > >letters[1:5]->x >logf<-"test.txt" >cat('%%%%%%%%%%%%%%%%%%%%%%%%%%\n',file=logf) >catf<-function(x,...,logfile='log.txt', append=TRUE){ cat(x,'\n', >file=logfile, append=append)} >testit<-function(x,...){ > paste0('this is x: ',x)->y > return(y) > catf("++++++++++++++++++test=============",...) > } >sapply(x, function(x) testit(x, logfile=logf)) > >Any suggestions appreciated. > >Thanks >Herry >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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.
Not sure what you wanted to do.Note that you have "test'txt" and "log.txt" in your code. Using only "test.txt", the following worked fine for me: letters[1:5]->x logf<-"test.txt" cat('%%%%%%%%%%%%%%%%%%%%%%%%%%\n',file=logf) catf<-function(x,...,logfile='test.txt', append=TRUE){ cat(x,'\n', file=logfile, append=append)} testit<-function(x,...){ paste0('this is x: ',x)->y catf(y) } sapply(x, function(x) testit(x, logfile=logf)) If you do not see the file in your working directory (found by getwd() ), maybe you have write permissions issues. Note also that you may need none of this: see ?sink Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Wed, Feb 14, 2018 at 4:16 PM, <Alexander.Herr at csiro.au> wrote:> Hi List, > I am trying to write unsuccessfully to a logfile with cat. Here my example > code: > > letters[1:5]->x > logf<-"test.txt" > cat('%%%%%%%%%%%%%%%%%%%%%%%%%%\n',file=logf) > catf<-function(x,...,logfile='log.txt', append=TRUE){ cat(x,'\n', > file=logfile, append=append)} > testit<-function(x,...){ > paste0('this is x: ',x)->y > return(y) > catf("++++++++++++++++++test=============",...) > } > sapply(x, function(x) testit(x, logfile=logf)) > > Any suggestions appreciated. > > Thanks > Herry > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]