[Rd] [PATCH 2/2] readtable: add test for type conversion hook 'colConvert'

2019-03-25 Thread Kurt Van Dijck
Signed-off-by: Kurt Van Dijck 
---
 tests/reg-tests-2.R | 21 +
 tests/reg-tests-2.Rout.save | 27 +++
 2 files changed, 48 insertions(+)

diff --git a/tests/reg-tests-2.R b/tests/reg-tests-2.R
index 9fd5242..5026fe7 100644
--- a/tests/reg-tests-2.R
+++ b/tests/reg-tests-2.R
@@ -1329,6 +1329,27 @@ unlink(foo)
 ## added in 2.0.0
 
 
+## colConvert in read.table
+probecol <- function(col) {
+   tmp <- as.POSIXlt(col, optional=TRUE, tryFormats=c("%d/%m/%Y %H:%M"));
+   if (all(!is.na(tmp)))
+   return (tmp)
+   tmp <- as.POSIXlt(col, optional=TRUE, tryFormats=c("%d/%m/%Y"));
+   if (all(!is.na(tmp)))
+   return (tmp)
+}
+
+Mat <- matrix(c(1:3, letters[1:3], 1:3, LETTERS[1:3],
+c("22/4/1969", "8/4/1971", "23/9/1973"),
+c("22/4/1969 6:01", " 8/4/1971 7:23", "23/9/1973 8:45")),
+  3, 6)
+foo <- tempfile()
+write.table(Mat, foo, sep = ",", col.names = FALSE, row.names = FALSE)
+read.table(foo, sep = ",", colConvert=probecol)
+unlist(sapply(.Last.value, class))
+unlink(foo)
+
+
 ## write.table with complex columns (PR#7260, in part)
 write.table(data.frame(x = 0.5+1:4, y = 1:4 + 1.5i), file = "")
 # printed all as complex in 2.0.0.
diff --git a/tests/reg-tests-2.Rout.save b/tests/reg-tests-2.Rout.save
index 598dd71..668898e 100644
--- a/tests/reg-tests-2.Rout.save
+++ b/tests/reg-tests-2.Rout.save
@@ -4206,6 +4206,33 @@ Warning message:
 > ## added in 2.0.0
 > 
 > 
+> ## colConvert in read.table
+> probecol <- function(col) {
++  tmp <- as.POSIXlt(col, optional=TRUE, tryFormats=c("%d/%m/%Y %H:%M"));
++  if (all(!is.na(tmp)))
++  return (tmp)
++  tmp <- as.POSIXlt(col, optional=TRUE, tryFormats=c("%d/%m/%Y"));
++  if (all(!is.na(tmp)))
++  return (tmp)
++ }
+> 
+> Mat <- matrix(c(1:3, letters[1:3], 1:3, LETTERS[1:3],
++ c("22/4/1969", "8/4/1971", "23/9/1973"),
++ c("22/4/1969 6:01", " 8/4/1971 7:23", "23/9/1973 8:45")),
++   3, 6)
+> foo <- tempfile()
+> write.table(Mat, foo, sep = ",", col.names = FALSE, row.names = FALSE)
+> read.table(foo, sep = ",", colConvert=probecol)
+  V1 V2 V3 V4 V5  V6
+1  1  a  1  A 1969-04-22 1969-04-22 06:01:00
+2  2  b  2  B 1971-04-08 1971-04-08 07:23:00
+3  3  c  3  C 1973-09-23 1973-09-23 08:45:00
+> unlist(sapply(.Last.value, class))
+   V1V2V3V4   V51   V52   V61   
V62 
+"integer"  "factor" "integer"  "factor" "POSIXlt"  "POSIXt" "POSIXlt"  
"POSIXt" 
+> unlink(foo)
+> 
+> 
 > ## write.table with complex columns (PR#7260, in part)
 > write.table(data.frame(x = 0.5+1:4, y = 1:4 + 1.5i), file = "")
 "x" "y"
-- 
1.8.5.rc3

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


[Rd] [PATCH 1/2] readtable: add hook for type conversions per column

2019-03-25 Thread Kurt Van Dijck
This commit adds a function parameter to readtable. The function is called
for every column.
The goal is to allow specific (non-standard) type conversions depending on the 
input.
When the parameter is not given, or the function returns NULL, the legacy 
default applies.
The colClasses parameter still takes precedence, i.e. the colConvertFn only 
applies to
the default conversions.
This allows to properly load a .csv with timestamps expressed in the (quite 
common) %d/%m/%y %H:%M format,
which was impossible since overruling as.POSIXlt makes a copy in the users 
namespace, and
read.table would still take the base version of as.POSIXlt.
Rather than fixing my specific requirement, this hook allows to probe for any 
custom format
and do smart things with little syntax.

Signed-off-by: Kurt Van Dijck 
---
 src/library/utils/R/readtable.R | 12 +++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/src/library/utils/R/readtable.R b/src/library/utils/R/readtable.R
