Re: [R] [R-devel] Source Code for function

2019-09-10 Thread David Winsemius



On 9/10/19 11:13 AM, Golden, Shelby wrote:

Wow, this is great! Thank you, Richard, for going so in-depth with this 
explanation.

My ultimate goal is to create a vector that stores logical evaluations to be 
used later in a function that subsets data. I’ve coded multiple projects, now, 
that require this kind of sub-setting across multiple unique logical 
evaluations. So, streamlining the in-put and the out-put is ideal to minimize 
error and generalize my code.

I will look further into this information to see if it can help me get around 
using parse(text = )), which, to my understanding, is not a good coding method 
(https://stackoverflow.com/questions/1743698/evaluate-expression-given-as-a-string
 - comment left Martin Maechler).



It appears you have in mind keeping around a set of "off-the-shelf" 
expressions that will be chosen for varying situations. It seems to me 
that you should instead be storing complex predefined logical 
constructions in functions that can be applied to multi-column objects 
like dataframes, data.tables, or matrices:



> A_is_GT_B <- function(A, B){ A > B}
> set.seed(123)
> test <- data.frame(x=sample(10),y=sample(10), z=sample(10) )
> test$x_GT_y <- with(test, A_is_GT_B(x,y))
> test
    x  y  z x_GT_y
1   3 10  8  FALSE
2  10  5  7   TRUE
3   2  3  2  FALSE
4   8  8  1  FALSE
5   6  1  6   TRUE
6   9  4  3   TRUE
7   1  6  4  FALSE
8   7  9 10  FALSE
9   5  7  9  FALSE

10  4  2  5   TRUE

The expressions then are encapsulated inside the function and you can 
make lists that carry multiple functions.


It does remain possible to have expression vectors:

exprs <- expression( x > y, x >+ z)

eval( exprs[[2]], test)

> exprs <- expression( x > y, x >+ z)
>
> eval( exprs[[2]], test)
 [1] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE


This however is considered poor practice. Notice that you are 
constrained to know ahead of time the column names of the arguments to 
be passed, whereas with the function-approach, the names are not 
required and you can use positional matching.


--

David.



Thank you again, and I hope you have a wonderful week!

Shelby


From: Richard O'Keefe 
Date: Sunday, September 8, 2019 at 11:01 PM
To: "Golden, Shelby" 
Cc: "Richard M. Heiberger" , Bert Gunter , 
"r-help@R-project.org" , "Gillenwater, Lucas" 
Subject: Re: [R] [R-devel] Source Code for function


I am looking to understand why the keyword function can take a logical argument
(eg: x<4) and use that later inside the function's definition for logical 
evaluations

The "function" keyword does not take a logical argument.
Let me show you some parallels:

f <- function (x, y) {x+y}  # R
(set! f (lambda (x y) (+ x y))) ; Scheme
f = (x, y) => { return x+y; };  // Javascript
f = (x, y) => { return x+y; };  // C#, given a suitable declaration for f
f = (x, y) -> { return x+y; }   // Java, given a suitable declaration for f.
lambda x y; x+y end -> f;   // Pop-2, older than the others.

In all of these,
   - there is something ('function', 'lambda', '=>', '->') that
 says "here is an anonymous function"
   - there is a list of zero or more parameters
   - there is a body which may contain statements and may also
 return a result.
The keyword in itself does nothing.  The compiler recognises the
construction and generates code for a procedure that is bound to
the environment where it is created, so that it can find variables
other than those in its parameter list.

When it comes to passing parameters to a function, there is
nothing special about logical expressions in any of these languages.

Now there *is* something about functions in R that is special.
The S language (which R is based on) is the only one I am familiar
with that combines two properties:
  - it is an imperative language with side effects to variables
  - it does not evaluate function arguments when they are passed
but when they are first *used*.
An obvious reason for this is to allow plotting methods to construct
labels from their arguments and to allow model fitting methods to
remember the form of the model.

If you want argument evaluation delayed for any other reason, it is
probably better to pass a function.  See

