[Rd] [RE: why does parent.frame() cycle when called from inside capture.output()?]

2016-12-12 Thread frederik
Thanks, Mark - I'm taking up your invitation to forward your message
to the list just because it gives us some valuable data on (1) how
long the behavior has been around, and (2) how many other people
(don't) understand the behavior, and (3) how we might fix or work
around it.

I notice some other people also seem to be diffident about posting on
R-devel; perhaps I should conclude that bugs like this are below the
radar for busy statisticians.

FWIW, after playing around a bit with mvbutils::mvb.parent.frame, I
now have a working "desubN" (see the attachment on my original
message, and this one). I don't really have a good understanding of
*why* it now works, and why the original version didn't work...

Thanks again Mark. Also, nice hacking.

Frederick

- Forwarded message from mark.braving...@data61.csiro.au -

Date: Mon, 12 Dec 2016 06:20:17 +
From: mark.braving...@data61.csiro.au
To: frede...@ofb.net
Subject: RE: [Rd] why does parent.frame() cycle when called from inside
capture.output()?
X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED,
DKIM_VALID,DKIM_VALID_AU,T_SPF_HELO_TEMPERROR,T_SPF_TEMPERROR 
autolearn=ham
autolearn_force=no version=3.4.1
X-Spam-Level: 
X-My-Tags: inbox r-devel

Hi Frederik

[ I'm replying off-list in case you, or the rest of R-devel, don't find this 
reply useful... please fwd to the list if it does help you ]

I'm the author of the 'debug' package. When I wrote it--- many years ago now--- 
I encountered some fairly strange behaviour with frames in the call stack, 
which is reminiscent of what you're seeing. The debug package still works fine, 
so I presume nothing has changed much.

If you load debug and then do ?mvb.sys.parent (and look at the code), you might 
get *some* idea of what's going on. TBH I can't remember the details now...

HTH
Mark

Mark Bravington
CSIRO Marine Lab
Hobart
Australia


From: R-devel [r-devel-boun...@r-project.org] on behalf of frede...@ofb.net 
[frede...@ofb.net]
Sent: 12 December 2016 16:54
To: r-devel@r-project.org
Cc: r-h...@r-project.org
Subject: [Rd] why does parent.frame() cycle when called from inside 
capture.output()?

Hello R devel/help,

I ran into this strange behavior:

# showstack is supposed to walk through the stack of parent
# environments when it is called:
showstack = function() {
  env = environment()
  for(i in 1:12) {
env = do.call(parent.frame, list(), env=env)
print(env)
  }
}

# a simple chain of functions:
g3=function(t) showstack()
g2=function(w) g3(w)
g1=function(z) g2(z)
g=function(y) g1(y)

g()
# outputs:
# 
# 
# 
# 
# 
# 
# 
# 
# 
# ...

cat(capture.output(g()),sep="\n")
# outputs:
# 
# 
# 
# 
# 
# 
# 
# 
# 
# 
# 
# 

The strange thing of course is that the second call doesn't stay with
R_GlobalEnv, but in fact goes into a loop of period 4. I'm not so
surprised that the parent frame of the global environment is itself,
as in the first call, but it seems weird to have a loop of period 4...
Using `ls()` shows that two of the "loop" environments belong to
capture.output() and eval().

But if capture.output is really evaluating its input in the parent
frame, as it appears to be doing from its source code, then I would
have expected the output to be the same as the output I get by
evaluating the same expression in this frame.

I was trying to debug a function which attempts to be a multi-frame
version of deparse(substitute(...)). I'm attaching this function in
case anyone is curious. Perhaps my attachment can shed more light on
the problem I'm having.

Apologies if this is not a bug - I wasn't sure which mailing list to
send this to, and I took a guess.

Thanks,

Frederick


- End forwarded message -
# We can't just use `mvb.parent.frame` as a replacement for
# `parent.frame`, because the former throws an error when there is no
# parent frame - while we had relied on the latter gracefully giving
# us the global environment. So here's a wrapper which gives
# mvb.parent.frame the behavior we want:

my_mvb_parent=function() {
  tryCatch(
mvb.parent.frame(2),
error=function(e) { globalenv()})
}

desubN <- function(y,n=1) {
  env=environment();
  for(i in 1:n) {
y = do.call(substitute, list(substitute(y)), env=env)
env = do.call(my_mvb_parent, list(), env=env)
  }
  deparse(y)
}
g2=function(t) {
  for(i in 1:5) {
print(desubN(t,i))
print(capture.output(desubN(t,i)))
  }
}
g1=function(z) g2(z)
g=function(y) g1(y)

g(log)

# now capture.output seems to give the same results as the bare
# expression
## [1] "t"
## [1] "[1] \"t\""
## [1] "z"
## [1] "[1] \"z\""
## [1] "y"
## [1] "[1] \"y\""
## [1] "log"
## [1] "[1] \"log\""
## [1] "log"
## [1] "[1] \"log\""
__
R-devel@r-project.org mailing list
http

Re: [Rd] Strange behavior when using progress bar (Fwd: Re: [R] The code itself disappears after starting to execute the for loop)

2016-12-12 Thread Jon Skoien

Thanks John,

As the problem can be reproduced, I would like to submit this as a bug 
report. But I think someone will have to add me to Bugzilla first.
Given the few responses, I am not expecting that this will get a high 
priority though...


Jon

On 12/7/2016 2:59 PM, Fox, John wrote:

Dear Martin and Jon,

I can reproduce this problem in the Windows GUI, where I observed it using 
Jon's program after 75 iterations. I didn't observe the problem in a Windows 
terminal or under RStudio, letting the program run for more than 200 iterations 
in each case.

My system and session info:

- snip -


Sys.info()

  sysname  release  version nodename
"Windows" "10 x64""build 14393" "JOHN-CARBON-X1"
  machinelogin user   effective_user
 "x86-64"   "John Fox"   "John Fox"   "John Fox"


sessionInfo()

R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 14393)

