Re: [Rd] odd behavior of names

2018-07-30 Thread Martin Maechler
> William Dunlap via R-devel 
> on Sun, 29 Jul 2018 10:06:40 -0700 writes:

> Bugzilla issue 16101 describes another
> first-list-name-printed-differently oddity with 
> the Windows GUI version of R:
  

Indeed:
1) "first-list-name-printed" [i.e "names" only in that
 context of printing]
2) Windows GUI version of R only
   (not 'Rterm', i.e., not in ESS (emacs speaks
   statistics), nor in Rstudio on Windows

Kevin Ushey has reported this last week in an official (and
perfect) bug report to R's bugzilla bug reporting site:

 https://bugs.r-project.org/bugzilla/show_bug.cgi?id=17447

Bug ID: 17447
   Summary: list() prints first variable name in backticks
   
 Component: Windows GUI / Window specific
  Reporter: kevinushey ..  gmail ..

His minimal REPREX was even much simpler:

   > list(a = 1, b = 2)
$`a`
[1] 1

$b
[1] 2


Thank you, Bill, for the nice extra example.

Martin Maechler
ETH Zurich and R Core Team



> > a <- "One is \u043E\u0434\u0438\u043D\nTwo is \u0434\u0432\u0430\n"
> > Encoding(a) # expect "UTF-8"
> [1] "UTF-8"
> > sapply(strsplit(a, "\n")[[1]], charToRaw)[c(1,1,2)]
> $`One is один`
>  [1] 4f 6e 65 20 69 73 20 d0 be d0 b4 d0
> [13] b8 d0 bd
> 
> $`One is `
>  [1] 4f 6e 65 20 69 73 20 d0 be d0 b4 d0
> [13] b8 d0 bd
> 
> $`Two is `
>  [1] 54 77 6f 20 69 73 20 d0 b4 d0 b2 d0
> [13] b0
> 
> > names(.Last.value)
> [1] "One is один" "One is один"
> [3] "Two is два"
> 
> 
> 
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com

[..]

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


Re: [Rd] apply with zero-row matrix

2018-07-30 Thread Martin Maechler
> David Hugh-Jones 
> on Mon, 30 Jul 2018 05:33:19 +0100 writes:

> Forgive me if this has been asked many times before, but I
> couldn't find anything on the mailing lists.

> I'd expect apply(m, 1, foo) not to call `foo` if m is a
> matrix with zero rows.  In fact:

> m <- matrix(NA, 0, 5)
> apply(m, 1, function (x) {cat("Called...\n"); print(x)})
> ## Called...
> ## [1] FALSE FALSE FALSE FALSE FALSE


> Similarly for apply(m, 2,...) if m has no columns.  Is
> there a reason for this?

Yes :

The reverse is really true for almost all basic R functions:

They *are* called and give an "empty" result automatically
when the main argument is empty.

What you basicaly propose is to add an extra

 if()
 return()

to all R functions.  While that makes sense for high-level R
functions that do a lot of things, this would really be a bad
idea in general :

This would make all of these basic functions larger {more to maintain} and
slightly slower for all non-zero cases just to make them
slightly faster for the rare zero-length case.

Martin Maechler
ETH Zurich and R core Team

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


Re: [Rd] apply with zero-row matrix

2018-07-30 Thread David Hugh-Jones
Hi Martin,

Fair enough for R functions in general. But the behaviour of apply violates
the expectation that apply(m, 1, fun) calls fun n times when m has n rows.
That seems pretty basic.

Also, I understand from your argument why it makes sense to call apply and
return a special result (presumably NULL) for an empty argument; but why
should apply call fun?

Cheers
David

On Mon, 30 Jul 2018 at 08:41, Martin Maechler 
wrote:

> > David Hugh-Jones
> > on Mon, 30 Jul 2018 05:33:19 +0100 writes:
>
> > Forgive me if this has been asked many times before, but I
> > couldn't find anything on the mailing lists.
>
> > I'd expect apply(m, 1, foo) not to call `foo` if m is a
> > matrix with zero rows.  In fact:
>
> > m <- matrix(NA, 0, 5)
> > apply(m, 1, function (x) {cat("Called...\n"); print(x)})
> > ## Called...
> > ## [1] FALSE FALSE FALSE FALSE FALSE
>
>
> > Similarly for apply(m, 2,...) if m has no columns.  Is
> > there a reason for this?
>
> Yes :
>
> The reverse is really true for almost all basic R functions:
>
> They *are* called and give an "empty" result automatically
> when the main argument is empty.
>
> What you basicaly propose is to add an extra
>
>  if()
>  return()
>
> to all R functions.  While that makes sense for high-level R
> functions that do a lot of things, this would really be a bad
> idea in general :
>
> This would make all of these basic functions larger {more to maintain} and
> slightly slower for all non-zero cases just to make them
> slightly faster for the rare zero-length case.
>
> Martin Maechler
> ETH Zurich and R core Team
>
> --
Sent from Gmail Mobile

[[alternative HTML version deleted]]

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


Re: [Rd] Fwd: help building very old R

2018-07-30 Thread Dirk Eddelbuettel


On 30 July 2018 at 05:35, David Hugh-Jones wrote:
| Hi guys,
| 
| Perhaps someone here can help.
| 
| I am trying to build versions of R 1 for the rcheology package (just
| arrived on CRAN).
| 
| For R prior to 1.5.0, I cannot configure support for tcl-tk.
| 
| I am building on Debian Woody (provided by Docker debian/eol) and have the
| following packages installed:
| r-base-dev tclx8.3-dev tk8.3-dev xvfb xbase-clients x-window-system-core
| 
| I download R source from http://cran.r-project.org/src/base/R-1 and run
| 
| ./configure --with-tcl-tk=yes
| --with-tcl-config=/usr/lib/tcl8.3/tclConfig.sh
|   --with-tk-config=/usr/lib/tk8.3/tkConfig.sh
| 
| These are the locations for the relevant tkConfig.sh and tclConfig.sh files.
| This gives output as follows:
| 
| R is now configured for x86_64-unknown-linux-gnu
| 
|   Source directory:  .
|   Installation directory:/usr/local
| 
|   C compiler:gcc  -g -O2
|   C++ compiler:  c++  -g -O2
|   FORTRAN compiler:  g77  -g -O2
|   X11 support:   yes
|   Gnome support: no
|   Tcl/Tk support:no
|   R profiling support:   yes
|   R as a shared library: no
| 
| And config.log reveals:
| configure:13099: checking for /usr/lib/tcl8.3/tclConfig.sh
| configure:13134: checking for /usr/lib/tk8.3/tkConfig.sh
| configure:13204: checking for /usr/include/tcl8.3/tcl.h
| configure:13214: gcc -E -I/usr/local/include conftest.c >/dev/null
| 2>conftest.out
| configure:13313: checking for /usr/include/tk8.3/tk.h
| configure:13323: gcc -E -I/usr/local/include -I/usr/X11R6/include
| -I/usr/include
| /tcl8.3 conftest.c >/dev/null 2>conftest.out
| configure:13319: /usr/include/tk8.3/tk.h: No such file or directory
| configure: failed program was:
| #line 13318 "configure"
| #include "confdefs.h"
| #include 
| configure:13348: checking for /usr/include/tk.h
| configure:13358: gcc -E -I/usr/local/include -I/usr/X11R6/include
| -I/usr/include
| /tcl8.3 conftest.c >/dev/null 2>conftest.out
| configure:13354: /usr/include/tk.h: No such file or directory
| configure: failed program was:
| #line 13353 "configure"
| #include "confdefs.h"
| #include 
| configure:13385: checking for tk.h
| configure:13389: tk.h: No such file or directory
| 
| In fact, tk.h is in  /usr/include/tcl8.3/ , despite the failed program
| compilation report.
| 
| R 1.5.0 and above work fine. Can anyone remember far back, if something
| changed in the configure script?
| 
| Alternatively, those who are feeling brave can download the Docker image
| creation scripts from github.com/hughjonesd/rcheology .

Have you considered the actual Debian packages from "way back then" ?
Debian has this nifty snapshot archive where you can get old binaries:

  http://snapshot.debian.org/

For (source package) r-base we see 3.5.1 all the way down to 0.61.2

  http://snapshot.debian.org/package/r-base/

Alternatively, the package itself is now (finally, my bad) in a git repo
which goes back to 0.61.2 as well (using snapshot as the feeder)

  https://salsa.debian.org/edd/r-base/commits/master

Hth,  Dirk

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org

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


Re: [Rd] apply with zero-row matrix

2018-07-30 Thread Martin Maechler
> David Hugh-Jones 
> on Mon, 30 Jul 2018 10:12:24 +0100 writes:

> Hi Martin, Fair enough for R functions in general. But the
> behaviour of apply violates the expectation that apply(m,
> 1, fun) calls fun n times when m has n rows.  That seems
> pretty basic.

Well, that expectation is obviously wrong ;-)  see below

> Also, I understand from your argument why it makes sense
> to call apply and return a special result (presumably
> NULL) for an empty argument; but why should apply call fun?

> Cheers David

The reason is seen e.g. in

> apply(matrix(,0,3), 2, quantile)
 [,1] [,2] [,3]
0% NA   NA   NA
25%NA   NA   NA
50%NA   NA   NA
75%NA   NA   NA
100%   NA   NA   NA
> 

and that is documented (+/-) in the first paragraph of the
'Value:' section of help(apply) :

 > Value:
 > 
 >  If each call to ‘FUN’ returns a vector of length ‘n’, then ‘apply’
 >  returns an array of dimension ‘c(n, dim(X)[MARGIN])’ if ‘n > 1’.
 >  If ‘n’ equals ‘1’, ‘apply’ returns a vector if ‘MARGIN’ has length
 >  1 and an array of dimension ‘dim(X)[MARGIN]’ otherwise.  If ‘n’ is
 >  ‘0’, the result has length 0 but not necessarily the ‘correct’
 >  dimension.


To determine 'n', the function *is* called once even when
length(X) ==  0

It may indeed be would helpful to add this explicitly to the
help page  ( /src/library/base/man/apply.Rd ).
Can you propose a wording (in *.Rd if possible) ?

With regards,
Martin

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


