Re: [R-pkg-devel] How to write example results to a tempdir()?

2018-04-26 Thread Henrik Bengtsson
For the information: there were various ideas discussed around this in
R-devel thread 'Base R examples that write to current working
directory' started on 2018-03-29
(https://stat.ethz.ch/pipermail/r-devel/2018-March/075779.html).

On Thu, Apr 26, 2018 at 10:31 AM, Jennifer Bryan
 wrote:
> One downside of all of this is that examples become increasingly dominated
> by code re: tempdir setup and the like.
>
> It may not discourage the people on this list, but I suspect too much extra
> machinery makes the typical user less likely to engage with examples.
>
> On Thu, Apr 26, 2018 at 10:04 AM, Henrik Bengtsson
>  wrote:
>>
>> On Thu, Apr 26, 2018 at 6:28 AM, Spencer Graves
>>  wrote:
>> >
>> >
>> > On 2018-04-26 07:11, Jose A Guijarro wrote:
>> >>
>> >> El 25/04/18 a las 20:21, Duncan Murdoch escribió:
>> >>>
>> >>> On 25/04/2018 1:32 PM, Sarah Goslee wrote:
>> 
>>  Don't change the working directory! That has all kinds of unpleasant
>>  side effects for the unsuspecting user, possibly even more so than
>>  writing to a file.
>> 
>>  Instead, write the file to the temp directory, and read it from
>>  there,
>>  with e.g.
>> 
>>  wd <- tempdir()
>>  write(dat, file.path(wd, 'Ttest_1981-2000.dat'))
>> 
>>  Using file.path() means that the appropriate path delimiter for that
>>  OS will be used.
>> >>>
>> >>>
>> >>> That's one good way to do it.  But it is possible to change directory
>> >>> and
>> >>> change back at the end of the example.  For example,
>> >>>
>> >>> wd <- tempdir()
>> >>> savedir <- setwd(wd)
>> >>>
>> >>> ... # the original code that writes and reads in the current dir
>> >>>
>> >>> setwd(savedir)
>> >>>
>> >>> There's a worry that an error in the middle of the code will leave the
>> >>> user in the wrong place.  If that's really unlikely to happen, then
>> >>> this
>> >>> code is a little simpler than Sarah's suggestion.
>> >>>
>> >>> If it is likely, you can use tryCatch(..., finally = setwd(savedir)),
>> >>> but
>> >>> I think Sarah's solution would be preferable in most cases:  many
>> >>> users will
>> >>> not understand what tryCatch() does.
>> >
>> >
>> >
>> > Hi, Duncan, et al.:
>> >
>> >
>> >   Under what circumstances should one also use "on.exit":
>> >
>> >
>> > wd <- tempdir()
>> > savedir <- setwd(wd)
>> > on.exit(setwd(savedir))?
>> >
>> >
>> >   Even senior R programmers may miss a failure mode.  In a function,
>> > this works well:  I don't need to remember to "setwd(savedir)" later in
>> > the
>> > code, which could be a problem if I have multiple exit points.  I don't
>> > know
>> > how it would work in a vignette or the examples section of a *.Rd file.
>> > tryCatch(..., finally = setwd(savedir)) condenses this into one line ...
>> > and
>> > is too terse for me in many cases.
>>
>> FWIW, it can also be used in a local() call, e.g.
>>
>> local({
>>   wd <- tempdir()
>>   savedir <- setwd(wd)
>>   on.exit(setwd(savedir))
>>   [...]
>> })
>>
>> /Henrik
>>
>> >
>> >
>> >   Spencer Graves
>> >
>> >
>> >>>
>> >>> Duncan Murdoch
>> >>
>> >>
>> >> Many thanks for the suggestion! In this way I only need to apply slight
>> >> changes in the examples and can avoid modifying all read/writes along
>> >> the
>> >> package. I will try this approach and see if it passes the CRAN checks.
>> >>
>> >> Jose
>> >>
>> >>>
>> 
>>  Sarah
>> 
>> 
>>  On Wed, Apr 25, 2018 at 12:30 PM, Jose A Guijarro
>>  
>>  wrote:
>> >
>> > Dear all,
>> >
>> > I am struggling to update my package climatol from version 3.0 to
>> > 3.1.
>> > The old version had all examples under a "dontrun" section because
>> > they
>> > needed files created by other examples that the user had to run
>> > first.
>> >
>> > As this is not acceptable anymore, I made the examples runnable and
>> > prepared small ad-hoc datasets, but then writing files to the user
>> > space
>> > is against the CRAN policy rules, and I was suggested to run them on
>> > a
>> > temporal directory. Therefore I changed all my examples to
>> > read/write
>> > files to a tempdir(), as in:
>> >
>> > \examples{
>> > #Set a temporal working directory and write input files:
>> > wd <- tempdir()
>> > setwd(wd)
>> > data(Ttest) #(This loads matrix 'dat' and data.frame 'est.c')
>> > write(dat,'Ttest_1981-2000.dat')
>> >
>> >
>> > write.table(est.c,'Ttest_1981-2000.est',row.names=FALSE,col.names=FALSE)
>> > rm(dat,est.c) #remove loaded data from memory space
>> > #Now run the example:
>> > dd2m('Ttest',1981,2000)
>> > #Input and output files can be found in directory:
>> > print(wd)
>> > }
>> >
>> > But now CRAN checks return this warning ten times (one for every
>> > example
>> > in the package):
>> >
>> 

Re: [R-pkg-devel] How to write example results to a tempdir()?

2018-04-26 Thread Jennifer Bryan
One downside of all of this is that examples become increasingly dominated
by code re: tempdir setup and the like.

It may not discourage the people on this list, but I suspect too much extra
machinery makes the typical user less likely to engage with examples.

On Thu, Apr 26, 2018 at 10:04 AM, Henrik Bengtsson <
henrik.bengts...@gmail.com> wrote:

> On Thu, Apr 26, 2018 at 6:28 AM, Spencer Graves
>  wrote:
> >
> >
> > On 2018-04-26 07:11, Jose A Guijarro wrote:
> >>
> >> El 25/04/18 a las 20:21, Duncan Murdoch escribió:
> >>>
> >>> On 25/04/2018 1:32 PM, Sarah Goslee wrote:
> 
>  Don't change the working directory! That has all kinds of unpleasant
>  side effects for the unsuspecting user, possibly even more so than
>  writing to a file.
> 
>  Instead, write the file to the temp directory, and read it from there,
>  with e.g.
> 
>  wd <- tempdir()
>  write(dat, file.path(wd, 'Ttest_1981-2000.dat'))
> 
>  Using file.path() means that the appropriate path delimiter for that
>  OS will be used.
> >>>
> >>>
> >>> That's one good way to do it.  But it is possible to change directory
> and
> >>> change back at the end of the example.  For example,
> >>>
> >>> wd <- tempdir()
> >>> savedir <- setwd(wd)
> >>>
> >>> ... # the original code that writes and reads in the current dir
> >>>
> >>> setwd(savedir)
> >>>
> >>> There's a worry that an error in the middle of the code will leave the
> >>> user in the wrong place.  If that's really unlikely to happen, then
> this
> >>> code is a little simpler than Sarah's suggestion.
> >>>
> >>> If it is likely, you can use tryCatch(..., finally = setwd(savedir)),
> but
> >>> I think Sarah's solution would be preferable in most cases:  many
> users will
> >>> not understand what tryCatch() does.
> >
> >
> >
> > Hi, Duncan, et al.:
> >
> >
> >   Under what circumstances should one also use "on.exit":
> >
> >
> > wd <- tempdir()
> > savedir <- setwd(wd)
> > on.exit(setwd(savedir))?
> >
> >
> >   Even senior R programmers may miss a failure mode.  In a function,
> > this works well:  I don't need to remember to "setwd(savedir)" later in
> the
> > code, which could be a problem if I have multiple exit points.  I don't
> know
> > how it would work in a vignette or the examples section of a *.Rd file.
> > tryCatch(..., finally = setwd(savedir)) condenses this into one line ...
> and
> > is too terse for me in many cases.
>
> FWIW, it can also be used in a local() call, e.g.
>
> local({
>   wd <- tempdir()
>   savedir <- setwd(wd)
>   on.exit(setwd(savedir))
>   [...]
> })
>
> /Henrik
>
> >
> >
> >   Spencer Graves
> >
> >
> >>>
> >>> Duncan Murdoch
> >>
> >>
> >> Many thanks for the suggestion! In this way I only need to apply slight
> >> changes in the examples and can avoid modifying all read/writes along
> the
> >> package. I will try this approach and see if it passes the CRAN checks.
> >>
> >> Jose
> >>
> >>>
> 
>  Sarah
> 
> 
>  On Wed, Apr 25, 2018 at 12:30 PM, Jose A Guijarro <
> jguijar...@aemet.es>
>  wrote:
> >
> > Dear all,
> >
> > I am struggling to update my package climatol from version 3.0 to
> 3.1.
> > The old version had all examples under a "dontrun" section because
> they
> > needed files created by other examples that the user had to run
> first.
> >
> > As this is not acceptable anymore, I made the examples runnable and
> > prepared small ad-hoc datasets, but then writing files to the user
> > space
> > is against the CRAN policy rules, and I was suggested to run them on
> a
> > temporal directory. Therefore I changed all my examples to read/write
> > files to a tempdir(), as in:
> >
> > \examples{
> > #Set a temporal working directory and write input files:
> > wd <- tempdir()
> > setwd(wd)
> > data(Ttest) #(This loads matrix 'dat' and data.frame 'est.c')
> > write(dat,'Ttest_1981-2000.dat')
> >
> > write.table(est.c,'Ttest_1981-2000.est',row.names=FALSE,col.
> names=FALSE)
> > rm(dat,est.c) #remove loaded data from memory space
> > #Now run the example:
> > dd2m('Ttest',1981,2000)
> > #Input and output files can be found in directory:
> > print(wd)
> > }
> >
> > But now CRAN checks return this warning ten times (one for every
> > example
> > in the package):
> >
> > Warning: working directory was changed to ‘/tmp/RtmpWSRK2F’,
> resetting
> >
> > Any hint on how to solve the problem will be highly appreciated...
> >
> > Jose
> >
> 
> 
> >>>
> >>
> >> __
> >> R-package-devel@r-project.org mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-package-devel
> >
> >
> > __
> > R-package-devel@r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-package-devel
>
> 

Re: [R-pkg-devel] How to write example results to a tempdir()?

2018-04-26 Thread William Dunlap
The withr package has a bunch of simple functions executing code in various
contexts, e.g., in a given directory or with given options() settings:

> withr::with_dir
function (new, code)
{
old <- setwd(dir = new)
on.exit(setwd(old))
force(code)
}

> withr::with_dir(tempdir(), getwd())
[1] "/tmp/RtmpvClVBM"

Perhaps some could be moved into core R since they are good for running
examples
and tests.



Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Thu, Apr 26, 2018 at 10:20 AM, Duncan Murdoch 
wrote:

> On 26/04/2018 1:04 PM, Henrik Bengtsson wrote:
>
>> On Thu, Apr 26, 2018 at 6:28 AM, Spencer Graves
>>  wrote:
>>
>>>
>>>
>>> On 2018-04-26 07:11, Jose A Guijarro wrote:
>>>

 El 25/04/18 a las 20:21, Duncan Murdoch escribió:

>
> On 25/04/2018 1:32 PM, Sarah Goslee wrote:
>
>>
>> Don't change the working directory! That has all kinds of unpleasant
>> side effects for the unsuspecting user, possibly even more so than
>> writing to a file.
>>
>> Instead, write the file to the temp directory, and read it from there,
>> with e.g.
>>
>> wd <- tempdir()
>> write(dat, file.path(wd, 'Ttest_1981-2000.dat'))
>>
>> Using file.path() means that the appropriate path delimiter for that
>> OS will be used.
>>
>
>
> That's one good way to do it.  But it is possible to change directory
> and
> change back at the end of the example.  For example,
>
> wd <- tempdir()
> savedir <- setwd(wd)
>
> ... # the original code that writes and reads in the current dir
>
> setwd(savedir)
>
> There's a worry that an error in the middle of the code will leave the
> user in the wrong place.  If that's really unlikely to happen, then
> this
> code is a little simpler than Sarah's suggestion.
>
> If it is likely, you can use tryCatch(..., finally = setwd(savedir)),
> but
> I think Sarah's solution would be preferable in most cases:  many
> users will
> not understand what tryCatch() does.
>

>>>
>>>
>>> Hi, Duncan, et al.:
>>>
>>>
>>>Under what circumstances should one also use "on.exit":
>>>
>>>
>>> wd <- tempdir()
>>> savedir <- setwd(wd)
>>> on.exit(setwd(savedir))?
>>>
>>>
>>>Even senior R programmers may miss a failure mode.  In a function,
>>> this works well:  I don't need to remember to "setwd(savedir)" later in
>>> the
>>> code, which could be a problem if I have multiple exit points.  I don't
>>> know
>>> how it would work in a vignette or the examples section of a *.Rd file.
>>> tryCatch(..., finally = setwd(savedir)) condenses this into one line ...
>>> and
>>> is too terse for me in many cases.
>>>
>>
>> FWIW, it can also be used in a local() call, e.g.
>>
>> local({
>>wd <- tempdir()
>>savedir <- setwd(wd)
>>on.exit(setwd(savedir))
>>[...]
>> })
>>
>
> Thanks for pointing that out.  That also has the advantage that it doesn't
> wipe out variables named "wd" and "savedir".
>
> On the other hand, it has the disadvantage that it doesn't leave wd and
> savedir behind for the user to examine, and cut and paste of the central
> executable lines won't work, you need to cut the whole local() call.
>
> Duncan Murdoch
>
>
> __
> R-package-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel
>

[[alternative HTML version deleted]]

__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel


Re: [R-pkg-devel] How to write example results to a tempdir()?

2018-04-26 Thread Duncan Murdoch

On 26/04/2018 1:04 PM, Henrik Bengtsson wrote:

On Thu, Apr 26, 2018 at 6:28 AM, Spencer Graves
 wrote:



On 2018-04-26 07:11, Jose A Guijarro wrote:


El 25/04/18 a las 20:21, Duncan Murdoch escribió:


On 25/04/2018 1:32 PM, Sarah Goslee wrote:


Don't change the working directory! That has all kinds of unpleasant
side effects for the unsuspecting user, possibly even more so than
writing to a file.

Instead, write the file to the temp directory, and read it from there,
with e.g.

wd <- tempdir()
write(dat, file.path(wd, 'Ttest_1981-2000.dat'))

Using file.path() means that the appropriate path delimiter for that
OS will be used.



That's one good way to do it.  But it is possible to change directory and
change back at the end of the example.  For example,

wd <- tempdir()
savedir <- setwd(wd)

... # the original code that writes and reads in the current dir

setwd(savedir)

There's a worry that an error in the middle of the code will leave the
user in the wrong place.  If that's really unlikely to happen, then this
code is a little simpler than Sarah's suggestion.

If it is likely, you can use tryCatch(..., finally = setwd(savedir)), but
I think Sarah's solution would be preferable in most cases:  many users will
not understand what tryCatch() does.




Hi, Duncan, et al.:


   Under what circumstances should one also use "on.exit":


wd <- tempdir()
savedir <- setwd(wd)
on.exit(setwd(savedir))?


   Even senior R programmers may miss a failure mode.  In a function,
this works well:  I don't need to remember to "setwd(savedir)" later in the
code, which could be a problem if I have multiple exit points.  I don't know
how it would work in a vignette or the examples section of a *.Rd file.
tryCatch(..., finally = setwd(savedir)) condenses this into one line ... and
is too terse for me in many cases.


FWIW, it can also be used in a local() call, e.g.

local({
   wd <- tempdir()
   savedir <- setwd(wd)
   on.exit(setwd(savedir))
   [...]
})


Thanks for pointing that out.  That also has the advantage that it 
doesn't wipe out variables named "wd" and "savedir".


On the other hand, it has the disadvantage that it doesn't leave wd and 
savedir behind for the user to examine, and cut and paste of the central 
executable lines won't work, you need to cut the whole local() call.


Duncan Murdoch

__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel


Re: [R-pkg-devel] How to write example results to a tempdir()?

2018-04-26 Thread Henrik Bengtsson
On Thu, Apr 26, 2018 at 6:28 AM, Spencer Graves
 wrote:
>
>
> On 2018-04-26 07:11, Jose A Guijarro wrote:
>>
>> El 25/04/18 a las 20:21, Duncan Murdoch escribió:
>>>
>>> On 25/04/2018 1:32 PM, Sarah Goslee wrote:

 Don't change the working directory! That has all kinds of unpleasant
 side effects for the unsuspecting user, possibly even more so than
 writing to a file.

 Instead, write the file to the temp directory, and read it from there,
 with e.g.

 wd <- tempdir()
 write(dat, file.path(wd, 'Ttest_1981-2000.dat'))

 Using file.path() means that the appropriate path delimiter for that
 OS will be used.
>>>
>>>
>>> That's one good way to do it.  But it is possible to change directory and
>>> change back at the end of the example.  For example,
>>>
>>> wd <- tempdir()
>>> savedir <- setwd(wd)
>>>
>>> ... # the original code that writes and reads in the current dir
>>>
>>> setwd(savedir)
>>>
>>> There's a worry that an error in the middle of the code will leave the
>>> user in the wrong place.  If that's really unlikely to happen, then this
>>> code is a little simpler than Sarah's suggestion.
>>>
>>> If it is likely, you can use tryCatch(..., finally = setwd(savedir)), but
>>> I think Sarah's solution would be preferable in most cases:  many users will
>>> not understand what tryCatch() does.
>
>
>
> Hi, Duncan, et al.:
>
>
>   Under what circumstances should one also use "on.exit":
>
>
> wd <- tempdir()
> savedir <- setwd(wd)
> on.exit(setwd(savedir))?
>
>
>   Even senior R programmers may miss a failure mode.  In a function,
> this works well:  I don't need to remember to "setwd(savedir)" later in the
> code, which could be a problem if I have multiple exit points.  I don't know
> how it would work in a vignette or the examples section of a *.Rd file.
> tryCatch(..., finally = setwd(savedir)) condenses this into one line ... and
> is too terse for me in many cases.

FWIW, it can also be used in a local() call, e.g.

local({
  wd <- tempdir()
  savedir <- setwd(wd)
  on.exit(setwd(savedir))
  [...]
})

/Henrik

>
>
>   Spencer Graves
>
>
>>>
>>> Duncan Murdoch
>>
>>
>> Many thanks for the suggestion! In this way I only need to apply slight
>> changes in the examples and can avoid modifying all read/writes along the
>> package. I will try this approach and see if it passes the CRAN checks.
>>
>> Jose
>>
>>>

 Sarah


 On Wed, Apr 25, 2018 at 12:30 PM, Jose A Guijarro 
 wrote:
>
> Dear all,
>
> I am struggling to update my package climatol from version 3.0 to 3.1.
> The old version had all examples under a "dontrun" section because they
> needed files created by other examples that the user had to run first.
>
> As this is not acceptable anymore, I made the examples runnable and
> prepared small ad-hoc datasets, but then writing files to the user
> space
> is against the CRAN policy rules, and I was suggested to run them on a
> temporal directory. Therefore I changed all my examples to read/write
> files to a tempdir(), as in:
>
> \examples{
> #Set a temporal working directory and write input files:
> wd <- tempdir()
> setwd(wd)
> data(Ttest) #(This loads matrix 'dat' and data.frame 'est.c')
> write(dat,'Ttest_1981-2000.dat')
>
> write.table(est.c,'Ttest_1981-2000.est',row.names=FALSE,col.names=FALSE)
> rm(dat,est.c) #remove loaded data from memory space
> #Now run the example:
> dd2m('Ttest',1981,2000)
> #Input and output files can be found in directory:
> print(wd)
> }
>
> But now CRAN checks return this warning ten times (one for every
> example
> in the package):
>
> Warning: working directory was changed to ‘/tmp/RtmpWSRK2F’, resetting
>
> Any hint on how to solve the problem will be highly appreciated...
>
> Jose
>


>>>
>>
>> __
>> R-package-devel@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-package-devel
>
>
> __
> R-package-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel

__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel


Re: [R-pkg-devel] How to write example results to a tempdir()?

2018-04-26 Thread Duncan Murdoch

On 26/04/2018 9:28 AM, Spencer Graves wrote:



On 2018-04-26 07:11, Jose A Guijarro wrote:

El 25/04/18 a las 20:21, Duncan Murdoch escribió:

On 25/04/2018 1:32 PM, Sarah Goslee wrote:

Don't change the working directory! That has all kinds of unpleasant
side effects for the unsuspecting user, possibly even more so than
writing to a file.

Instead, write the file to the temp directory, and read it from
there, with e.g.

wd <- tempdir()
write(dat, file.path(wd, 'Ttest_1981-2000.dat'))

Using file.path() means that the appropriate path delimiter for that
OS will be used.


That's one good way to do it.  But it is possible to change directory
and change back at the end of the example.  For example,

wd <- tempdir()
savedir <- setwd(wd)

... # the original code that writes and reads in the current dir

setwd(savedir)

There's a worry that an error in the middle of the code will leave
the user in the wrong place.  If that's really unlikely to happen,
then this code is a little simpler than Sarah's suggestion.

If it is likely, you can use tryCatch(..., finally = setwd(savedir)),
but I think Sarah's solution would be preferable in most cases:  many
users will not understand what tryCatch() does.



Hi, Duncan, et al.:


    Under what circumstances should one also use "on.exit":


wd <- tempdir()
savedir <- setwd(wd)
on.exit(setwd(savedir))?


    Even senior R programmers may miss a failure mode.  In a
function, this works well:  I don't need to remember to "setwd(savedir)"
later in the code, which could be a problem if I have multiple exit
points.  I don't know how it would work in a vignette or the examples
section of a *.Rd file.  tryCatch(..., finally = setwd(savedir))
condenses this into one line ... and is too terse for me in many cases.



As you say, on.exit() is appropriate in a function.  It is not 
appropriate in example code.


It's not really defined when it should execute in example code.  If I 
run example(foo), the on.exit() appears to run right away, not waiting 
for the end.  I'd guess it gets installed in the function that runs the 
example code, possibly messing it up.


If I cut and paste from the help page, the on.exit() expression doesn't 
seem to run at all.


I'd expect that a vignette would be similar to example(foo), but I 
haven't tried.


Duncan Murdoch



    Spencer Graves



Duncan Murdoch


Many thanks for the suggestion! In this way I only need to apply
slight changes in the examples and can avoid modifying all read/writes
along the package. I will try this approach and see if it passes the
CRAN checks.

Jose





Sarah


On Wed, Apr 25, 2018 at 12:30 PM, Jose A Guijarro
 wrote:

Dear all,

I am struggling to update my package climatol from version 3.0 to 3.1.
The old version had all examples under a "dontrun" section because
they
needed files created by other examples that the user had to run first.

As this is not acceptable anymore, I made the examples runnable and
prepared small ad-hoc datasets, but then writing files to the user
space
is against the CRAN policy rules, and I was suggested to run them on a
temporal directory. Therefore I changed all my examples to read/write
files to a tempdir(), as in:

\examples{
#Set a temporal working directory and write input files:
wd <- tempdir()
setwd(wd)
data(Ttest) #(This loads matrix 'dat' and data.frame 'est.c')
write(dat,'Ttest_1981-2000.dat')
write.table(est.c,'Ttest_1981-2000.est',row.names=FALSE,col.names=FALSE)

rm(dat,est.c) #remove loaded data from memory space
#Now run the example:
dd2m('Ttest',1981,2000)
#Input and output files can be found in directory:
print(wd)
}

But now CRAN checks return this warning ten times (one for every
example
in the package):

Warning: working directory was changed to ‘/tmp/RtmpWSRK2F’, resetting

Any hint on how to solve the problem will be highly appreciated...

Jose








__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel


__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel



__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel


Re: [R-pkg-devel] How to write example results to a tempdir()?

2018-04-26 Thread Spencer Graves



On 2018-04-26 07:11, Jose A Guijarro wrote:

El 25/04/18 a las 20:21, Duncan Murdoch escribió:

On 25/04/2018 1:32 PM, Sarah Goslee wrote:

Don't change the working directory! That has all kinds of unpleasant
side effects for the unsuspecting user, possibly even more so than
writing to a file.

Instead, write the file to the temp directory, and read it from 
there, with e.g.


wd <- tempdir()
write(dat, file.path(wd, 'Ttest_1981-2000.dat'))

Using file.path() means that the appropriate path delimiter for that
OS will be used.


That's one good way to do it.  But it is possible to change directory 
and change back at the end of the example.  For example,


wd <- tempdir()
savedir <- setwd(wd)

... # the original code that writes and reads in the current dir

setwd(savedir)

There's a worry that an error in the middle of the code will leave 
the user in the wrong place.  If that's really unlikely to happen, 
then this code is a little simpler than Sarah's suggestion.


If it is likely, you can use tryCatch(..., finally = setwd(savedir)), 
but I think Sarah's solution would be preferable in most cases:  many 
users will not understand what tryCatch() does.



Hi, Duncan, et al.:


  Under what circumstances should one also use "on.exit":


wd <- tempdir()
savedir <- setwd(wd)
on.exit(setwd(savedir))?


  Even senior R programmers may miss a failure mode.  In a 
function, this works well:  I don't need to remember to "setwd(savedir)" 
later in the code, which could be a problem if I have multiple exit 
points.  I don't know how it would work in a vignette or the examples 
section of a *.Rd file.  tryCatch(..., finally = setwd(savedir)) 
condenses this into one line ... and is too terse for me in many cases.



  Spencer Graves



Duncan Murdoch


Many thanks for the suggestion! In this way I only need to apply 
slight changes in the examples and can avoid modifying all read/writes 
along the package. I will try this approach and see if it passes the 
CRAN checks.


Jose





Sarah


On Wed, Apr 25, 2018 at 12:30 PM, Jose A Guijarro 
 wrote:

Dear all,

I am struggling to update my package climatol from version 3.0 to 3.1.
The old version had all examples under a "dontrun" section because 
they

needed files created by other examples that the user had to run first.

As this is not acceptable anymore, I made the examples runnable and
prepared small ad-hoc datasets, but then writing files to the user 
space

is against the CRAN policy rules, and I was suggested to run them on a
temporal directory. Therefore I changed all my examples to read/write
files to a tempdir(), as in:

\examples{
#Set a temporal working directory and write input files:
wd <- tempdir()
setwd(wd)
data(Ttest) #(This loads matrix 'dat' and data.frame 'est.c')
write(dat,'Ttest_1981-2000.dat')
write.table(est.c,'Ttest_1981-2000.est',row.names=FALSE,col.names=FALSE) 


rm(dat,est.c) #remove loaded data from memory space
#Now run the example:
dd2m('Ttest',1981,2000)
#Input and output files can be found in directory:
print(wd)
}

But now CRAN checks return this warning ten times (one for every 
example

in the package):

Warning: working directory was changed to ‘/tmp/RtmpWSRK2F’, resetting

Any hint on how to solve the problem will be highly appreciated...

Jose








__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel


__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel


Re: [R-pkg-devel] How to write example results to a tempdir()?

2018-04-26 Thread Jose A Guijarro

El 25/04/18 a las 20:21, Duncan Murdoch escribió:

On 25/04/2018 1:32 PM, Sarah Goslee wrote:

Don't change the working directory! That has all kinds of unpleasant
side effects for the unsuspecting user, possibly even more so than
writing to a file.

Instead, write the file to the temp directory, and read it from 
there, with e.g.


wd <- tempdir()
write(dat, file.path(wd, 'Ttest_1981-2000.dat'))

Using file.path() means that the appropriate path delimiter for that
OS will be used.


That's one good way to do it.  But it is possible to change directory 
and change back at the end of the example.  For example,


wd <- tempdir()
savedir <- setwd(wd)

... # the original code that writes and reads in the current dir

setwd(savedir)

There's a worry that an error in the middle of the code will leave the 
user in the wrong place.  If that's really unlikely to happen, then 
this code is a little simpler than Sarah's suggestion.


If it is likely, you can use tryCatch(..., finally = setwd(savedir)), 
but I think Sarah's solution would be preferable in most cases:  many 
users will not understand what tryCatch() does.


Duncan Murdoch


Many thanks for the suggestion! In this way I only need to apply slight 
changes in the examples and can avoid modifying all read/writes along 
the package. I will try this approach and see if it passes the CRAN checks.


Jose





Sarah


On Wed, Apr 25, 2018 at 12:30 PM, Jose A Guijarro 
 wrote:

Dear all,

I am struggling to update my package climatol from version 3.0 to 3.1.
The old version had all examples under a "dontrun" section because they
needed files created by other examples that the user had to run first.

As this is not acceptable anymore, I made the examples runnable and
prepared small ad-hoc datasets, but then writing files to the user 
space

is against the CRAN policy rules, and I was suggested to run them on a
temporal directory. Therefore I changed all my examples to read/write
files to a tempdir(), as in:

\examples{
#Set a temporal working directory and write input files:
wd <- tempdir()
setwd(wd)
data(Ttest) #(This loads matrix 'dat' and data.frame 'est.c')
write(dat,'Ttest_1981-2000.dat')
write.table(est.c,'Ttest_1981-2000.est',row.names=FALSE,col.names=FALSE) 


rm(dat,est.c) #remove loaded data from memory space
#Now run the example:
dd2m('Ttest',1981,2000)
#Input and output files can be found in directory:
print(wd)
}

But now CRAN checks return this warning ten times (one for every 
example

in the package):

Warning: working directory was changed to ‘/tmp/RtmpWSRK2F’, resetting

Any hint on how to solve the problem will be highly appreciated...

Jose








__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel


Re: [R-pkg-devel] How to write example results to a tempdir()?

2018-04-25 Thread Duncan Murdoch

On 25/04/2018 1:32 PM, Sarah Goslee wrote:

Don't change the working directory! That has all kinds of unpleasant
side effects for the unsuspecting user, possibly even more so than
writing to a file.

Instead, write the file to the temp directory, and read it from there, with e.g.

wd <- tempdir()
write(dat, file.path(wd, 'Ttest_1981-2000.dat'))

Using file.path() means that the appropriate path delimiter for that
OS will be used.


That's one good way to do it.  But it is possible to change directory 
and change back at the end of the example.  For example,


wd <- tempdir()
savedir <- setwd(wd)

... # the original code that writes and reads in the current dir

setwd(savedir)

There's a worry that an error in the middle of the code will leave the 
user in the wrong place.  If that's really unlikely to happen, then this 
code is a little simpler than Sarah's suggestion.


If it is likely, you can use tryCatch(..., finally = setwd(savedir)), 
but I think Sarah's solution would be preferable in most cases:  many 
users will not understand what tryCatch() does.


Duncan Murdoch



Sarah


On Wed, Apr 25, 2018 at 12:30 PM, Jose A Guijarro  wrote:

Dear all,

I am struggling to update my package climatol from version 3.0 to 3.1.
The old version had all examples under a "dontrun" section because they
needed files created by other examples that the user had to run first.

As this is not acceptable anymore, I made the examples runnable and
prepared small ad-hoc datasets, but then writing files to the user space
is against the CRAN policy rules, and I was suggested to run them on a
temporal directory. Therefore I changed all my examples to read/write
files to a tempdir(), as in:

\examples{
#Set a temporal working directory and write input files:
wd <- tempdir()
setwd(wd)
data(Ttest) #(This loads matrix 'dat' and data.frame 'est.c')
write(dat,'Ttest_1981-2000.dat')
write.table(est.c,'Ttest_1981-2000.est',row.names=FALSE,col.names=FALSE)
rm(dat,est.c) #remove loaded data from memory space
#Now run the example:
dd2m('Ttest',1981,2000)
#Input and output files can be found in directory:
print(wd)
}

But now CRAN checks return this warning ten times (one for every example
in the package):

Warning: working directory was changed to ‘/tmp/RtmpWSRK2F’, resetting

Any hint on how to solve the problem will be highly appreciated...

Jose






__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel


Re: [R-pkg-devel] How to write example results to a tempdir()?

2018-04-25 Thread Uwe Ligges



On 25.04.2018 19:45, Jose A Guijarro wrote:

Many thanks for your advise, Sarah!

I will try to implement it. (Now I will have to change all my reading and
writing sentences of all my functions, because they read/write from/to
the users working directory...)


Whcih is OK if the user specifies some filename.

Best,
Uwe Ligges




Best regards,

     Jose

El 25/04/18 a las 19:32, Sarah Goslee escribió:

Don't change the working directory! That has all kinds of unpleasant
side effects for the unsuspecting user, possibly even more so than
writing to a file.

Instead, write the file to the temp directory, and read it from there, 
with e.g.


wd <- tempdir()
write(dat, file.path(wd, 'Ttest_1981-2000.dat'))

Using file.path() means that the appropriate path delimiter for that
OS will be used.

Sarah


On Wed, Apr 25, 2018 at 12:30 PM, Jose A Guijarro 
 wrote:

Dear all,

I am struggling to update my package climatol from version 3.0 to 3.1.
The old version had all examples under a "dontrun" section because they
needed files created by other examples that the user had to run first.

As this is not acceptable anymore, I made the examples runnable and
prepared small ad-hoc datasets, but then writing files to the user space
is against the CRAN policy rules, and I was suggested to run them on a
temporal directory. Therefore I changed all my examples to read/write
files to a tempdir(), as in:

\examples{
#Set a temporal working directory and write input files:
wd <- tempdir()
setwd(wd)
data(Ttest) #(This loads matrix 'dat' and data.frame 'est.c')
write(dat,'Ttest_1981-2000.dat')
write.table(est.c,'Ttest_1981-2000.est',row.names=FALSE,col.names=FALSE)
rm(dat,est.c) #remove loaded data from memory space
#Now run the example:
dd2m('Ttest',1981,2000)
#Input and output files can be found in directory:
print(wd)
}

But now CRAN checks return this warning ten times (one for every example
in the package):

Warning: working directory was changed to ‘/tmp/RtmpWSRK2F’, resetting

Any hint on how to solve the problem will be highly appreciated...

Jose







__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel


Re: [R-pkg-devel] How to write example results to a tempdir()?

2018-04-25 Thread Jose A Guijarro

Many thanks for your advise, Sarah!

I will try to implement it. (Now I will have to change all my reading and
writing sentences of all my functions, because they read/write from/to
the users working directory...)

Best regards,

    Jose

El 25/04/18 a las 19:32, Sarah Goslee escribió:

Don't change the working directory! That has all kinds of unpleasant
side effects for the unsuspecting user, possibly even more so than
writing to a file.

Instead, write the file to the temp directory, and read it from there, with e.g.

wd <- tempdir()
write(dat, file.path(wd, 'Ttest_1981-2000.dat'))

Using file.path() means that the appropriate path delimiter for that
OS will be used.

Sarah


On Wed, Apr 25, 2018 at 12:30 PM, Jose A Guijarro  wrote:

Dear all,

I am struggling to update my package climatol from version 3.0 to 3.1.
The old version had all examples under a "dontrun" section because they
needed files created by other examples that the user had to run first.

As this is not acceptable anymore, I made the examples runnable and
prepared small ad-hoc datasets, but then writing files to the user space
is against the CRAN policy rules, and I was suggested to run them on a
temporal directory. Therefore I changed all my examples to read/write
files to a tempdir(), as in:

\examples{
#Set a temporal working directory and write input files:
wd <- tempdir()
setwd(wd)
data(Ttest) #(This loads matrix 'dat' and data.frame 'est.c')
write(dat,'Ttest_1981-2000.dat')
write.table(est.c,'Ttest_1981-2000.est',row.names=FALSE,col.names=FALSE)
rm(dat,est.c) #remove loaded data from memory space
#Now run the example:
dd2m('Ttest',1981,2000)
#Input and output files can be found in directory:
print(wd)
}

But now CRAN checks return this warning ten times (one for every example
in the package):

Warning: working directory was changed to ‘/tmp/RtmpWSRK2F’, resetting

Any hint on how to solve the problem will be highly appreciated...

Jose





--
Firma
Dr. José A. Guijarro
Logotipo de Aemet
Delegación Territorial en Illes Balears
Jefe de la Unidad de Estudios Meteorológicos del Mediterráneo
Tel. (+34) 971145409   (Tno. interno: 50790)
www.aemet.es 
 
 
 



__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel


Re: [R-pkg-devel] How to write example results to a tempdir()?

2018-04-25 Thread Sarah Goslee
Don't change the working directory! That has all kinds of unpleasant
side effects for the unsuspecting user, possibly even more so than
writing to a file.

Instead, write the file to the temp directory, and read it from there, with e.g.

wd <- tempdir()
write(dat, file.path(wd, 'Ttest_1981-2000.dat'))

Using file.path() means that the appropriate path delimiter for that
OS will be used.

Sarah


On Wed, Apr 25, 2018 at 12:30 PM, Jose A Guijarro  wrote:
> Dear all,
>
> I am struggling to update my package climatol from version 3.0 to 3.1.
> The old version had all examples under a "dontrun" section because they
> needed files created by other examples that the user had to run first.
>
> As this is not acceptable anymore, I made the examples runnable and
> prepared small ad-hoc datasets, but then writing files to the user space
> is against the CRAN policy rules, and I was suggested to run them on a
> temporal directory. Therefore I changed all my examples to read/write
> files to a tempdir(), as in:
>
> \examples{
> #Set a temporal working directory and write input files:
> wd <- tempdir()
> setwd(wd)
> data(Ttest) #(This loads matrix 'dat' and data.frame 'est.c')
> write(dat,'Ttest_1981-2000.dat')
> write.table(est.c,'Ttest_1981-2000.est',row.names=FALSE,col.names=FALSE)
> rm(dat,est.c) #remove loaded data from memory space
> #Now run the example:
> dd2m('Ttest',1981,2000)
> #Input and output files can be found in directory:
> print(wd)
> }
>
> But now CRAN checks return this warning ten times (one for every example
> in the package):
>
> Warning: working directory was changed to ‘/tmp/RtmpWSRK2F’, resetting
>
> Any hint on how to solve the problem will be highly appreciated...
>
> Jose
>


-- 
Sarah Goslee
http://www.functionaldiversity.org

__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel