sink() output to another directory

classic Classic list List threaded Threaded
24 messages Options
12
Reply | Threaded
Open this post in threaded view
|

sink() output to another directory

Rich Shepard
   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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: sink() output to another directory

Henrik Bengtsson-5
On Thu, Sep 13, 2018 at 3:33 PM Rich Shepard <[hidden email]> 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
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: sink() output to another directory

Rich Shepard
In reply to this post by Rich Shepard
On Thu, 13 Sep 2018, Duncan Murdoch wrote:

> What did you try?  Prefixing with either ./ or / doesn't make any sense.

Duncan,

   Using linux (and perhaps other unices) ./ and / refer to the current
directory. My code, to print to the sub-directory
(../analyses/stat-summaries/) when the script is being run in ../analyses:

sink('stat-summaries/estacada-wnw-precip.txt')
print(/summary(estacada_wnw_wx))
sink()

Rich

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: sink() output to another directory

Rich Shepard
In reply to this post by Rich Shepard
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: sink() output to another directory

plangfelder
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 <[hidden email]>
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
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: sink() output to another directory

Rolf Turner
In reply to this post by Rich Shepard

On 09/14/2018 10:49 AM, Rich Shepard wrote:

> On Thu, 13 Sep 2018, Duncan Murdoch wrote:
>
>> What did you try?  Prefixing with either ./ or / doesn't make any sense.
>
> Duncan,
>
>    Using linux (and perhaps other unices) ./ and / refer to the current
> directory.

This is simply incorrect; "./" refers to the current directory but "/"
refers to the root directory.

Note that sink("./mung.txt") gives the same result as sink("mung.txt").
I.e. the "./" is redundant.

If you have a directory "gorp" in your current directory, then

     sink("gorp/mung.txt")

will put the sink() output into the file "mung.txt" in the directory "gorp".

<SNIP>

cheers,

Rolf Turner

--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: sink() output to another directory

Rich Shepard
In reply to this post by plangfelder
On Thu, 13 Sep 2018, Peter Langfelder wrote:

> Remove the / from the print command, it does not belong there.

Peter,

   So the print() function cannot accept a relative path to a different
directory for its output? This does seem to be the case:

source('rainfall-dubois-crk-all.r')
Error in source("rainfall-dubois-crk-all.r") :
   rainfall-dubois-crk-all.r:25:7: unexpected '/'
