Dear R-help, I am running a Mac under Sierra, with R version 3.4.2 and RStudio 1.1.383. When running head () or tail () on an object in a script using source (<script name>) nothing appears in the output file, but if I use these commands in the normal R window the normal output appears. What am I doing wrong? Tom Backer Johnsen University of Bergen Norway
Hello, Try print(head(...)) Hope this helps, Rui Barradas Em 07-11-2017 20:01, Tom Backer Johnsen escreveu:> Dear R-help, > > I am running a Mac under Sierra, with R version 3.4.2 and RStudio 1.1.383. When running head () or tail () on an object in a script using source (<script name>) nothing appears in the output file, but if I use these commands in the normal R window the normal output appears. > > What am I doing wrong? > > Tom Backer Johnsen > University of Bergen > Norway > ______________________________________________ > 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. >
Either change your script by adding print(...) calls where you want to see something printed or change the call to source() by adding print.eval=TRUE or echo=TRUE. E.g.,> cat(file = tf <- tempfile(), "head(10:1)\ntail(letters)\nx <-gamma(0:4)\nx\n")> source(tf)Warning message: In gamma(0:4) : NaNs produced> source(tf, print.eval=TRUE)[1] 10 9 8 7 6 5 [1] "u" "v" "w" "x" "y" "z" [1] NaN 1 1 2 6 Warning message: In gamma(0:4) : NaNs produced> source(tf, echo=TRUE, print.eval=TRUE)> head(10:1)[1] 10 9 8 7 6 5> tail(letters)[1] "u" "v" "w" "x" "y" "z"> x <- gamma(0:4)> x[1] NaN 1 1 2 6 Warning message: In gamma(0:4) : NaNs produced>Calling options(warn=1) before calling source() will keep the warnings near the lines that caused them. Bill Dunlap TIBCO Software wdunlap tibco.com On Tue, Nov 7, 2017 at 12:01 PM, Tom Backer Johnsen <Backer at uib.no> wrote:> Dear R-help, > > I am running a Mac under Sierra, with R version 3.4.2 and RStudio > 1.1.383. When running head () or tail () on an object in a script using > source (<script name>) nothing appears in the output file, but if I use > these commands in the normal R window the normal output appears. > > What am I doing wrong? > > Tom Backer Johnsen > University of Bergen > Norway > ______________________________________________ > 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]]
On 07/11/2017 3:01 PM, Tom Backer Johnsen wrote:> Dear R-help, > > I am running a Mac under Sierra, with R version 3.4.2 and RStudio 1.1.383. When running head () or tail () on an object in a script using source (<script name>) nothing appears in the output file, but if I use these commands in the normal R window the normal output appears. > > What am I doing wrong?You aren't printing the results. source() has an argument "print.eval" which defaults to FALSE; you can change it. A common way to do that is to set echo=TRUE, but echoing the input is not necessary. So use source(filename, echo = TRUE) for a lot of output, or source(filename, print.eval = TRUE) for somewhat less. In most cases an explicit print() will be needed if the head() or tail() is in a loop or in a function. Duncan Murdoch
On 11/7/2017 12:01 PM, Tom Backer Johnsen wrote:> Dear R-help, > > I am running a Mac under Sierra, with R version 3.4.2 and RStudio 1.1.383. When running head () or tail () on an object in a script using source (<script name>) nothing appears in the output file, but if I use these commands in the normal R window the normal output appears. > > What am I doing wrong? > > Tom Backer Johnsen > University of Bergen > Norway > ______________________________________________ > 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. >I believe you need to wrap your call to head/tail in a print statement: print(head(_whatever_)) Hope this is helpful, Dan -- Daniel Nordlund Port Townsend, WA USA
Hello Thank you all for most useful responses. I was looking for answers in the wrong place, that is why I have not responded before! Tom Backer Johnsen> On 7 Nov 2017, at 21:25, Rui Barradas <ruipbarradas at sapo.pt> wrote: > > Hello, > > Try > > print(head(...)) > > Hope this helps, > > Rui Barradas > > Em 07-11-2017 20:01, Tom Backer Johnsen escreveu: >> Dear R-help, >> >> I am running a Mac under Sierra, with R version 3.4.2 and RStudio 1.1.383. When running head () or tail () on an object in a script using source (<script name>) nothing appears in the output file, but if I use these commands in the normal R window the normal output appears. >> >> What am I doing wrong? >> >> Tom Backer Johnsen >> University of Bergen >> Norway >> ______________________________________________ >> 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. >>