Re: [Rd] apply with zero-row matrix

2018-07-30 Thread Emil Bode
Hi David,

Besides Martins point, there is also the issue that for a lot of cases you 
would still like to have the right class returned.
Right now these are returns:

> apply(matrix(NA_integer_,0,5), 1, class)
character(0)
> apply(matrix(NA_integer_,0,5), 1, identity)
integer(0)
> apply(matrix(NA,0,5), 1, identity)
logical(0)

In your case, these would all return NULL, so I think there is value in 
running FUN at least once (Say if you'd want to check if FUN always returns the 
right class). 
And from a philosophical point of view, R is mostly a functional 
programming language, I think if you want side-effects a for-loop would look 
better.


Best regards, 
Emil Bode
 
Data-analyst
 
+31 6 43 83 89 33
emil.b...@dans.knaw.nl
 
DANS: Netherlands Institute for Permanent Access to Digital Research 
Resources
Anna van Saksenlaan 51 | 2593 HW Den Haag | +31 70 349 44 50 | 
i...@dans.knaw.nl  | dans.knaw.nl 

DANS is an institute of the Dutch Academy KNAW  and 
funding organisation NWO . 

On 30/07/2018, 11:12, "R-devel on behalf of David Hugh-Jones" 
 wrote:

Hi Martin,

Fair enough for R functions in general. But the behaviour of apply 
violates
the expectation that apply(m, 1, fun) calls fun n times when m has n 
rows.
That seems pretty basic.