24: sink('stat-summaries/estacada-wnw-precip.txt')
25: print(/
           ^

   Then I'll print to the cwd and move the files manually afterwards.

Thanks,

Rich

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: sink() output to another directory

Rich Shepard
In reply to this post by Rolf Turner
On Fri, 14 Sep 2018, Rolf Turner wrote:

> This is simply incorrect; "./" refers to the current directory but "/" refers
> to the root directory.

Rolf,

   I was not sufficientl clear.

> Note that sink("./mung.txt") gives the same result as sink("mung.txt"). I.e.
> the "./" is redundant.
>
> If you have a directory "gorp" in your current directory, then
>
>    sink("gorp/mung.txt")
>
> will put the sink() output into the file "mung.txt" in the directory "gorp".

sink('stat-summaries/estacada-wnw-precip.txt')
print(summary(estacada_se_wx))
sink()

results in

24: sink('stat-summaries/estacada-wnw-precip.txt')
25: print(/
           ^
   Does not matter if I use single or double quotes.

Regards,

Rich

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: sink() output to another directory

R help mailing list-2
In reply to this post by Rich Shepard
In my experience, any path that can be used at the shell prompt in a unix-alike can be used anywhere that R wants a file name.

[that is, when running R on a unix-alike system, and when pwd at the shell prompt returns the same value as getwd() in R]

Hopefully, that helps...


-Don

--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
Lab cell 925-724-7509
 
 

On 9/13/18, 3:49 PM, "R-help on behalf of Rich Shepard" <[hidden email] on behalf of [hidden email]> wrote:

    On Thu, 13 Sep 2018, Duncan Murdoch wrote:
   
    > What did you try?  Prefixing with either ./ or / doesn't make any sense.
   
    Duncan,
   
       Using linux (and perhaps other unices) ./ and / refer to the current
    directory. My code, to print to the sub-directory
    (../analyses/stat-summaries/) when the script is being run in ../analyses:
   
    sink('stat-summaries/estacada-wnw-precip.txt')
    print(/summary(estacada_wnw_wx))
    sink()
   
    Rich
   
    ______________________________________________
    [hidden email] 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.
   

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: sink() output to another directory

Rich Shepard
On Thu, 13 Sep 2018, MacQueen, Don wrote:

> In my experience, any path that can be used at the shell prompt in a
> unix-alike can be used anywhere that R wants a file name.

Don,

   That's been my experiences, too.

> Hopefully, that helps...

   That's why I don't understand why the plot() function accepts the
different directory while the sink() function (here) doesn't.

   I showed R rejecting:

sink('stat-summaries/estacada-se-precip.txt')
print(summary(estacada_se_wx))
sink()

while accepting:

pdf('../images/rainfall-estacada-se.pdf')
  <snip xyplot() function>
plot(rain_est_se)
dev.off()

   Changing the sink() file to
'./stat-summaries/estacada-se-precip.txt'

generates the same error while I regularly use this syntax to copy files or
specify the relative path to an executable file.

Regards,

Rich

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: sink() output to another directory

Henrik Bengtsson-5
On Thu, Sep 13, 2018 at 6:05 PM Rich Shepard <[hidden email]> wrote:

>
> On Thu, 13 Sep 2018, MacQueen, Don wrote:
>
> > In my experience, any path that can be used at the shell prompt in a
> > unix-alike can be used anywhere that R wants a file name.
>
> Don,
>
>    That's been my experiences, too.
>
> > Hopefully, that helps...
>
>    That's why I don't understand why the plot() function accepts the
> different directory while the sink() function (here) doesn't.
>
>    I showed R rejecting:
>
> sink('stat-summaries/estacada-se-precip.txt')
> print(summary(estacada_se_wx))
> sink()
>
> while accepting:
>
> pdf('../images/rainfall-estacada-se.pdf')
>   <snip xyplot() function>
> plot(rain_est_se)
> dev.off()
>
>    Changing the sink() file to
> './stat-summaries/estacada-se-precip.txt'
>
> generates the same error

"same error" as what? (ambiguity is the reason for not being able to
help you - all the replies in this thread this far are correct and on
the spot)

BTW, not that it should matter, what is your operating system and version of R?

/Henrik

> while I regularly use this syntax to copy files or
> specify the relative path to an executable file.
>
> Regards,
>
> Rich
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: sink() output to another directory

Rich Shepard
On Thu, 13 Sep 2018, Henrik Bengtsson wrote:

>> sink('stat-summaries/estacada-se-precip.txt')
>> print(summary(estacada_se_wx))
>> sink()
>>
>> while accepting:
>>
>> pdf('../images/rainfall-estacada-se.pdf')
>>   <snip xyplot() function>
>> plot(rain_est_se)
>> dev.off()
>>
>>    Changing the sink() file to
>> './stat-summaries/estacada-se-precip.txt'
>>
>> generates the same error
>
> "same error" as what? (ambiguity is the reason for not being able to
> help you - all the replies in this thread this far are correct and on
> the spot)
>
> BTW, not that it should matter, what is your operating system and version of R?

Henrik,

   As I wrote in earlier messages:

sink('stat-summaries/estacada-wnw-precip.txt')
print(summary(estacada_se_wx))
sink()

results in

24: sink('stat-summaries/estacada-wnw-precip.txt')
25: print(/
            ^
    Does not matter if I use single or double quotes.

   The message that print() doesn't like the forward slash results when I
specify 'stat-summaries/estacada-wnw-precip.txt' or
'./stat-summaries/estacada-wnw-precip.txt'.

   Running R-3.5.1 on Slackware-14.2.

Rich

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: sink() output to another directory

Bert Gunter-2
I find your "explanation" confusing. You appear to be misusing
print(). Please read ?print carefully. You print objects in R, not
files. Objects in R do not have "/" in their names (without some
trickery). See ?make.names .

-- 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 Thu, Sep 13, 2018 at 7:12 PM Rich Shepard <[hidden email]> wrote:

>
> On Thu, 13 Sep 2018, Henrik Bengtsson wrote:
>
> >> sink('stat-summaries/estacada-se-precip.txt')
> >> print(summary(estacada_se_wx))
> >> sink()
> >>
> >> while accepting:
> >>
> >> pdf('../images/rainfall-estacada-se.pdf')
> >>   <snip xyplot() function>
> >> plot(rain_est_se)
> >> dev.off()
> >>
> >>    Changing the sink() file to
> >> './stat-summaries/estacada-se-precip.txt'
> >>
> >> generates the same error
> >
> > "same error" as what? (ambiguity is the reason for not being able to
> > help you - all the replies in this thread this far are correct and on
> > the spot)
> >
> > BTW, not that it should matter, what is your operating system and version of R?
>
> Henrik,
>
>    As I wrote in earlier messages:
>
> sink('stat-summaries/estacada-wnw-precip.txt')
> print(summary(estacada_se_wx))
> sink()
>
> results in
>
> 24: sink('stat-summaries/estacada-wnw-precip.txt')
> 25: print(/
>             ^
>     Does not matter if I use single or double quotes.
>
>    The message that print() doesn't like the forward slash results when I
> specify 'stat-summaries/estacada-wnw-precip.txt' or
> './stat-summaries/estacada-wnw-precip.txt'.
>
>    Running R-3.5.1 on Slackware-14.2.
>
> Rich
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: sink() output to another directory

Rolf Turner
In reply to this post by Rich Shepard
On 09/14/2018 02:12 PM, Rich Shepard wrote:

> On Thu, 13 Sep 2018, Henrik Bengtsson wrote:
>
>>> sink('stat-summaries/estacada-se-precip.txt')
>>> print(summary(estacada_se_wx))
>>> sink()
>>>
>>> while accepting:
>>>
>>> pdf('../images/rainfall-estacada-se.pdf')
>>>   <snip xyplot() function>
>>> plot(rain_est_se)
>>> dev.off()
>>>
>>>    Changing the sink() file to
>>> './stat-summaries/estacada-se-precip.txt'
>>>
>>> generates the same error
>>
>> "same error" as what? (ambiguity is the reason for not being able to
>> help you - all the replies in this thread this far are correct and on
>> the spot)
>>
>> BTW, not that it should matter, what is your operating system and
>> version of R?
>
> Henrik,
>
>    As I wrote in earlier messages:
>
> sink('stat-summaries/estacada-wnw-precip.txt')
> print(summary(estacada_se_wx))
> sink()
>
> results in
>
> 24: sink('stat-summaries/estacada-wnw-precip.txt')
> 25: print(/
>             ^
>     Does not matter if I use single or double quotes.
>
>    The message that print() doesn't like the forward slash results when I
> specify 'stat-summaries/estacada-wnw-precip.txt' or
> './stat-summaries/estacada-wnw-precip.txt'.
>
>    Running R-3.5.1 on Slackware-14.2.

This would appear to have nothing to do with sink().  There is something
weird about your data set estacada_se_wx or the summary thereof that
print() doesn't like.

If I do:

> system("mkdir stat-summaries")
> estacada_se_wx <- rnorm(10)
> sink('stat-summaries/estacada-se-precip.txt')
> print(summary(estacada_se_wx))
> sink()
> system("cat stat-summaries/estacada-se-precip.txt")

I get:

>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
> -1.7292 -0.7621  0.5428  0.2808  1.2603  1.7702

OMMMMMMM!!!

cheers,

Rolf Turner

--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: sink() output to another directory

plangfelder
In reply to this post by Rich Shepard
For the second time: Rich, there should be no slash in the print() command.

Use the form

sink("../directory/file")
print(summary(foo)) ### no slashes here
sink(NULL)

Peter

On Thu, Sep 13, 2018 at 7:12 PM Rich Shepard <[hidden email]>
wrote:

> On Thu, 13 Sep 2018, Henrik Bengtsson wrote:
>
> >> sink('stat-summaries/estacada-se-precip.txt')
> >> print(summary(estacada_se_wx))
> >> sink()
> >>
> >> while accepting:
> >>
> >> pdf('../images/rainfall-estacada-se.pdf')
> >>   <snip xyplot() function>
> >> plot(rain_est_se)
> >> dev.off()
> >>
> >>    Changing the sink() file to
> >> './stat-summaries/estacada-se-precip.txt'
> >>
> >> generates the same error
> >
> > "same error" as what? (ambiguity is the reason for not being able to
> > help you - all the replies in this thread this far are correct and on
> > the spot)
> >
> > BTW, not that it should matter, what is your operating system and
> version of R?
>
> Henrik,
>
>    As I wrote in earlier messages:
>
> sink('stat-summaries/estacada-wnw-precip.txt')
> print(summary(estacada_se_wx))
> sink()
>
> results in
>
> 24: sink('stat-summaries/estacada-wnw-precip.txt')
> 25: print(/
>             ^
>     Does not matter if I use single or double quotes.
>
>    The message that print() doesn't like the forward slash results when I
> specify 'stat-summaries/estacada-wnw-precip.txt' or
> './stat-summaries/estacada-wnw-precip.txt'.
>
>    Running R-3.5.1 on Slackware-14.2.
>
> Rich
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: sink() output to another directory

plangfelder
In reply to this post by Rich Shepard
There is no path in print. The path (file) is set in sink().

Peter

On Thu, Sep 13, 2018 at 4:35 PM Rich Shepard <[hidden email]>
wrote:

> On Thu, 13 Sep 2018, Peter Langfelder wrote:
>
> > Remove the / from the print command, it does not belong there.
>
> Peter,
>
>    So the print() function cannot accept a relative path to a different
> directory for its output? This does seem to be the case:
>
> source('rainfall-dubois-crk-all.r')
> Error in source("rainfall-dubois-crk-all.r") :
>    rainfall-dubois-crk-all.r:25:7: unexpected '/'
> 24: sink('stat-summaries/estacada-wnw-precip.txt')
> 25: print(/
>            ^
>
>    Then I'll print to the cwd and move the files manually afterwards.
>
> Thanks,
>
> Rich
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: sink() output to another directory

Jeff Newmiller
In reply to this post by Henrik Bengtsson-5
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 <[hidden email]> wrote:

>On Thu, Sep 13, 2018 at 3:33 PM Rich Shepard <[hidden email]>
>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
>>
>> ______________________________________________
>> [hidden email] 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.
>
>______________________________________________
>[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: sink() output to another directory

plangfelder
In reply to this post by Rich Shepard
Apologies if my advice wasn't clear: the file you want to write to goes in
the sink() function/command. You can put the file anywhere on your file
system, no need to write into current directory and then move the file.

The print command is completely unaware of the file you point to in sink().
Technically, print() sends output to a device called "standard output"
which is usually screen, but it can be changed to a file (_any_ writable
file) using the sink() command.

Hope this helps,

Peter

On Thu, Sep 13, 2018 at 4:35 PM Rich Shepard <[hidden email]>
wrote:

> On Thu, 13 Sep 2018, Peter Langfelder wrote:
>
> > Remove the / from the print command, it does not belong there.
>
> Peter,
>
>    So the print() function cannot accept a relative path to a different
> directory for its output? This does seem to be the case:
>
> source('rainfall-dubois-crk-all.r')
> Error in source("rainfall-dubois-crk-all.r") :
>    rainfall-dubois-crk-all.r:25:7: unexpected '/'
> 24: sink('stat-summaries/estacada-wnw-precip.txt')
> 25: print(/
>            ^
>
>    Then I'll print to the cwd and move the files manually afterwards.
>
> Thanks,
>
> Rich
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: sink() output to another directory

Henrik Bengtsson-5
In reply to this post by Rich Shepard
On Thu, Sep 13, 2018 at 7:12 PM Rich Shepard <[hidden email]> wrote:

>
> On Thu, 13 Sep 2018, Henrik Bengtsson wrote:
>
> >> sink('stat-summaries/estacada-se-precip.txt')
> >> print(summary(estacada_se_wx))
> >> sink()
> >>
> >> while accepting:
> >>
> >> pdf('../images/rainfall-estacada-se.pdf')
> >>   <snip xyplot() function>
> >> plot(rain_est_se)
> >> dev.off()
> >>
> >>    Changing the sink() file to
> >> './stat-summaries/estacada-se-precip.txt'
> >>
> >> generates the same error
> >
> > "same error" as what? (ambiguity is the reason for not being able to
> > help you - all the replies in this thread this far are correct and on
> > the spot)
> >
> > BTW, not that it should matter, what is your operating system and version of R?
>
> Henrik,
>
>    As I wrote in earlier messages:
>
> sink('stat-summaries/estacada-wnw-precip.txt')
> print(summary(estacada_se_wx))
> sink()
>
> results in
>
> 24: sink('stat-summaries/estacada-wnw-precip.txt')
> 25: print(/
>             ^
>     Does not matter if I use single or double quotes.
>
>    The message that print() doesn't like the forward slash results when I
> specify 'stat-summaries/estacada-wnw-precip.txt' or
> './stat-summaries/estacada-wnw-precip.txt'.

Since it is impossible to get that error message (which is a syntax
error, i.e. the R parser does not accept the code as written and it
never gets to the point where the R engine even runs your code) for
the code you are showing, I strongly suspect that you didn't source()
the same file that you were editing (the one that contains the
three-line code you are displaying above).

I see from one of your later message that you've got it to work now.

/Henrik

>
>    Running R-3.5.1 on Slackware-14.2.
>
> Rich
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: sink() output to another directory

Rich Shepard
In reply to this post by Bert Gunter-2
On Thu, 13 Sep 2018, Bert Gunter wrote:

> I find your "explanation" confusing. You appear to be misusing print().
> Please read ?print carefully. You print objects in R, not files. Objects
> in R do not have "/" in their names (without some trickery). See
> ?make.names .

Bert,

   I had read both ?print and ?print.default looking for information about
placing the printed object in another directory, and found nothing.

   My initial assumption was that print() worked similar to plot(). I use
plot() after specifying a pdf file as output and thought that sink() (which
?sink tells me diverts R output to a connection (and stops it as dev.off
stops writing to the pdf file). That sink() apparently does not work the
same way is why I posted my question.

Regards,

Rich

______________________________________________
[hidden email] 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.
12