locale:
[1] LC_COLLATE=English_Canada.1252  LC_CTYPE=English_Canada.1252
[3] LC_MONETARY=English_Canada.1252 LC_NUMERIC=C
[5] LC_TIME=English_Canada.1252

attached base packages:
[1] stats graphics  grDevices utils datasets  methods   base

- snip -

I hope this helps,
  John

-
John Fox, Professor
McMaster University
Hamilton, Ontario
Canada L8S 4M4
Web: socserv.mcmaster.ca/jfox



-Original Message-
From: R-devel [mailto:r-devel-boun...@r-project.org] On Behalf Of Martin
Maechler
Sent: December 7, 2016 5:58 AM
To: Jon Skoien 
Cc: r-devel@r-project.org
Subject: Re: [Rd] Strange behavior when using progress bar (Fwd: Re: [R] The
code itself disappears after starting to execute the for loop)


Jon Skoien 
 on Wed, 7 Dec 2016 11:04:04 +0100 writes:

 > I would like to ask once more if this is reproducible also for others?
 > If yes, should I submit it as a bug-report?

 > Best,
 > Jon

Please  Windows users .. this is possibly only for you!

Note that I do *not* see problems on Linux (in ESS; did not try RStudio).

Please also indicate in which form you are running R.
Here it does depend if this is inside RStudio, ESS, the "Windows GUI", the
"Windows terminal", ...

Martin Maechler,
ETH Zurich


 > On 11/28/2016 11:26 AM, Jon Skoien wrote:
 >> I first answered to the email below in r-help, but as I did not see
 >> any response, and it looks like a bug/unwanted behavior, I am also
 >> posting here. I have observed this in RGui, whereas it seems not to
 >> happen in RStudio.
 >>
 >> Similar to OP, I sometimes have a problem with functions using the
 >> progress bar. Frequently, the console is cleared after x iterations
 >> when the progress bar is called in a function which is wrapped in a
 >> loop. In the example below, this happened for me every ~44th
 >> iteration. Interestingly, it seems that reduction of the sleep times
 >> in this function increases the number of iterations before clearing.
 >> In my real application, where the progress bar is used in a much
 >> slower function, the console is cleared every 2-3 iteration, which
 >> means that I cannot scroll back to check the output.

  testit <- function(x = sort(runif(20)), ...) {
pb <- txtProgressBar(...)
for(i in c(0, x, 1)) {Sys.sleep(0.2); setTxtProgressBar(pb, i)}
Sys.sleep(1)
close(pb)
  }

  it <- 0
  while (TRUE) {testit(style = 3); it <- it + 1; print(paste("done", it))}

 >> Is this only a problem for a few, or is it reproducible? Any hints to
 >> what the problem could be, or if it can be fixed? I have seen this in
 >> some versions of R, and could also reproduce in 3.3.2.