Also, I understand from your argument why it makes sense to call apply 
and
return a special result (presumably NULL) for an empty argument; but why
should apply call fun?

Cheers
David

On Mon, 30 Jul 2018 at 08:41, Martin Maechler 

wrote:

> > David Hugh-Jones
> > on Mon, 30 Jul 2018 05:33:19 +0100 writes:
>
> > Forgive me if this has been asked many times before, but I
> > couldn't find anything on the mailing lists.
>
> > I'd expect apply(m, 1, foo) not to call `foo` if m is a
> > matrix with zero rows.  In fact:
>
> > m <- matrix(NA, 0, 5)
> > apply(m, 1, function (x) {cat("Called...\n"); print(x)})
> > ## Called...
> > ## [1] FALSE FALSE FALSE FALSE FALSE
>
>
> > Similarly for apply(m, 2,...) if m has no columns.  Is
> > there a reason for this?
>
> Yes :
>
> The reverse is really true for almost all basic R functions:
>
> They *are* called and give an "empty" result automatically
> when the main argument is empty.
>
> What you basicaly propose is to add an extra
>
>  if()
>  return()
>
> to all R functions.  While that makes sense for high-level R
> functions that do a lot of things, this would really be a bad
> idea in general :
>
> This would make all of these basic functions larger {more to 
maintain} and
> slightly slower for all non-zero cases just to make them
> slightly faster for the rare zero-length case.
>
> Martin Maechler
> ETH Zurich and R core Team
>
> --
Sent from Gmail Mobile

[[alternative HTML version deleted]]

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




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


Re: [Rd] apply with zero-row matrix

2018-07-30 Thread Gabor Grothendieck
Try pmap and related functions in purrr:

  pmap(as.data.frame(m), ~ { cat("Called...\n"); print(c(...)) })
  ## list()

On Mon, Jul 30, 2018 at 12:33 AM, David Hugh-Jones
 wrote:
> Forgive me if this has been asked many times before, but I couldn't find
> anything on the mailing lists.
>
> I'd expect apply(m, 1, foo) not to call `foo` if m is a matrix with zero
> rows.
> In fact:
>
> m <- matrix(NA, 0, 5)
> apply(m, 1, function (x) {cat("Called...\n"); print(x)})
> ## Called...
> ## [1] FALSE FALSE FALSE FALSE FALSE
>
> Similarly for apply(m, 2,...) if m has no columns.
> Is there a reason for this? Could it be documented?
>
> David
> --
> Sent from Gmail Mobile
>
> [[alternative HTML version deleted]]
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

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


Re: [Rd] apply with zero-row matrix

2018-07-30 Thread David Hugh-Jones
Interesting discussion. I'm not wholly convinced by Martin's and Emil's
arguments. The behaviour seems to violate an obvious expectation (fun is
called once per row) to satisfy a subtle one (result has a guaranteed
dimension and type).

In any case, here's a suggested chunk of rd to go at the end of the "Value":

If \code{dim(X)[MARGIN]} is zero, then \code{FUN} is called once, with an
argument of the appropriate dimensions. The argument's type is the same as
\code{typeof(m)}, and the argument values are those returned by
\code{vector(typeof(m))}. For example, if m is numeric, the argument will
be a vector (or matrix or array) of zeroes. The type and length of the
value returned by \code{FUN} is used to determine the type of the result.

And at the end of "Details":

\code{FUN} is always called at least once, see below.


David


On Mon, 30 Jul 2018 at 15:05, Gabor Grothendieck 
wrote:

