Neither ?sink nor ?capture.output indicates how the output file can be specified to be in a directory other than the cwd. When the cwd is ../analyses/ and I want the output to be in ../analyses/stat-summaries/ how do I write this? sink('example-output.txt') print(summary(df)) sink() writes output to the current directory. My attempts to prefix the file name with ./ or just / don't sit well with R. What is the proper syntax? TIA, Rich
On Thu, Sep 13, 2018 at 3:33 PM Rich Shepard <rshepard at appl-ecosys.com> wrote:> > Neither ?sink nor ?capture.output indicates how the output file can be > specified to be in a directory other than the cwd. > > When the cwd is ../analyses/ and I want the output to be in > ../analyses/stat-summaries/ how do I write this? > > sink('example-output.txt') > print(summary(df)) > sink() > > writes output to the current directory. My attempts to prefix the file name > with ./ or just / don't sit well with R.Hi welcome to R-help. Please help the helper(s) to help you by being as explicit as possible what you've tried (i.e. cut'n'paste your code), provide error messages (cut'n'paste) you get, if any, and/or what you mean by "don't sit well with R" (that can mean many different things). /Henrik> What is the proper syntax? > > TIA, > > Rich > > ______________________________________________ > 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.
On Thu, 13 Sep 2018, Rich Shepard wrote:> sink('example-output.txt') > print(summary(df)) > sink()Let me expand on this. When the script contains # Open PDF device to save plot pdf('../images/rainfall-estacada-se.pdf') ... plot(rain_est_se) dev.off() the file, rainfall-estacada-se.pdf is placed in the images directory, which is on the same directory level as the one in which the script is being run. I thought the equivalent syntax with sink() would work, but the print command rejects the forward slash that plot() accepts: Error in source("rainfall-dubois-crk-all.r") : rainfall-dubois-crk-all.r:25:7: unexpected '/' Is this more clear? Thanks, Rich
Remove the / from the print command, it does not belong there. sink("../directory/file.txt"); print(summary(foo)) sink(NULL) On Thu, Sep 13, 2018 at 4:03 PM Rich Shepard <rshepard at appl-ecosys.com> wrote:> On Thu, 13 Sep 2018, Rich Shepard wrote: > > > sink('example-output.txt') > > print(summary(df)) > > sink() > > Let me expand on this. When the script contains > > # Open PDF device to save plot > pdf('../images/rainfall-estacada-se.pdf') > ... > plot(rain_est_se) > dev.off() > > the file, rainfall-estacada-se.pdf is placed in the images directory, which > is on the same directory level as the one in which the script is being run. > I thought the equivalent syntax with sink() would work, but the print > command rejects the forward slash that plot() accepts: > > Error in source("rainfall-dubois-crk-all.r") : > rainfall-dubois-crk-all.r:25:7: unexpected '/' > > Is this more clear? > > Thanks, > > Rich > > ______________________________________________ > 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]]
It is not possible for the current working directory to begin with "../". That is like saying n=n-1, because once follow the two dots up to the next directory the two dots refer to the next directory up. I don't think anyone in this list understands what is going on for you, so I recommend using the reprex package to create a confirmed-reproducible example and send that along so we can identify the bug or user error that is puzzling you. On September 13, 2018 3:42:26 PM PDT, Henrik Bengtsson <henrik.bengtsson at gmail.com> wrote:>On Thu, Sep 13, 2018 at 3:33 PM Rich Shepard <rshepard at appl-ecosys.com> >wrote: >> >> Neither ?sink nor ?capture.output indicates how the output file >can be >> specified to be in a directory other than the cwd. >> >> When the cwd is ../analyses/ and I want the output to be in >> ../analyses/stat-summaries/ how do I write this? >> >> sink('example-output.txt') >> print(summary(df)) >> sink() >> >> writes output to the current directory. My attempts to prefix the >file name >> with ./ or just / don't sit well with R. > >Hi welcome to R-help. Please help the helper(s) to help you by being >as explicit as possible what you've tried (i.e. cut'n'paste your >code), provide error messages (cut'n'paste) you get, if any, and/or >what you mean by "don't sit well with R" (that can mean many different >things). > >/Henrik > >> What is the proper syntax? >> >> TIA, >> >> Rich >> >> ______________________________________________ >> 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. > >______________________________________________ >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.-- Sent from my phone. Please excuse my brevity.
On Thu, 13 Sep 2018, Rich Shepard wrote:> sink('stat-summary/example-output.txt') > print(summary(df)) > sink()My apologies to everyone for not seeing a typo further in the script. I had the path to the appropriate directory in the sink() function and the print() function had only the command as above. Except for one dataframe. In that one I had stuck a '/' in front of the summary() function and that caused the problem. I just found an R equivalent to lint to find such syntactical errors before I embarrass myself again. Has anyone used lintr from github? I will definitely start using this on my scripts. Mea culpa, Rich