"some versions of R" ... all on Windows ?

 >>
 >> Best wishes,
 >> Jon
 >>
 >> R version 3.3.2 (2016-10-31)
 >> Platform: x86_64-w64-mingw32/x64 (64-bit)
 >> Running under: Windows 8.1 x64 (build 9600)
 >>
 >> locale:
 >> [1] LC_COLLATE=English_United States.1252
 >> [2] LC_CTYPE=English_United States.1252
 >> [3] LC_MONETARY=English_United States.1252
 >> [4] LC_NUMERIC=C
 >> [5] LC_TIME=English_United States.1252
 >>
 >> attached base packages:
 >> [1] stats graphics  grDevices utils datasets  methods base

[.]

 > Jon Olav Skøien
 > Joint Research Centre - European Commission
 > Institute for Space, Security & Migration
 > Disaster Risk Management Unit

 > Via E. Fermi 2749, TP 122,  I-21027 Ispra (VA), ITALY

 > jon.sko...@jrc.ec.europa.eu
 > Tel:  +39 0332 789205

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


--
Jon Olav Skøien
Joint Research Centre - European Commission
Institute for Space, Security & Migration
Disaster Risk Management Unit

Via E. Fermi 2749, TP 122, 

Re: [Rd] accessing data by packagename::dataname from within package code fails.

2016-12-12 Thread Witold E Wolski
I have narrowed down the problem.
The error
Error : 'AminoAcids' is not an exported object from 'namespace:bibliospec'
Error : unable to load R code in package 'bibliospec'

occurs only if I try to access the data using bibliospec::AminoAcids