> Try pmap and related functions in purrr:
>
>   pmap(as.data.frame(m), ~ { cat("Called...\n"); print(c(...)) })
>   ## list()
>
> On Mon, Jul 30, 2018 at 12:33 AM, David Hugh-Jones
>  wrote:
> > Forgive me if this has been asked many times before, but I couldn't find
> > anything on the mailing lists.
> >
> > I'd expect apply(m, 1, foo) not to call `foo` if m is a matrix with zero
> > rows.
> > In fact:
> >
> > m <- matrix(NA, 0, 5)
> > apply(m, 1, function (x) {cat("Called...\n"); print(x)})
> > ## Called...
> > ## [1] FALSE FALSE FALSE FALSE FALSE
> >
> > Similarly for apply(m, 2,...) if m has no columns.
> > Is there a reason for this? Could it be documented?
> >
> > David
> > --
> > Sent from Gmail Mobile
> >
> > [[alternative HTML version deleted]]
> >
> > __
> > R-devel@r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
>
>
>
> --
> Statistics & Software Consulting
> GKX Group, GKX Associates Inc.
> tel: 1-877-GKX-GROUP
> email: ggrothendieck at gmail.com
>

[[alternative HTML version deleted]]

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


Re: [Rd] Fwd: help building very old R

2018-07-30 Thread David Hugh-Jones
Thanks for the tip. That could be a huge timesaver. But it lists only a
single package for versions 0.90.1-2 ... how does that work?
David


On Mon, 30 Jul 2018 at 12:27, Dirk Eddelbuettel  wrote:

>
> On 30 July 2018 at 05:35, David Hugh-Jones wrote:
> | Hi guys,
> |
> | Perhaps someone here can help.
> |
> | I am trying to build versions of R 1 for the rcheology package (just
> | arrived on CRAN).
> |
> | For R prior to 1.5.0, I cannot configure support for tcl-tk.
> |
> | I am building on Debian Woody (provided by Docker debian/eol) and have
> the
> | following packages installed:
> | r-base-dev tclx8.3-dev tk8.3-dev xvfb xbase-clients x-window-system-core
> |
> | I download R source from http://cran.r-project.org/src/base/R-1 and run
> |
> | ./configure --with-tcl-tk=yes
> | --with-tcl-config=/usr/lib/tcl8.3/tclConfig.sh
> |   --with-tk-config=/usr/lib/tk8.3/tkConfig.sh
> |
> | These are the locations for the relevant tkConfig.sh and tclConfig.sh
> files.
> | This gives output as follows:
> |
> | R is now configured for x86_64-unknown-linux-gnu
> |
> |   Source directory:  .
> |   Installation directory:/usr/local
> |
> |   C compiler:gcc  -g -O2
> |   C++ compiler:  c++  -g -O2
> |   FORTRAN compiler:  g77  -g -O2
> |   X11 support:   yes
> |   Gnome support: no
> |   Tcl/Tk support:no
> |   R profiling support:   yes
> |   R as a shared library: no
> |
> | And config.log reveals:
> | configure:13099: checking for /usr/lib/tcl8.3/tclConfig.sh
> | configure:13134: checking for /usr/lib/tk8.3/tkConfig.sh
> | configure:13204: checking for /usr/include/tcl8.3/tcl.h
> | configure:13214: gcc -E -I/usr/local/include conftest.c >/dev/null
> | 2>conftest.out
> | configure:13313: checking for /usr/include/tk8.3/tk.h
> | configure:13323: gcc -E -I/usr/local/include -I/usr/X11R6/include
> | -I/usr/include
> | /tcl8.3 conftest.c >/dev/null 2>conftest.out
> | configure:13319: /usr/include/tk8.3/tk.h: No such file or directory
> | configure: failed program was:
> | #line 13318 "configure"
> | #include "confdefs.h"
> | #include 
> | configure:13348: checking for /usr/include/tk.h
> | configure:13358: gcc -E -I/usr/local/include -I/usr/X11R6/include
> | -I/usr/include
> | /tcl8.3 conftest.c >/dev/null 2>conftest.out
> | configure:13354: /usr/include/tk.h: No such file or directory
> | configure: failed program was:
> | #line 13353 "configure"
> | #include "confdefs.h"
> | #include 
> | configure:13385: checking for tk.h
> | configure:13389: tk.h: No such file or directory
> |
> | In fact, tk.h is in  /usr/include/tcl8.3/ , despite the failed program
> | compilation report.
> |
> | R 1.5.0 and above work fine. Can anyone remember far back, if something
> | changed in the configure script?
> |
> | Alternatively, those who are feeling brave can download the Docker image
> | creation scripts from github.com/hughjonesd/rcheology .
>
> Have you considered the actual Debian packages from "way back then" ?
> Debian has this nifty snapshot archive where you can get old binaries:
>
>   http://snapshot.debian.org/
>
> For (source package) r-base we see 3.5.1 all the way down to 0.61.2
>
>   http://snapshot.debian.org/package/r-base/
>
> Alternatively, the package itself is now (finally, my bad) in a git repo
> which goes back to 0.61.2 as well (using snapshot as the feeder)
>
>   https://salsa.debian.org/edd/r-base/commits/master
>
> Hth,  Dirk
>
> --
> http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org
>