?integrate

  -- the first argument is a function, not a general expression

?optim

  -- the second argument is a function, not a general expression
(That is, the argument in question is an expression whose value must
be a function, not an expression to be manipulated *textually* or as
a formula.)



On Sat, 7 Sep 2019 at 08:07, Golden, Shelby 
mailto:gold...@njhealth.org>> wrote:
Thank you all for your reply. I should clarify, that I am looking to understand 
why the keyword function can take a logical argument (eg: x<4) and use that 
later inside the function's definition for logical evaluations.

Consider this example, which is a simplification of 
getAnywhere(subset.data.frame):
x = data.frame("Col1" = c(1, 2, 3, 4, 5), "Col2" = c(6, 7, 8, 9, 10))
test 

[R] Calling a LAPACK subroutine from R

2019-09-10 Thread Giovanni Petris


Hello R-helpers!

I am trying to call a LAPACK subroutine directly from my R code using 
.Fortran(), but R cannot find the symbol name. How can I register/load the 
appropriate library?

> ### AR(1) Precision matrix
> n <- 4L
> phi <- 0.64
> AB <- matrix(0, 2, n)
> AB[1, ] <- c(1, rep(1 + phi^2, n-2), 1)
> AB[2, -n] <- -phi
> round(AB, 3)
  [,1]  [,2]  [,3] [,4]
[1,]  1.00  1.41  1.411
[2,] -0.64 -0.64 -0.640
> 
> ### Cholesky factor
> AB.ch <- .Fortran("dpbtrf", UPLO = 'L', N = as.integer(n),
+  KD = 1L, AB = AB, LDAB = 2L, INFO = as.integer(0))$AB
Error in .Fortran("dpbtrf", UPLO = "L", N = as.integer(n), KD = 1L, AB = AB,  : 
  Fortran symbol name "dpbtrf" not in load table
> sessionInfo()
R version 3.6.0 (2019-04-26)
Platform: x86_64-apple-darwin18.5.0 (64-bit)
Running under: macOS Mojave 10.14.6

Matrix products: default
BLAS/LAPACK: /usr/local/Cellar/openblas/0.3.6_1/lib/libopenblasp-r0.3.6.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

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

Thank you in advance for your help!

Best,
Giovanni Petris



--
Giovanni Petris, PhD
Professor
Director of Statistics
Department of Mathematical Sciences
University of Arkansas - Fayetteville, AR 72701


__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] [R-devel] Source Code for function

2019-09-10 Thread Golden, Shelby
Wow, this is great! Thank you, Richard, for going so in-depth with this 
explanation.

My ultimate goal is to create a vector that stores logical evaluations to be 
used later in a function that subsets data. I’ve coded multiple projects, now, 
that require this kind of sub-setting across multiple unique logical 
evaluations. So, streamlining the in-put and the out-put is ideal to minimize 
error and generalize my code.

I will look further into this information to see if it can help me get around 
using parse(text = )), which, to my understanding, is not a good coding method 
(https://stackoverflow.com/questions/1743698/evaluate-expression-given-as-a-string
 - comment left Martin Maechler).

Thank you again, and I hope you have a wonderful week!

Shelby


From: Richard O'Keefe 
Date: Sunday, September 8, 2019 at 11:01 PM
To: "Golden, Shelby" 
Cc: "Richard M. Heiberger" , Bert Gunter 
, "r-help@R-project.org" , 
"Gillenwater, Lucas" 
Subject: Re: [R] [R-devel] Source Code for function

> I am looking to understand why the keyword function can take a logical 
> argument
> (eg: x<4) and use that later inside the function's definition for logical 
> evaluations

The "function" keyword does not take a logical argument.
Let me show you some parallels:

f <- function (x, y) {x+y}  # R
(set! f (lambda (x y) (+ x y))) ; Scheme
f = (x, y) => { return x+y; };  // Javascript
f = (x, y) => { return x+y; };  // C#, given a suitable declaration for f
f = (x, y) -> { return x+y; }   // Java, given a suitable declaration for f.
lambda x y; x+y end -> f;   // Pop-2, older than the others.

