Nordlund, Dan (DSHS/RDA)
2013-Feb-07 22:10 UTC
[R] FW: Sourcing my file does not print command outputs
Forgot to send to R-help From: Nordlund, Dan (DSHS/RDA) Sent: Thursday, February 07, 2013 2:09 PM To: 'James Jong' Subject: RE: [R] Sourcing my file does not print command outputs James, Your code seems to have ‘…’ sitting on a line all by itself (maybe should be at the end of the preceding comment? Anyway, when I eliminated that problem and sourced the script using the following call source(‘c:/tmp/test_source.R’, print.eval=TRUE) The summary information and dotplot printed just fine. Dan Daniel J. Nordlund Washington State Department of Social and Health Services Planning, Performance, and Accountability Research and Data Analysis Division Olympia, WA 98504-5204 From: James Jong [mailto:ribonucleico@gmail.com] Sent: Thursday, February 07, 2013 1:48 PM To: Nordlund, Dan (DSHS/RDA) Subject: Re: [R] Sourcing my file does not print command outputs Thanks Dan, Here is a minimal full example, where dotplot fails to print anything if I source the file. I have tried restarting R and directly sourcing this file without doing anything else first (my .Rprofile is empty). This is running on Linux with the latest stable version of R. =======================================#Setup rm(list = ls(all = TRUE)) set.seed(1) #Generate an input matrix with 10,000 values #2,000 rows, 5 columns X <- data.frame(replicate(5, rnorm(2000))) #Construct Y using X Y <- runif(1)*X[,1]*X[,2]^2+runif(1)*X[,3]/X[,4] #Convert Y to binary Y <- as.factor(ifelse(sign(Y)>0,'X1','X0')) #Create bootstrap samples for fitting models library(caret) print("Creating bootstrap samples") tmp <- createResample(Y,times = 25) myCtrl <- trainControl(method = "boot", index = tmp, timingSamps = 10) RFmodel <- train(X,Y,method='rf',trControl=myCtrl,tuneLength=1) NNmodel <- train(X,Y,method='nnet',trControl=myCtrl,tuneLength=3, trace = FALSE) ## GLMnet = GLMmodel, ... #Assess re-sampled (out of sample) accuracy print("Assessing re-sampled (OOB) accuracy") resamps <- resamples( list( RF = RFmodel, NN = NNmodel )) print("Summarizing resamps") summary(resamps) dotplot(resamps, metric = "Accuracy") ======================================= Thanks, James On Thu, Feb 7, 2013 at 4:37 PM, Nordlund, Dan (DSHS/RDA) <NordlDJ@dshs.wa.gov<mailto:NordlDJ@dshs.wa.gov>> wrote: I don’t know where resamps comes from and I don’t know what parameters you provided to source(), so you haven’t provided a reproducible example. But, did you call source with print.eval=TRUE ? Dan Daniel J. Nordlund Washington State Department of Social and Health Services Planning, Performance, and Accountability Research and Data Analysis Division Olympia, WA 98504-5204 From: James Jong [mailto:ribonucleico@gmail.com<mailto:ribonucleico@gmail.com>] Sent: Thursday, February 07, 2013 1:30 PM To: Nordlund, Dan (DSHS/RDA) Cc: r-help@r-project.org<mailto:r-help@r-project.org> Subject: Re: [R] Sourcing my file does not print command outputs Thanks. Interestingly I am having the same problem with dotplot(resamps, metric = "Accuracy") Nothing shows up if I source the file. But I can visualize it I call this command from the command line... Any thoughts? Thanks again, James On Thu, Feb 7, 2013 at 3:09 PM, Nordlund, Dan (DSHS/RDA) <NordlDJ@dshs.wa.gov<mailto:NordlDJ@dshs.wa.gov><mailto:NordlDJ@dshs.wa.gov<mailto:NordlDJ@dshs.wa.gov>>> wrote:> -----Original Message----- > From: r-help-bounces@r-project.org<mailto:r-help-bounces@r-project.org><mailto:r-help-bounces@r-project.org<mailto:r-help-bounces@r-project.org>> [mailto:r-help-bounces@r-<mailto:r-help-bounces@r-><mailto:r-help-bounces@r-<mailto:r-help-bounces@r->> > project.org<http://project.org><http://project.org>] On Behalf Of John Kane > Sent: Thursday, February 07, 2013 10:57 AM > To: James Jong; r-help@r-project.org<mailto:r-help@r-project.org><mailto:r-help@r-project.org<mailto:r-help@r-project.org>> > Subject: Re: [R] Sourcing my file does not print command outputs > > From ?source > "Note that running code via source differs in a few respects from > entering it at the R command line. Since expressions are not executed > at the top level, auto-printing is not done. So you will need to > include explicit print calls for things you want to be printed" > > John Kane > Kingston ON Canada > > > > -----Original Message----- > > From: ribonucleico@gmail.com<mailto:ribonucleico@gmail.com><mailto:ribonucleico@gmail.com<mailto:ribonucleico@gmail.com>> > > Sent: Thu, 7 Feb 2013 13:36:56 -0500 > > To: r-help@r-project.org<mailto:r-help@r-project.org><mailto:r-help@r-project.org<mailto:r-help@r-project.org>> > > Subject: [R] Sourcing my file does not print command outputs > > > > I looked at the documentation of source() and summary(), and I could > not > > find the reason why calling something like: > > > >> summary(resamps) > > > > from the command line, works (it prints the summary) > > > > whereas calling > > > > summary(resampls) > > > > from a file that I source with source("my_file.r") does not print > > anything. > > > > How can I get summary(resamps) to print when I source a file with > this > > command? > > > > Thanks, > > > > James > >James, You might also want to look at ?source for the echo or print.eval parameters to the source() function. Using source("my_file.r", echo=TRUE) or source("my_file.r", print.eval=TRUE) may get you what you want without adding explicit print statements to your script file. Hope this is helpful, Dan Daniel J. Nordlund Washington State Department of Social and Health Services Planning, Performance, and Accountability Research and Data Analysis Division Olympia, WA 98504-5204 ______________________________________________ R-help@r-project.org<mailto:R-help@r-project.org><mailto:R-help@r-project.org<mailto:R-help@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. [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org<mailto:R-help@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. [[alternative HTML version deleted]]