[[alternative HTML version deleted]]

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


Re: [Rd] Fwd: help building very old R

2018-07-30 Thread Dirk Eddelbuettel


On 30 July 2018 at 16:26, David Hugh-Jones wrote:
| Thanks for the tip. That could be a huge timesaver. But it lists only a
| single package for versions 0.90.1-2 ... how does that work?

The lookip is source or binary-package based. Eg here are three binaries
built from source 'r-base':

  http://snapshot.debian.org/binary/r-base-core/
  http://snapshot.debian.org/binary/r-base-dev/
  http://snapshot.debian.org/binary/r-mathlib/

etc pp

Dirk

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org

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


[Rd] Problem with parseData

2018-07-30 Thread Barbara Lerner
Hi,

I have run into a problem with parseData from the utils package.  When 
an assignment is done with = instead of <-, the information provided by 
parseData does not include an entry for the assignment.

For this input, stored in file "BadPosition.R":

y <- 5
foo = 7

And running this code:

parsed <- parse("BadPosition.R", keep.source=TRUE)
parsedData <- utils::getParseData (parsed, includeText=TRUE)
print(paste("parseData =", parsedData))

I get the following output:

[1] "parseData = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2)"
[2] "parseData = c(1, 1, 1, 3, 6, 6, 1, 1, 5, 7, 7)"
[3] "parseData = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2)"
[4] "parseData = c(6, 1, 1, 4, 6, 6, 3, 3, 5, 7, 7)"
[5] "parseData = c(7, 1, 3, 2, 4, 5, 10, 12, 11, 13, 14)"
[6] "parseData = c(0, 3, 7, 7, 5, 7, 12, 0, 0, 14, 0)"
[7] "parseData = c(\"expr\", \"SYMBOL\", \"expr\", \"LEFT_ASSIGN\", 
\"NUM_CONST\", \"expr\", \"SYMBOL\", \"expr\", \"EQ_ASSIGN\", 
\"NUM_CONST\", \"expr\")"
[8] "parseData = c(FALSE, TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, FALSE, 
TRUE, TRUE, FALSE)"
[9] "parseData = c(\"y <- 5\", \"y\", \"y\", \"<-\", \"5\", \"5\", 
\"foo\", \"foo\", \"=\", \"7\", \"7\")"

Notice how there is an entry for "y <- 5" beginning on line 1, column 1, 
ending at line 1, column 6, but there is no analogous entry for "foo = 7".

I am running R 3.5.0 on a Mac running 10.12.6.

Thanks for your help and please let me know if you need any further 
information.

Barbara

-- 
Barbara Lerner
Associate Professor
Computer Science Department
Mount Holyoke College



[[alternative HTML version deleted]]

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


[Rd] trace in uniroot() ?

2018-07-30 Thread J C Nash
In looking at rootfinding for the histoRicalg project (see 
gitlab.com/nashjc/histoRicalg),
I thought I would check how uniroot() solves some problems. The following short 
example