In all of these,
  - there is something ('function', 'lambda', '=>', '->') that
says "here is an anonymous function"
  - there is a list of zero or more parameters
  - there is a body which may contain statements and may also
return a result.
The keyword in itself does nothing.  The compiler recognises the
construction and generates code for a procedure that is bound to
the environment where it is created, so that it can find variables
other than those in its parameter list.

When it comes to passing parameters to a function, there is
nothing special about logical expressions in any of these languages.

Now there *is* something about functions in R that is special.
The S language (which R is based on) is the only one I am familiar
with that combines two properties:
 - it is an imperative language with side effects to variables
 - it does not evaluate function arguments when they are passed
   but when they are first *used*.
An obvious reason for this is to allow plotting methods to construct
labels from their arguments and to allow model fitting methods to
remember the form of the model.

If you want argument evaluation delayed for any other reason, it is
probably better to pass a function.  See
> ?integrate
 -- the first argument is a function, not a general expression
> ?optim
 -- the second argument is a function, not a general expression
(That is, the argument in question is an expression whose value must
be a function, not an expression to be manipulated *textually* or as
a formula.)



On Sat, 7 Sep 2019 at 08:07, Golden, Shelby 
mailto:gold...@njhealth.org>> wrote:
Thank you all for your reply. I should clarify, that I am looking to understand 
why the keyword function can take a logical argument (eg: x<4) and use that 
later inside the function's definition for logical evaluations.

Consider this example, which is a simplification of 
getAnywhere(subset.data.frame):
x = data.frame("Col1" = c(1, 2, 3, 4, 5), "Col2" = c(6, 7, 8, 9, 10))
test <- function(x, logic){
e <- substitute(logic)
r <- eval(e, x, parent.frame())
r[r]
}


Shelby


On 9/6/19, 1:02 PM, "R-help on behalf of Richard M. Heiberger" 
mailto:r-help-boun...@r-project.org> on behalf of 
r...@temple.edu> wrote:

You might also want to look at the codetools package, for example the
showTree function " Prints a Lisp-style representation of R
expression."

> library(codetools)

> showTree(quote(x %*% x))
(%*% x x)
> showTree(quote(a+b))
(+ a b)
> showTree(quote(y ~ a+b))
(~ y (+ a b))

On Fri, Sep 6, 2019 at 2:30 PM Bert Gunter 
mailto:bgunter.4...@gmail.com>> wrote:
>
> The following may be of use (it gives the parse tree of the text):
>
> > z <- as.list(parse(text = "function(x)x %*% x"))
> > z[[1]]
> function(x) x %*% x
> > z[[c(1,1)]]
> `function`
> > z[[c(1,2)]]
> $x
> > z[[c(1,3)]]
> x %*% x
> > z[[c(1,3,1)]]
> `%*%`
> > z[[c(1,3,2)]]
> x
> > z[[c(1,3,3)]]
> x
>
>
> Bert Gunter
>
>
>
> On Fri, Sep 6, 2019 at 10:14 AM Wang Jiefei 
mailto:szwj...@gmail.com>> wrote:
>
> > If you are looking for an R code parser, I think the `parse` and `eval`
> > function might be a good start point. See the example below.
> >
> > > parse(t

Re: [R] missing NEWS.pdf

2019-09-10 Thread peter dalgaard
I rarely do a full install, but isn't there a "make docs" step, which you may 
have omitted?

-pd

> On 10 Sep 2019, at 11:51 , Jim Maas  wrote:
> 
> I usually compile my own version of R on Ubuntu, now 18.04 and it works
> fine. I recently tried to compile v 3.6.1 and it all worked until the end
> of the "make install" which did not complete because it said it was missing
> "NEWS.pdf".  Is this a bug or oversight, or have I done something wrong?
> Thanks
> J
> 
> -- 
> Jim Maas
> jimmaasuk  at gmail.com
> 
>   [[alternative HTML version deleted]]
> 
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd@cbs.dk  Priv: pda...@gmail.com

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] R_BATCH_OPTIONS not respected?