within the initialize method of an R reference class.
It does work, as far as I tested everywhere else. In other methods of
a reference class as well as in free functions.
It also does not work in the initialization list to the initialize function.
So I also can't do something like
initialize = function(aminoAcids=bibliospec::AminoAcids){


I guess this is an R FEATURE.

But then where and how is the best practice to initialize class
members with default values?

Thank you.





On 12 December 2016 at 15:45, Witold E Wolski  wrote:
> I am wrting a package called bibliospec.
> I have a dataset in data/AminoAcids.tsv and would like to be able to
> access it with
>
> bibliospec::AminoAcids
>
> from within my package code.
>
> R CMD build gives me the error:
> Error : 'AminoAcids' is not an exported object from 'namespace:bibliospec'
>
> I am able to access the data in package code with
> data(AminoAcids)
> AminoAcids
>
> but this will give me a NOTE with R CMD check
>
> Also, after loading the packagepackage I am able to access the data with
> bibliospec::AminoAcids
>
> But I can't access it from the package bibliospec code.
> So why can't I access bibliospec::AminoAcids from within package code?
>
> Help appreciated
>
> Witold
> --
> Witold Eryk Wolski



-- 
Witold Eryk Wolski

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


Re: [Rd] [R-pkg-devel] accessing data by packagename::dataname from within package code fails.

2016-12-12 Thread William Dunlap via R-devel
You can define the data in the R directory.  You can keep it all in a *.R
file
by wrapping the text of the *.csv file in quotes and using
read.table(text="quoted stuff"), as in:

theData <- read.csv(header=TRUE, text="
English,Digit
One,1
Two,2
Three,3")
N <- nrow(theData)

You need to make sure 'theData' is defined before using it so put
everything requiring
it in one file or use the Collate: directive in the DESCRIPTION file.



Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Mon, Dec 12, 2016 at 12:34 PM, Witold E Wolski 
wrote:

> I have narrowed down the problem.
> The error
> Error : 'AminoAcids' is not an exported object from 'namespace:bibliospec'
> Error : unable to load R code in package 'bibliospec'
>
> occurs only if I try to access the data using bibliospec::AminoAcids
>
> within the initialize method of an R reference class.
> It does work, as far as I tested everywhere else. In other methods of
> a reference class as well as in free functions.
> It also does not work in the initialization list to the initialize
> function.
> So I also can't do something like
> initialize = function(aminoAcids=bibliospec::AminoAcids){
>
>
> I guess this is an R FEATURE.
>
> But then where and how is the best practice to initialize class
> members with default values?
>
> Thank you.
>
>
>
>
>
> On 12 December 2016 at 15:45, Witold E Wolski  wrote:
> > I am wrting a package called bibliospec.
> > I have a dataset in data/AminoAcids.tsv and would like to be able to
> > access it with
> >
> > bibliospec::AminoAcids
> >
> > from within my package code.
> >
> > R CMD build gives me the error:
> > Error : 'AminoAcids' is not an exported object from
> 'namespace:bibliospec'
> >
> > I am able to access the data in package code with
> > data(AminoAcids)
> > AminoAcids
> >
> > but this will give me a NOTE with R CMD check
> >
> > Also, after loading the packagepackage I am able to access the data with
> > bibliospec::AminoAcids
> >
> > But I can't access it from the package bibliospec code.
> > So why can't I access bibliospec::AminoAcids from within package code?
> >
> > Help appreciated
> >
> > Witold
> > --
> > Witold Eryk Wolski
>
>
>
> --
> Witold Eryk Wolski
>
> __
> r-package-de...@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel
>

[[alternative HTML version deleted]]

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


Re: [Rd] accessing data by packagename::dataname from within package code fails.

2016-12-12 Thread Gabriel Becker
Witold,

Are you using the sys data approach to actually put your data into your
package's namespace? From ?data (Good practices section)

Use of ‘data’ within a function without an ‘envir’ argument has

 the almost always undesirable side-effect of putting an object in

 the user's workspace (and indeed, of replacing any object of that

 name already there).  It would almost always be better to put the

 object in the current evaluation environment by ‘data(..., envir =

 environment())’.  However, two alternatives are usually

 preferable, both described in the ‘Writing R Extensions’ manual.


• For sets of data, set up a package to use lazy-loading of

  data.


• For objects which are system data, for example lookup tables

  used in calculations within the function, use a file

  ‘R/sysdata.rda’ in the package sources or create the objects

  by R code at package installation time.


 A sometimes important distinction is that the second approach

 places objects in the namespace but the first does not.  So if it

 is important that the function sees ‘mytable’ as an object from

 the package, it is system data and the second approach should be

 used.  In the unusual case that a package uses a lazy-loaded

 dataset as a default argument to a function, that needs to be

 specified by ‘::’, e.g., ‘survival::survexp.us’.


It does seem a bit strange that the :: works elsewhere but not in
initialize, but I don't have time to track that down atm.

Best,
~G

On Mon, Dec 12, 2016 at 12:34 PM, Witold E Wolski 
wrote:

> I have narrowed down the problem.
> The error
> Error : 'AminoAcids' is not an exported object from 'namespace:bibliospec'
> Error : unable to load R code in package 'bibliospec'
>
> occurs only if I try to access the data using bibliospec::AminoAcids
>
> within the initialize method of an R reference class.
> It does work, as far as I tested everywhere else. In other methods of
> a reference class as well as in free functions.
> It also does not work in the initialization list to the initialize
> function.
> So I also can't do something like
> initialize = function(aminoAcids=bibliospec::AminoAcids){
>
>
> I guess this is an R FEATURE.
>
> But then where and how is the best practice to initialize class
> members with default values?
>
> Thank you.
>
>
>
>
>
> On 12 December 2016 at 15:45, Witold E Wolski  wrote:
> > I am wrting a package called bibliospec.
> > I have a dataset in data/AminoAcids.tsv and would like to be able to
> > access it with
> >
> > bibliospec::AminoAcids
> >
> > from within my package code.
> >
> > R CMD build gives me the error:
> > Error : 'AminoAcids' is not an exported object from
> 'namespace:bibliospec'
> >
> > I am able to access the data in package code with
> > data(AminoAcids)
> > AminoAcids
> >
> > but this will give me a NOTE with R CMD check
> >
> > Also, after loading the packagepackage I am able to access the data with
> > bibliospec::AminoAcids
> >
> > But I can't access it from the package bibliospec code.
> > So why can't I access bibliospec::AminoAcids from within package code?
> >
> > Help appreciated
> >
> > Witold
> > --
> > Witold Eryk Wolski
>
>
>
> --
> Witold Eryk Wolski
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



-- 
Gabriel Becker, PhD
Associate Scientist (Bioinformatics)
Genentech Research

[[alternative HTML version deleted]]

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

Re: [Rd] [RE: why does parent.frame() cycle when called from inside capture.output()?]

2016-12-12 Thread Mark.Bravington
Hi Frederik

Goodo, glad you found mvbutils::mvb.parent.frame useful. I had forgotten that 
it's in mvbutils rather than debug package. This all dates back about 15 
years...

To be fair, I don't think R's behaviour with duplicated-but-aliased frames in 
the call stack is a "bug"--- everything normal just works--- and it's not 
something to be "fixed" IMO, since it's clearly built-in by design and who 
knows what else would break if it got changed? But, working round it is indeed 
sometimes necessary...

[ BTW I'm not particularly diffident about posting to R-devel--- eg here I 
am!--- but I'm generally too busy to check out my own answers thoroughly, so 
rather than risk opening up blind alleys, I tend to suggest things off-list. In 
the cases where I'm sure that I do have a good answer, someone else has already 
usually responded... ]

bye
mark

Mark Bravington
CSIRO Marine Lab
Hobart
Australia


From: frede...@ofb.net [frede...@ofb.net]
Sent: 12 December 2016 19:23
To: R-devel@r-project.org
Cc: Bravington, Mark (Data61, Hobart); r-h...@r-project.org
Subject: [RE: [Rd] why does parent.frame() cycle when called from inside 
capture.output()?]

Thanks, Mark - I'm taking up your invitation to forward your message
to the list just because it gives us some valuable data on (1) how
long the behavior has been around, and (2) how many other people
(don't) understand the behavior, and (3) how we might fix or work
around it.

I notice some other people also seem to be diffident about posting on
R-devel; perhaps I should conclude that bugs like this are below the
radar for busy statisticians.

FWIW, after playing around a bit with mvbutils::mvb.parent.frame, I
now have a working "desubN" (see the attachment on my original
message, and this one). I don't really have a good understanding of
*why* it now works, and why the original version didn't work...

Thanks again Mark. Also, nice hacking.

Frederick

- Forwarded message from mark.braving...@data61.csiro.au -

Date: Mon, 12 Dec 2016 06:20:17 +
From: mark.braving...@data61.csiro.au
To: frede...@ofb.net
Subject: RE: [Rd] why does parent.frame() cycle when called from inside
capture.output()?
X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED,
DKIM_VALID,DKIM_VALID_AU,T_SPF_HELO_TEMPERROR,T_SPF_TEMPERROR 
autolearn=ham
autolearn_force=no version=3.4.1
X-Spam-Level:
X-My-Tags: inbox r-devel

Hi Frederik

[ I'm replying off-list in case you, or the rest of R-devel, don't find this 
reply useful... please fwd to the list if it does help you ]

I'm the author of the 'debug' package. When I wrote it--- many years ago now--- 
I encountered some fairly strange behaviour with frames in the call stack, 
which is reminiscent of what you're seeing. The debug package still works fine, 
so I presume nothing has changed much.

If you load debug and then do ?mvb.sys.parent (and look at the code), you might 
get *some* idea of what's going on. TBH I can't remember the details now...

HTH
Mark

Mark Bravington
CSIRO Marine Lab
Hobart
Australia


From: R-devel [r-devel-boun...@r-project.org] on behalf of frede...@ofb.net 
[frede...@ofb.net]
Sent: 12 December 2016 16:54
To: r-devel@r-project.org
Cc: r-h...@r-project.org
Subject: [Rd] why does parent.frame() cycle when called from inside 
capture.output()?

Hello R devel/help,

I ran into this strange behavior:

# showstack is supposed to walk through the stack of parent
# environments when it is called:
showstack = function() {
  env = environment()
  for(i in 1:12) {
env = do.call(parent.frame, list(), env=env)
print(env)
  }
}

# a simple chain of functions:
g3=function(t) showstack()
g2=function(w) g3(w)
g1=function(z) g2(z)
g=function(y) g1(y)

g()
# outputs:
# 
# 
# 
# 
# 
# 
# 
# 
# 
# ...

cat(capture.output(g()),sep="\n")
# outputs:
# 
# 
# 
# 
# 
# 
# 
# 
# 
# 
# 
# 

The strange thing of course is that the second call doesn't stay with
R_GlobalEnv, but in fact goes into a loop of period 4. I'm not so
surprised that the parent frame of the global environment is itself,
as in the first call, but it seems weird to have a loop of period 4...
Using `ls()` shows that two of the "loop" environments belong to
capture.output() and eval().

But if capture.output is really evaluating its input in the parent
frame, as it appears to be doing from its source code, then I would
have expected the output to be the same as the output I get by
evaluating the same expression in this frame.

I was trying to debug a function which attempts to be a multi-frame
version of deparse(substitute(...)). I'm attaching this function in
case anyone is curious. Perhaps my attachment can shed more light on
the problem I'm having.

Apologies if this is not a bug -