ff <- function(x){ exp(0.5*x) - 2 }
ff(2)
ff(1)
uniroot(ff, 0, 10)
uniroot(ff, c(0, 10), trace=1)
uniroot(ff, c(0, 10), trace=TRUE)


shows that the trace parameter, as described in the Rd file, does not seem to
be functional except in limited situations (and it suggests an
integer, then uses a logical for the example, e.g.,
 ## numerically, f(-|M|) becomes zero :
 u3 <- uniroot(exp, c(0,2), extendInt="yes", trace=TRUE)
)

When extendInt is set, then there is some information output, but trace alone
produces nothing.

I looked at the source code -- it is in R-3.5.1/src/library/stats/R/nlm.R and
calls zeroin2 code from R-3.5.1/src/library/stats/src/optimize.c as far as I
can determing. My code inspection suggests trace does not show the iterations
of the rootfinding, and only has effect when the search interval is allowed
to be extended. It does not appear that there is any mechanism to ask
the zeroin2 C code to display intermediate work.

This isn't desperately important for me as I wrote an R version of the code in
package rootoned on R-forge (which Martin Maechler adapted as unirootR.R in
Rmpfr so multi-precision roots can be found). My zeroin.R has 'trace' to get
the pattern of different steps. In fact it is a bit excessive. Note
unirootR.R uses 'verbose' rather than 'trace'. However, it would be nice to be
able to see what is going on with uniroot() to verify equivalent operation at
the same precision level. It is very easy for codes to be very slightly
different and give quite widely different output.

Indeed, even without the trace, we see (zeroin from rootoned here)

> zeroin(ff, c(0, 10), trace=FALSE)
$root
[1] 1.386294

$froot
[1] -5.658169e-10

$rtol
[1] 7.450581e-09

$maxit
[1] 9

> uniroot(ff, c(0, 10), trace=FALSE)
$root
[1] 1.38629

$f.root
[1] -4.66072e-06

$iter
[1] 10

$init.it
[1] NA

$estim.prec
[1] 6.103516e-05

>

Is the lack of trace a bug, or at least an oversight? Being able to follow 
iterations is a
classic approach to checking that computations are proceeding as they should.

Best, JN

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


[Rd] Code Optimization: print.data.frame + as.data.frame(head(x, n = options("max.print")))

2018-07-30 Thread Juan Telleria Ruiz de Aguirre
Dear R Developers,

I would like to propose a simple optimization for print.data.frame
base function:

To add: x <- as.data.frame(head(x, n = options("max.print")))

This would prevent that, if for example, we have a 10GB data.frame
(e.g.: Instead of a data.table), and we accidentally print it, the R
Session does not "collapse", forcing us to press ESC or kill the
RSession.

function (x, ..., digits = NULL, quote = FALSE, right = TRUE,
  row.names = TRUE)
{
  n <- length(row.names(x))
  if (length(x) == 0L) {
cat(sprintf(ngettext(n, "data frame with 0 columns and %d row",
 "data frame with 0 columns and %d rows"), n), "\n",
sep = "")
  }
  else if (n == 0L) {
print.default(names(x), quote = FALSE)
cat(gettext("<0 rows> (or 0-length row.names)\n"))
  }
  else {

x <- as.data.frame(head(x, n = options("max.print")))

m <- as.matrix(format.data.frame(x, digits = digits,
 na.encode = FALSE))
if (!isTRUE(row.names))
  dimnames(m)[[1L]] <- if (isFALSE(row.names))
rep.int("", n)
else row.names
print(m, ..., quote = quote, right = right)
  }
  invisible(x)
}

Thank you.

Best,
Juan

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


Re: [Rd] Code Optimization: print.data.frame + as.data.frame(head(x, n = options("max.print")))

2018-07-30 Thread Juan Telleria Ruiz de Aguirre
I polished a little bit more the function:

* Used:  getOption("max.print")
* Added comment at the end:  cat('[ reached getOption("max.print") --
omitted ', omitted,' rows ]')