index 238542e..076a707 100644
--- a/src/library/utils/R/readtable.R
+++ b/src/library/utils/R/readtable.R
@@ -65,6 +65,7 @@ function(file, header = FALSE, sep = "", quote = "\"'", dec = 
".",
  strip.white = FALSE, blank.lines.skip = TRUE,
  comment.char = "#", allowEscapes = FALSE, flush = FALSE,
  stringsAsFactors = default.stringsAsFactors(),
+ colConvert = NULL,
  fileEncoding = "", encoding = "unknown", text, skipNul = FALSE)
 {
 if (missing(file) && !missing(text)) {
@@ -226,9 +227,18 @@ function(file, header = FALSE, sep = "", quote = "\"'", 
dec = ".",
 if(rlabp) do[1L] <- FALSE # don't convert "row.names"
 for (i in (1L:cols)[do]) {
 data[[i]] <-
-if (is.na(colClasses[i]))
+if (is.na(colClasses[i])) {
+tmp <- NULL
+if (!is.null(colConvert))
+# attempt to convert from user provided hook
+tmp <- colConvert(data[[i]])
+if (!is.null(tmp))
+(tmp)
+else
+# fallback, default
 type.convert(data[[i]], as.is = as.is[i], dec=dec,
 numerals=numerals, na.strings = character(0L))
+}
 ## as na.strings have already been converted to 
 else if (colClasses[i] == "factor") as.factor(data[[i]])
 else if (colClasses[i] == "Date") as.Date(data[[i]])
-- 
1.8.5.rc3

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


[Rd] R 3.5.3 having trouble spawning a new process on my Windows 10 machine

2019-03-25 Thread Sam Albers
Hi all,

I am noticing some strange behaviour so I am bringing to this list. In the
past when I have submitted bugs to bugzilla, I have come here first for
confirmation/advice. Hopefully this is appropriate.

Upgrading from R 3.5.2 to R 3.5.3 seems to have elicited some strange
behaviour on my Windows machine. R seems to have trouble spawning a new
process on my machine. You can noticing with all sort of packages, like
devtools, which try to spawn new R processes. Here is the replication of
the beahviour:



## On R 3.5.3
Open a command prompt:

H:\>R
'"C:\PROGRA~1\R\R-35~1.3/bin/x64/Rterm.exe"' is not recognized as an
internal or external command,
operable program or batch file.

H:\>Rscript -e "sessionInfo()"
R version 3.5.3 (2019-03-11)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17134)

Matrix products: default

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

loaded via a namespace (and not attached):
[1] compiler_3.5.3



## On 3.5.2
H:\>R

R version 3.5.2 (2018-12-20) -- "Eggshell Igloo"
Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> q()
Save workspace image? [y/n/c]: n

H:\>Rscript -e "sessionInfo()"
R version 3.5.2 (2018-12-20)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17134)

Matrix products: default

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

loaded via a namespace (and not attached):
[1] compiler_3.5.2



## Considerations
- I have manually removed both my .Renviron and .Rprofile files just to
ensure those weren't muddling up anything.
- I have my PATH set like this "C:\Program Files\R\R-3.5.3\bin"  for 3.5.3
and "C:\Program Files\R\R-3.5.2\bin" for 3.5.2
- R 3.5.3 works final if I just open the default console that ships with R.
But then anytime I try to launch a new process I get the above error.

A similar issue has been raised up before
https://stat.ethz.ch/pipermail/r-devel/2017-September/074921.html
https://stat.ethz.ch/pipermail/r-devel/2015-September/071714.html

Am I missing something obvious here? Many thanks in advance for taking a
look.

Regards,

Sam

[[alternative HTML version deleted]]

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


Re: [Rd] R 3.5.3 having trouble spawning a new process on my Windows 10 machine

2019-03-25 Thread Joris Meys
Hi Sam,

Is your PATH set on your user environment variables or on the system?
Did you try to remove the entry for R-3.5.2 ?

I have tested with a standard install, and I can't reproduce your error. I
have the exact same path in my system PATH environment variable for 3.5.3,
and nothing for other versions.

Sorry I can't be of any more help.
Cheers
Joris

On Mon, Mar 25, 2019 at 4:54 PM Sam Albers  wrote:

> Hi all,
>
> I am noticing some strange behaviour so I am bringing to this list. In the
> past when I have submitted bugs to bugzilla, I have come here first for
> confirmation/advice. Hopefully this is appropriate.
>
> Upgrading from R 3.5.2 to R 3.5.3 seems to have elicited some strange
> behaviour on my Windows machine. R seems to have trouble spawning a new
> process on my machine. You can noticing with all sort of packages, like
> devtools, which try to spawn new R processes. Here is the replication of
> the beahviour:
>
>
>
> ## On R 3.5.3
> Open a command prompt:
>
> H:\>R
> '"C:\PROGRA~1\R\R-35~1.3/bin/x64/Rterm.exe"' is not recognized as an
> internal or external command,
> operable program or batch file.
>
> H:\>Rscript -e "sessionInfo()"
> R version 3.5.3 (2019-03-11)
> Platform: x86_64-w64-mingw32/x64 (64-bit)
> Running under: Windows 10 x64 (build 17134)
>
> Matrix products: default
>
> 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
>
> loaded via a namespace (and not attached):
> [1] compiler_3.5.3
>
>
>
> ## On 3.5.2
> H:\>R
>
> R version 3.5.2 (2018-12-20) -- "Eggshell Igloo"
> Copyright (C) 2018 The R Foundation for Statistical Computing
> Platform: x86_64-w64-mingw32/x64 (64-bit)
>
> R is free software and comes with ABSOLUTELY NO WARRANTY.
> You are welcome to redistribute it under certain conditions.
> Type 'license()' or 'licence()' for distribution details.
>
>   Natural language support but running in an English locale
>
> R is a collaborative project with many contributors.
> Type 'contributors()' for more information and
> 'citation()' on how to cite R or R packages in publications.
>
> Type 'demo()' for some demos, 'help()' for on-line help, or
> 'help.start()' for an HTML browser interface to help.
> Type 'q()' to quit R.
>
> > q()
> Save workspace image? [y/n/c]: n
>
> H:\>Rscript -e "sessionInfo()"
> R version 3.5.2 (2018-12-20)
> Platform: x86_64-w64-mingw32/x64 (64-bit)
> Running under: Windows 10 x64 (build 17134)
>
> Matrix products: default
>
> 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
>
> loaded via a namespace (and not attached):
> [1] compiler_3.5.2
>
>
>
> ## Considerations
> - I have manually removed both my .Renviron and .Rprofile files just to
> ensure those weren't muddling up anything.
> - I have my PATH set like this "C:\Program Files\R\R-3.5.3\bin"  for 3.5.3
> and "C:\Program Files\R\R-3.5.2\bin" for 3.5.2
> - R 3.5.3 works final if I just open the default console that ships with R.
> But then anytime I try to launch a new process I get the above error.
>
> A similar issue has been raised up before
> https://stat.ethz.ch/pipermail/r-devel/2017-September/074921.html
> https://stat.ethz.ch/pipermail/r-devel/2015-September/071714.html
>
> Am I missing something obvious here? Many thanks in advance for taking a
> look.
>
> Regards,
>
> Sam
>
> [[alternative HTML version deleted]]
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>


-- 
Joris Meys
Statistical consultant

Department of Data Analysis and Mathematical Modelling
Ghent University
Coupure Links 653, B-9000 Gent (Belgium)


---
Biowiskundedagen 2018-2019
http://www.biowiskundedagen.ugent.be/

---
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php

[[alternative HTML version deleted]]

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


Re: [Rd] Discrepancy between is.list() and is(x, "list")

2019-03-25 Thread Abs Spurdle
> I have noticed a discrepancy between is.list() and is(x, “list”)

There's a similar problem with inherits().

On R 3.5.3:

> f = function () 1
> class (f) = "f"

> is.function (f)
[1] TRUE
> inherits (f, "function")
[1] FALSE

I didn't check what happens with:
> class (f) = c ("f", "function")

However, they should have the same result, regardless.

> Is this discrepancy intentional?

I hope not.

[[alternative HTML version deleted]]

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


[Rd] bugs in head() and tail()

2019-03-25 Thread Abs Spurdle
(Using R 3.5.3).

I found bugs in head() and tail().

The following works:

> f = function () 1
> head (f)
1 function ()
2 1

However, the following does not:

> class (f) = "f"
> head (f)
Error in x[seq_len(n)] : object of type 'closure' is not subsettable

[[alternative HTML version deleted]]

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


Re: [Rd] bugs in head() and tail()

2019-03-25 Thread Gabriel Becker
Hi Abs,

This is because the class is "f", not c("f", "function") in your second
example. S3 method dispatch is doing what you (unintentionally, I presume)
asked it to do.

The S3 method which allows head to take functions is utils:::head.function.
S3 can only be expected to  understand inheritance if the value of class(f)
is multivalued. It doesn't have any way of knowing  f is still a function
after the class assignment because "function" does not appear anywhere in
the class vector, so instead of hitting utils:::head.function, it hits
utils:::head.default, which uses [ on the argument, causing the error.

I'd say this is "expected" behavior within the context of the S3 system.

I also see this behavior at least as far aback as 3.5.1, so its not new to
3.5.3.

Best,
~G

On Mon, Mar 25, 2019 at 8:44 PM Abs Spurdle  wrote:

> (Using R 3.5.3).
>
> I found bugs in head() and tail().
>
> The following works:
>
> > f = function () 1
> > head (f)
> 1 function ()
> 2 1
>
> However, the following does not:
>
> > class (f) = "f"
> > head (f)
> Error in x[seq_len(n)] : object of type 'closure' is not subsettable
>
> [[alternative HTML version deleted]]
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

[[alternative HTML version deleted]]

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