2019-09-10 Thread Marius Hofert
On Tue, Sep 10, 2019 at 12:38 PM Martin Maechler
 wrote:
>
> > Marius Hofert
> > on Mon, 9 Sep 2019 22:38:38 +0200 writes:
>
> > Hi,
> > I typically start R with "--no-restore --no-save" (to avoid .RData
> > files being written) and would like to have the same behavior under 'R
> > CMD BATCH'. I use R_BATCH_OPTIONS="--no-restore --no-save" in my
> > ~/.Renviron but running an R script with 'R CMD BATCH' still produces
> > a .RData file. What's the correct way of getting the '--no-restore
> > --no-save' options when in batch mode?
>
> > (This is on macOS 10.14.6 with R version 3.6.1)
>
> Maybe macOS is the problem?
>
> It works fine on Linux:
>
> export R_BATCH_OPTIONS='--no-save --no-restore'
> R CMD BATCH .R

Hoi Martin,

Thanks for helping. This also works for me, but not if I put
R_BATCH_OPTIONS="--no-restore --no-save" in ~/.Renviron.
If I have an R script called MWE.R containing
print(Sys.getenv("R_BATCH_OPTIONS")), I correctly see "--no-restore
--no-save"
being printed to .Rout, but still obtain .RData. (I also think this is
a macOS problem, but couldn't figure it out yet).

Cheers,
M

>
> produces  .Rout and nothing else  for me
>
>
> Martin
>
> > Thanks & cheers,
> > M
>
> > __
> > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] R_BATCH_OPTIONS not respected?

2019-09-10 Thread Martin Maechler
> Marius Hofert 
> on Mon, 9 Sep 2019 22:38:38 +0200 writes:

> Hi,
> I typically start R with "--no-restore --no-save" (to avoid .RData
> files being written) and would like to have the same behavior under 'R
> CMD BATCH'. I use R_BATCH_OPTIONS="--no-restore --no-save" in my
> ~/.Renviron but running an R script with 'R CMD BATCH' still produces
> a .RData file. What's the correct way of getting the '--no-restore
> --no-save' options when in batch mode?

> (This is on macOS 10.14.6 with R version 3.6.1)

Maybe macOS is the problem?

It works fine on Linux:

export R_BATCH_OPTIONS='--no-save --no-restore'
R CMD BATCH .R 

produces  .Rout and nothing else  for me


Martin

> Thanks & cheers,
> M

> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] missing NEWS.pdf

2019-09-10 Thread Jim Maas
I usually compile my own version of R on Ubuntu, now 18.04 and it works
fine. I recently tried to compile v 3.6.1 and it all worked until the end
of the "make install" which did not complete because it said it was missing
"NEWS.pdf".  Is this a bug or oversight, or have I done something wrong?
Thanks
J

-- 
Jim Maas
jimmaasuk  at gmail.com

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] [R-pkgs] New package ‘HBV.IANIGLA’ for hydrological modeling

2019-09-10 Thread Ezequiel Toum via R-packages



Hi, 




a new package ‘HBV.IANIGLA’ for hydrological modeling has been relesased. 

CRAN: [ https://cran.r-project.org/web/packages/HBV.IANIGLA/index.html | 
https://cran.r-project.org/web/packages/HBV.IANIGLA/index.html ] 


In case of doubts do not hesitate to write. 




Best, 

Ezequiel Toum 
-- 
Ezequiel Toum 
Ingeniero Civil 
Becario Doctoral 
https://www.mendoza-conicet.gob.ar/portal/ianigla/paginas/index/integrantes87 

Instituto Argentino de Nivología, 
Glaciología y Ciencias Ambientales (IANIGLA) 
CCT-CONICET-Mendoza 
C.C. 330, (5500) Mendoza, Argentina 
Tel: 54(261)5244266 
Fax: 54(261)5244201 

[[alternative HTML version deleted]]

___
R-packages mailing list
r-packa...@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-packages

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.