function (x, ..., digits = NULL, quote = FALSE, right = TRUE,
  row.names = TRUE)
{
  n <- length(row.names(x))
  if (length(x) == 0L) {
cat(sprintf(ngettext(n, "data frame with 0 columns and %d row",
 "data frame with 0 columns and %d rows"), n), "\n",
sep = "")
  }
  else if (n == 0L) {
print.default(names(x), quote = FALSE)
cat(gettext("<0 rows> (or 0-length row.names)\n"))
  }
  else {

omitted <- nrow(x)-getOption("max.print")

x <- as.data.frame(head(x, n = getOption("max.print")))

m <- as.matrix(format.data.frame(x, digits = digits,
 na.encode = FALSE))
if (!isTRUE(row.names))
  dimnames(m)[[1L]] <- if (isFALSE(row.names))
rep.int("", n)
else row.names
print(m, ..., quote = quote, right = right)

if((nrow(x)-getOption("max.print"))>0){

  cat('[ reached getOption("max.print") -- omitted ', omitted,' rows ]')

}

  }
  invisible(x)
}

[[alternative HTML version deleted]]

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


Re: [Rd] apply with zero-row matrix

2018-07-30 Thread Deepayan Sarkar
On Mon, Jul 30, 2018 at 6:08 PM, Martin Maechler
 wrote:
>> David Hugh-Jones
>> on Mon, 30 Jul 2018 10:12:24 +0100 writes:
>
> > Hi Martin, Fair enough for R functions in general. But the
> > behaviour of apply violates the expectation that apply(m,
> > 1, fun) calls fun n times when m has n rows.  That seems
> > pretty basic.
>
> Well, that expectation is obviously wrong ;-)  see below
>
> > Also, I understand from your argument why it makes sense
> > to call apply and return a special result (presumably
> > NULL) for an empty argument; but why should apply call fun?
>
> > Cheers David
>
> The reason is seen e.g. in
>
> > apply(matrix(,0,3), 2, quantile)
>  [,1] [,2] [,3]
> 0% NA   NA   NA
> 25%NA   NA   NA
> 50%NA   NA   NA
> 75%NA   NA   NA
> 100%   NA   NA   NA
> >

I don't think this example is relevant to what David is saying:
matrix(,0,3) has three columns, so he would expect quantile() to be
called 3 times, as it is.

I think his question is why quantile() is called at all when the input
has 0 rows, as in

apply(matrix(,0,3), 1, quantile)
# named numeric(0)

> and that is documented (+/-) in the first paragraph of the
> 'Value:' section of help(apply) :
>
>  > Value:
>  >
>  >  If each call to ‘FUN’ returns a vector of length ‘n’, then ‘apply’
>  >  returns an array of dimension ‘c(n, dim(X)[MARGIN])’ if ‘n > 1’.
>  >  If ‘n’ equals ‘1’, ‘apply’ returns a vector if ‘MARGIN’ has length
>  >  1 and an array of dimension ‘dim(X)[MARGIN]’ otherwise.  If ‘n’ is
>  >  ‘0’, the result has length 0 but not necessarily the ‘correct’
>  >  dimension.
>
>
> To determine 'n', the function *is* called once even when
> length(X) ==  0

This part of the docs also doesn't seem applicable, and in fact seems
incorrect: here we should have (according to the docs)

n = length(quantile(logical(0))) # 5

but the result does not have dim == c(5, 0) as the docs suggest:

dim(apply(matrix(,0,3), 1, quantile))
# NULL

So the length of the result of calling FUN() seems to be ignored in
this case, and as Emil points out, is only used to determine the mode
of the result.

I can't immediately think of an example where returning NULL instead
would make a difference, but there may well be some.

-Deepayan

> It may indeed be would helpful to add this explicitly to the
> help page  ( /src/library/base/man/apply.Rd ).
> Can you propose a wording (in *.Rd if possible) ?
>
> With regards,
> Martin
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

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