Re: [R] installation: while running make, unable to run pdflatex on 'NEWS.tex'

2024-03-02 Thread Benjamin Tyner

Thank you Ivan and Richard...

Short version: a simple "sudo apt install texlive" fixed it.

Longer version (attempt at a post-mortem): previously (back on 
2023-11-05) on this same system I built R version 4.3.2; and 
/home/btyner/R432/lib/R/doc/NEWS.pdf does exist (and is a valid pdf with 
48 pages) from that same install. Likewise before that on 2023-07-02 I 
built R version 4.3.1 and the NEWS.pdf is fine (47 pages) there too.


Still not sure how it happened that my system lost track of the 
necessary texlive add-ons at some point between 2023-11-05 and now, but 
running "sudo apt install texlive" made it clear that 
texlive-latex-recommended was indeed one of the culprits as suggested by 
Richard.


   The following additional packages will be installed:
  fonts-texgyre tex-gyre texlive-fonts-recommended
   texlive-latex-recommended tipa
   Suggested packages:
  texlive-fonts-recommended-doc texlive-latex-recommended-doc
   texlive-luatex texlive-pstricks tipa-doc
   The following NEW packages will be installed:
  fonts-texgyre tex-gyre texlive texlive-fonts-recommended
   texlive-latex-recommended tipa

In an attempt to figure out what might have happened after 2023-11-05,

   zgrep texlive /var/log/dpkg.log.*.gz

revealed some texlive related activity back on 2023-11-23 but no 
indication that anything was removed...


   /var/log/dpkg.log.4.gz:2023-11-23 11:02:55 install
   texlive-binaries:amd64  2021.20210626.59705-1ubuntu0.1
   /var/log/dpkg.log.4.gz:2023-11-23 11:02:55 status half-installed
   texlive-binaries:amd64 2021.20210626.59705-1ubuntu0.1
   /var/log/dpkg.log.4.gz:2023-11-23 11:02:56 status unpacked
   texlive-binaries:amd64 2021.20210626.59705-1ubuntu0.1
   /var/log/dpkg.log.4.gz:2023-11-23 11:02:56 install texlive-base:all
2021.20220204-1
   /var/log/dpkg.log.4.gz:2023-11-23 11:02:56 status half-installed
   texlive-base:all 2021.20220204-1
   /var/log/dpkg.log.4.gz:2023-11-23 11:03:02 status unpacked
   texlive-base:all 2021.20220204-1
   /var/log/dpkg.log.4.gz:2023-11-23 11:03:02 install
   texlive-latex-base:all  2021.20220204-1
   /var/log/dpkg.log.4.gz:2023-11-23 11:03:02 status half-installed
   texlive-latex-base:all 2021.20220204-1
   /var/log/dpkg.log.4.gz:2023-11-23 11:03:04 status unpacked
   texlive-latex-base:all 2021.20220204-1
   /var/log/dpkg.log.4.gz:2023-11-23 11:03:05 configure
   texlive-binaries:amd64 2021.20210626.59705-1ubuntu0.1 
   /var/log/dpkg.log.4.gz:2023-11-23 11:03:05 status unpacked
   texlive-binaries:amd64 2021.20210626.59705-1ubuntu0.1
   /var/log/dpkg.log.4.gz:2023-11-23 11:03:05 status half-configured
   texlive-binaries:amd64 2021.20210626.59705-1ubuntu0.1
   /var/log/dpkg.log.4.gz:2023-11-23 11:03:05 status installed
   texlive-binaries:amd64 2021.20210626.59705-1ubuntu0.1
   /var/log/dpkg.log.4.gz:2023-11-23 11:03:05 configure
   texlive-base:all 2021.20220204-1 
   /var/log/dpkg.log.4.gz:2023-11-23 11:03:05 status unpacked
   texlive-base:all 2021.20220204-1
   /var/log/dpkg.log.4.gz:2023-11-23 11:03:05 status half-configured
   texlive-base:all 2021.20220204-1
   /var/log/dpkg.log.4.gz:2023-11-23 11:03:09 status installed
   texlive-base:all 2021.20220204-1
   /var/log/dpkg.log.4.gz:2023-11-23 11:03:09 configure
   texlive-latex-base:all 2021.20220204-1 
   /var/log/dpkg.log.4.gz:2023-11-23 11:03:09 status unpacked
   texlive-latex-base:all 2021.20220204-1
   /var/log/dpkg.log.4.gz:2023-11-23 11:03:09 status half-configured
   texlive-latex-base:all 2021.20220204-1
   /var/log/dpkg.log.4.gz:2023-11-23 11:03:09 status installed
   texlive-latex-base:all 2021.20220204-1

One more thing:

   HISTTIMEFORMAT="%Y-%m-%d %T " history | grep texlive

revealed that I ran "sudo apt install texlive-latex-base" back on 
2024-02-19, so maybe that had something to do with it. Sadly the shell 
history does not exist prior to that date.



On 3/2/24 02:42, Ivan Krylov wrote:

В Fri, 1 Mar 2024 10:46:53 -0500
Benjamin Tyner  пишет:


my platform info:

 Platform: x86_64-pc-linux-gnu (64-bit)
 Running under: Ubuntu 22.04.4 LTS

Quick things first: have you installed all the build-dependencies? apt
build-dep r-base (maybe with --install-suggests? haven't tried that in
a while) should do that. In particular, the build dependencies include
texlive-fonts-extra and texlive-latex-extra. That's a lot of packages,
but some of them are required to compile LaTeX documents produced by R.


 you should 'make docs' now ...
 make[1]: Entering directory '/home/btyner/R-4.3.3/doc'
 creating doc/NEWS
 creating doc/NEWS.pdf
 Error in texi2dvi(file = file, pdf = TRUE, clean = clean, quiet =
 quiet, :
 unable to run pdflatex on 'NEWS.tex'
 LaTeX errors:
 ! LaTeX Error: File `pdftexcmds.sty' not found.

Can you compile a minimal LaTeX document? Something like:

\documentclass{article}
\begin{document}
\[ \mathbf{y} = \mathbf{X} \beta \]
\

[R] dput(..., file = stderr())

2024-03-01 Thread Benjamin Tyner
Curious to know if this warning is expected behavior, and if so, what is 
the recommended way instead:


> dput(letters, file = stderr())
   c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
   "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y",
   "z")
   Warning message:
   In dput(letters, file = stderr()) : wrote too few characters

(I ask because it doesn't happen when using file = stdout())

__
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] installation: while running make, unable to run pdflatex on 'NEWS.tex'

2024-03-01 Thread Benjamin Tyner



A kind member of R-core suggested this is due to a misconfiguration on 
my system, and to post it to the mailing list for troubleshooting.


When trying to build R version 4.3.3, in at least two places during the 
process it gives LaTeX errors of the form:


(example 1)

   you should 'make docs' now ...
   make[1]: Entering directory '/home/btyner/R-4.3.3/doc'
   creating doc/NEWS
   creating doc/NEWS.pdf
   Error in texi2dvi(file = file, pdf = TRUE, clean = clean, quiet =
   quiet, :
   unable to run pdflatex on 'NEWS.tex'
   LaTeX errors:
   ! LaTeX Error: File `pdftexcmds.sty' not found.

   Type X to quit or  to proceed,
   or enter new name. (Default extension: sty)

   ! Emergency stop.
   

   l.108 RequirePackage{pdftexcmds}[2018/09/10]
   ^^M
   ! ==> Fatal error occurred, no output PDF file produced!
   Calls:  -> texi2pdf -> texi2dvi
   Execution halted
   make[1]: /*/ [Makefile:74: NEWS.pdf] Error 1
   make[1]: Leaving directory '/home/btyner/R-4.3.3/doc'
   make: [Makefile:73: docs] Error 2 (ignored)

(example 2)

   make[1]: Entering directory '/home/btyner/R-4.3.3/src/library'
   building/updating vignettes for package 'grid' ...
   building/updating vignettes for package 'parallel' ...
   building/updating vignettes for package 'utils' ...
   building/updating vignettes for package 'stats' ...
   processing 'reshape.Rnw'
   Error: compiling TeX file 'reshape.tex' failed with message:
   unable to run pdflatex on 'reshape.tex'
   LaTeX errors:
   ! LaTeX Error: File `pdftexcmds.sty' not found.

   Type X to quit or  to proceed,
   or enter new name. (Default extension: sty)

   ! Emergency stop.
   

   l.108 RequirePackage{pdftexcmds}[2018/09/10]
   ^^M
   ! ==> Fatal error occurred, no output PDF file produced!
   Execution halted
   make[1]: ** [Makefile:103: vignettes] Error 1
   make[1]: Leaving directory '/home/btyner/R-4.3.3/src/library'
   make: ** [Makefile:81: vignettes] Error 2

here is my |pdflatex --version| info:

   pdfTeX 3.141592653-2.6-1.40.22 (TeX Live 2022/dev/Debian)
   kpathsea version 6.3.4/dev
   Compiled with libpng 1.6.37; using libpng 1.6.37
   Compiled with zlib 1.2.11; using zlib 1.2.11
   Compiled with xpdf version 4.03

my platform info:

   Platform: x86_64-pc-linux-gnu (64-bit)
   Running under: Ubuntu 22.04.4 LTS

I installed from this file with corresponding md5sum

   R-4.3.3.tar.xz 5602f5996107c346dba12a16e866d2e2

The specific commands I ran which led to the error were:

   wget https://cran.r-project.org/src/base/R-4/R-4.3.3.tar.xz
   mkdir R433
   tar xJf R-4.3.3.tar.xz
   cd R-4.3.3
   ./configure --prefix=/home/btyner/R433
   make

Happy to provide any additional information needed.

__
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] .BaseNamespaceEnv locking in non-interactive mode

2022-11-23 Thread Benjamin Tyner

Greetings,

Noticed that starting with R version 4.1.0, the base environment is 
locked by default. So for example in interactive mode, we have


> .BaseNamespaceEnv$foo <- "bar"
Error in .BaseNamespaceEnv$foo <- "bar" :
  cannot add binding of 'foo' to the base environment

However it appears that when operating in non-interactive mode, the 
locking is not actually enforced:


$ ~/R422/bin/Rscript -e "environmentIsLocked(.BaseNamespaceEnv)"
[1] TRUE

$ ~/R422/bin/Rscript -e ".BaseNamespaceEnv$foo <- 'bar'; 
.BaseNamespaceEnv$foo"

[1] "bar"

Just wondering if this is intentional, and if not, should I file a bug 
report?


Regards,

Ben

__
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] chaining closure arguments on-the-fly

2020-06-21 Thread Benjamin Tyner

On 6/20/20 5:04 PM, Duncan Murdoch wrote:
I think you effectively did that in your original post (all but 
encapsulating the expression in a function), so yes, it's possible. 
However, it's a really bad idea.  Why use non-standard evaluation when 
standard evaluation is fine?  Standard evaluation follows some well 
defined rules, and is easy to reason about.  NSE follows whatever 
rules it wants, so it's really hard for users to follow.  For example, 
assuming you had the g() you want, what would this give?


z <- 3
f(x = z, y = g(z))

You can't possibly know that without knowing whether there's a local 
variable in f named z that is created before y is evaluated.


Duncan Murdoch


Very good point, and I agree it's best to use standard evaluation 
whenever possible. The role of g would essentially be to modify one or 
more elements of f's formals in-place. For example calling:


   f(x = 3, y = g(expr))

would be equivalent to calling a function fm:

   fm(x = 3)

where the body of fm is identical to that of f, but:

> formals(fm)
   $x


   $y
   expr

though I expect g would be non-trivial to code up robustly.

__
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] chaining closure arguments on-the-fly

2020-06-20 Thread Benjamin Tyner



On 6/20/20 5:49 PM, Bert Gunter wrote:

Gents:
(with trepidation)

f(x = 3, y = g(expr))
**already** evaluates g in the environment of f, **not** in the 
environment of the caller.
(This does not contradict Duncan's example -- 3 is a constant, not a 
variable).


e.g.
> f <- function(x = 3, y = x^2 +k){
+     k <- 3
+     x + y
+ }

Ergo
> k <- 100; x <- 10
> f()
[1] 15
> f(0)
[1] 3
> x
[1] 10

This is all due to lazy evaluation where default arguments are 
evaluated in the function's environment (using standard evaluation). 
Arguments supplied in the call are evaluated in the caller's 
environment, so:


> f(x = x)
[1] 113

Am I missing something here?

Cheers,

Bert Gunter

"The trouble with having an open mind is that people keep coming along 
and sticking things into it."

-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

Default arguments are indeed evaluated in f's environment, but not 
supplied arguments. I haven't really thought about the semantics of 'g' 
with respect to default arguments. But certainly, lazy evaluation is key 
here.


Ben (with trepidation as well)

__
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] chaining closure arguments on-the-fly

2020-06-20 Thread Benjamin Tyner

On 6/20/20 9:00 AM, Duncan Murdoch wrote:

How about

g <- function(x, y = x) {
  f(x, y)
}
g(x = 3)

or even

yEqualsX <- function(f) function(x, y = x) f(x, y)

yEqualsX(f)(x = 3)

These are a lot like currying, but aren't currying, so they may be 
acceptable to you.  Personally I'd choose the first one.


Duncan Murdoch

Thank you Duncan; I should have been more explicit that I was also 
trying to avoid defining any new functions, but yes, it's hard to argue 
with the wrapper approach as a longstanding best practice.


Basically I'm wondering if it would be theoretically possible to 
construct a function "g" (possibly it would have to be 
primitive/internal) such that


   f(x = 3, y = g(expr))

would evaluate expr in the evaluation environment of f? After rereading 
section 4.3.3 of the R Language Definition, it's clear that supplied 
arguments are evaluated in the calling environment; though trickery in 
f()'s body may be used to evaluate elsewhere, such options seem limited 
from within the argument list itself.


Ben

__
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] chaining closure arguments on-the-fly

2020-06-20 Thread Benjamin Tyner

Greetings,

Occasionally, I desire to call a function with one argument set to equal 
to another. Here is a toy example:


   f <- function(x, y) {

       x + y
   }

   f(x = 3, y = x) # Error in f(x = 3, y = x) : object 'x' not found

So far, the most concise way I found to accomplish this is:

   f(x = 3, y = local(sys.frame(1)$x)) # evaluates to 6

but I dislike this solution because local() creates a new environment. 
Surely there must be a better way?


Note: I'm not interested in solutions that require modifying or currying f.

Regards,
Ben

__
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] safest way to subset (and replace) a data.table

2019-12-07 Thread Benjamin Tyner

Hi

I would like to replace a data.table 'DT' with a subset of itself, where 
the subset is determined by an expression 'expr' which evaluates to 
logical. Thus far I've been using:


   DT <- DT[expr, ]

however this frequently results in a SIGABRT from glibc of the form:

   "double free or corruption (!prev): 0xc08662b40"

I've tried under the latest version of R and the latest version of data 
table, but the issue still persists. So now I'm wondering if I'm doing 
it the wrong way.


Any advice?

Regards

Ben

__
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] Matrix::bdiag doesn't like being given a single named argument

2019-05-31 Thread Benjamin Tyner

Hello,

Perhaps not a bug, but interesting because the error only happens when 
there is a single named argument.


   > m <- matrix(1, 1, 1)
   > library(Matrix)
   > bdiag(m)
   1 x 1 sparse Matrix of class "dgCMatrix"

   [1,] 1
   > bdiag(a = m)
   Error in is.list(...) : supplied argument name 'a' does not match 'x'
   > bdiag(a = m, b = m)
   2 x 2 sparse Matrix of class "dgCMatrix"

   [1,] 1 .
   [2,] . 1

Moreover, this works fine:

   > bdiag(list(a = m))
   1 x 1 sparse Matrix of class "dgCMatrix"

   [1,] 1

Thoughts?

Regards

Ben

__
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] undo compile? (or: remove bytecode from closure)

2018-07-16 Thread Benjamin Tyner

Thanks Rui and Duncan, this is most helpful.


On 07/16/2018 06:58 AM, Duncan Murdoch wrote:

On 16/07/2018 5:31 AM, Rui Barradas wrote:

Hello,

Maybe the following is not the recommended way but it works
(and I believe makes sense).


f <- function(){}
formals(f) <- formals(fc)
body(f) <- body(fc)


That's not quite right:  it might lose the environment of fc, if it 
isn't the environment where this took place.  But a simpler solution 
is just


f <- fc
body(f) <- body(f)

because any assignment to the body of a function causes the bytecode 
to be dropped.


Both of our approaches will also cause the source references to be 
dropped.  If you want to save those, you need more steps:


f <- fc
body(f) <- body(f)
attr(f, "srcref") <- getSrcref(fc)

Duncan Murdoch



f
#function (x)
#{
#  x <- x + 1
#  pi * x
#}

f(1)
#[1] 6.283185


Hope this helps,

Rui Barradas

Às 03:25 de 16-07-2018, Benjamin Tyner escreveu:

Hi

Given a closure which has been compiled, what's the recommended way to
recover the original? For example,

      > f <- function(x) x+1
      > fc <- cmpfun(f)
      > rm(f)
      > fc
      function(x) x+1
      

what's the best way to recover f from fc ?

Regards

Ben

__
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-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] undo compile? (or: remove bytecode from closure)

2018-07-15 Thread Benjamin Tyner

Hi

Given a closure which has been compiled, what's the recommended way to 
recover the original? For example,


    > f <- function(x) x+1
    > fc <- cmpfun(f)
    > rm(f)
    > fc
    function(x) x+1
    

what's the best way to recover f from fc ?

Regards

Ben

__
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] system() or pipe(..., open = "r") without child process?

2018-05-22 Thread Benjamin Tyner

Greetings

On linux, is it possible to invoke an OS command from within R without 
spawning a child process? If not, is it possible to avoid copying the 
"parts of the caller's context" that are mentioned on the clone manpage?


 ENOMEM Cannot  allocate  sufficient memory to allocate a task structure
  for the child, or to copy those parts of  the caller's  
context

  that need to be copied.

(I am trying to avoid that ENOMEM condition when calling system(), 
pipe(), etc).


Regards,

Ben

__
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] rJava garbage collect

2018-02-06 Thread Benjamin Tyner

Hi Martin,

Thanks for providing the reference.

In this particular case, it helped me to discover that 13 JVM threads 
were garbage collecting in parallel, occasionally resulting in a race 
condition. Setting


    options(java.parameters = "-XS:ParallelGCThreads=1")

appears to resolve the issue.

Regards

Ben



On 6 February 2018 at 04:34, Benjamin Tyner https://stat.ethz.ch/mailman/listinfo/r-help>> wrote:
>/Hi />//>/Does rJava offer a way to instruct the JVM to perform a garbage 
collection? /

Do you really, really need to run the garbage collector?

Consider reading:
https://stackoverflow.com/questions/5086800/java-garbage-collection

Regards
Martin



__
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] rJava garbage collect

2018-02-06 Thread Benjamin Tyner

Thanks Jeff; indeed it works:

   .jcall("java/lang/System", method = "gc")


On 02/05/2018 11:53 PM, Jeff Newmiller wrote:

rJava offers a mechanism to call arbitrary methods in Java. Wouldn't you use 
that mechanism to call whatever you would call if you were programming in Java 
(e.g. System.gc)?


__
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] rJava garbage collect

2018-02-05 Thread Benjamin Tyner

Hi

Does rJava offer a way to instruct the JVM to perform a garbage collection?

Regards

Ben

__
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] setSessionLimit

2018-01-19 Thread Benjamin Tyner

Hello

The doc/NEWS.2 file mentions a setSessionLimit function, added with 
version 2.8.0


    o   setTimeLimit() function to set limits on the CPU
    and/or elapsed time for each top-level computation, and
    setSessionLimit() to set limits for the rest of the session.

However, I no longer see this function in recent versions of R, and 
there is no mention of its removal in the NEWS nor in the svn log. So 
I'm curious to learn why it was removed...does anyone recall?


Regards

Ben

__
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] read.table(..., header == FALSE, colClasses = )

2017-10-24 Thread Benjamin Tyner
Yes, it makes sense now; lesson learned. Thank you both! Sometimes it 
seems that no matter how good the documentation, some useR will 
inevitably (ab)use the code in ways that were never intended by the 
authors. Then when the code and/or documentation changes, it is not 
always obvious to the useR whether the intent of the authors has 
changed, or whether the useR had just been "getting the right answer for 
the wrong reason" all along. In this particular case, the change was 
documented as stemming from a "new feature" (as opposed to a bugfix or 
more stringent argument checking) that might appear to be a non fully 
backwards compatible change. For example one might have the (apparently) 
bad habit of using col.names as a shortcut to rename headers on-the-fly ...


   > getRversion()
   [1] ‘3.2.2’

   > read.table(textConnection("x y\na 3.14"), header = TRUE, 
colClasses = c(x = "character", y = "numeric"), col.names = c("foo", "bar"))

     foo  bar
   1   a 3.14

but indeed, the names attribute has zero effect on the result:

   > read.table(textConnection("x y\na 3.14"), header = TRUE, 
colClasses = c(y = "character", x = "numeric"), col.names = c("foo", "bar"))

     foo  bar
   1   a 3.14

so I agree it is good that we are checking for that now.

Regards
Ben

On 10/24/2017 08:55 AM, Martin Maechler wrote:

Benjamin Tyner 
 on Tue, 24 Oct 2017 07:21:33 -0400 writes:

 > Jeff,
 > Thank you for your reply. The intent was to construct a minimum
 > reproducible example. The same warning occurs when the 'file' argument
 > points to a file on disk with a million lines. But you are correct, my
 > example was slightly malformed and in fact gives an error under R
 > version 3.2.2. Please allow me to try again; in older versions of R,

 >    > read.table(file = textConnection("a\t3.14"), header = FALSE,
 > colClasses = c(x = "character", y = "numeric"), sep="\t")
 >      V1   V2
 >    1  a 3.14

 > (with no warning). As of version 3.3.0,

 >    > read.table(file = textConnection("a\t3.14"), header = FALSE,
 > colClasses = c(x = "character", y = "numeric"), sep="\t")
 >      V1   V2
 >    1  a 3.14
 >    Warning message:
 >    In read.table(file = textConnection("a\t3.14"), header = FALSE,  :
 >      not all columns named in 'colClasses' exist

 > My intent was not to complain but rather to learn more about best
 > practices regarding the names attribute.

which is a nice attitude, thank you.

An even shorter MRE (as header=FALSE is default, and the default
sep="" works, too):


tt <- read.table(textConnection("a 3.14"), colClasses = c(x="character", 
y="numeric"))

Warning message:
In read.table(file = textConnection("a 3.14"), colClasses = c(x = "character",  
:
   not all columns named in 'colClasses' exist
If you read in the help page -- you did read that before posting, did you?---
how 'colClasses' should be specified ,

 colClasses: character.  A vector of classes to be assumed for the
  columns.  If unnamed, recycled as necessary.  If named, names
  are matched with unspecified values being taken to be ‘NA’.

  Possible values are ..
  .

and the 'x' and 'y' names you used, are matched with the
colnames ... which on the other hand are "V1" and "V2"  for
you, and so you provoke a warning.

Once you have read (and understood) the above part of the help
page, it becomes, easy, no?


tt <- read.table(textConnection("a 3.14"), colClasses = 
c("character","numeric"))
t2 <- read.table(textConnection("a 3.14"), colClasses=c(x="character",y="numeric"), 
col.names=c("x","y"))
t2

   xy
1 a 3.14
i.e., no warning in both of these two cases.

So please, please, PLEASE: at least non-beginners like you *should*
take the effort to read the help page (and report if these seem
incomplete or otherwise improvable)...

Best,
Martin Maechler
ETH Zurich


__
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] read.table(..., header == FALSE, colClasses = )

2017-10-24 Thread Benjamin Tyner

Jeff,

Thank you for your reply. The intent was to construct a minimum 
reproducible example. The same warning occurs when the 'file' argument 
points to a file on disk with a million lines. But you are correct, my 
example was slightly malformed and in fact gives an error under R 
version 3.2.2. Please allow me to try again; in older versions of R,


   > read.table(file = textConnection("a\t3.14"), header = FALSE, 
colClasses = c(x = "character", y = "numeric"), sep="\t")

     V1   V2
   1  a 3.14

(with no warning). As of version 3.3.0,

   > read.table(file = textConnection("a\t3.14"), header = FALSE, 
colClasses = c(x = "character", y = "numeric"), sep="\t")

     V1   V2
   1  a 3.14
   Warning message:
   In read.table(file = textConnection("a\t3.14"), header = FALSE,  :
     not all columns named in 'colClasses' exist

My intent was not to complain but rather to learn more about best 
practices regarding the names attribute.


Regards

Ben



On 10/23/2017 08:51 PM, Jeff Newmiller wrote:

You are constructing the equivalent of a two-line data file, and complaining 
that it is not treating it like it was one line. If it did used to accept this 
silently [skeptical] then I for one am glad it produces a warning now.


__
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] read.table(..., header == FALSE, colClasses = )

2017-10-23 Thread Benjamin Tyner

Hello

I noticed that starting with R version 3.3.0 onward, this generates a 
warning:


   > txt <- c("a", "3.14")
   > read.table(file = textConnection(txt), header = FALSE, colClasses 
= c(x = "character", y = "numeric"))


the warning is "not all columns named in 'colClasses' exist" and I guess 
the change was made in response to this?


   https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=16478

Regardless, I am wondering whether this is desirable, that as a result 
of the change, the code has become stricter about the presence of a 
(formerly) harmless names attribute. Or am I missing something?


Regards

Ben

__
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] flushing on.exit prior to q()

2017-02-21 Thread Benjamin Tyner

Hi,

When using a custom error function that calls q(), what is the 
recommended way to "flush" the calling function's on.exit ?


For example, say I have a script:

   #!/usr/bin/Rscript --no-init-file

   options(error = function() {

  cat("on error message\n", file = stderr())

  q(save = "no", status = 1)
   })

   test <- function() {

   on.exit(cat("on exit message\n", file = stderr()))

   stop("error")
   }

   test()

when I run the script, the "on exit message" does not print. (I found 
one solution might involve adding a stop() to my custom error function 
prior to the q(); however this is somewhat less than ideal: it also 
triggers an "error during wrapup" because of the recursion).


Other ideas, best practices?

Thanks
Ben

__
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] [FORGED] file.exists() on device files

2017-01-13 Thread Benjamin Tyner

Thank you for the insights, Rolf and Henrik.

To give another example, this time in non-interactive mode,

   Rscript -e "file.exists(commandArgs(TRUE))" <(echo "Hi")

   [1] TRUE

versus

   Rscript -e "normalizePath(commandArgs(TRUE))" <(echo "Hi")
   [1] "/dev/fd/63"
   Warning message:
   In normalizePath(commandArgs(TRUE)) :
 path[1]="/dev/fd/63": No such file or directory

It almost seems like file.exists and normalizePath use separate criteria 
for determining existence?


Regards

Ben

On 01/12/2017 01:42 AM, Rolf Turner wrote:

On 12/01/17 16:33, Henrik Bengtsson wrote:




FYI, the /proc is there because Unix has something called the "proc
filesystem (procfs; https://en.wikipedia.org/wiki/Procfs) is a special
filesystem in Unix-like operating systems that presents information
about processes and other system information in a hierarchical
file-like structure".  For instance, you can query the uptime of the
machine by reading from /proc/uptime:

$ cat /proc/uptime
332826.96 661438.10

$ cat /proc/uptime
332871.40 661568.50


You can get all IDs (PIDs) of all processes currently running:

$ ls /proc/ | grep -E '^[0-9]+$'

and for each process you there are multiple attributes mapped as
files, e.g. if I start R as:

$ R --args -e "message('hello there')"

then I can query that process as:

$ pid=$(pidof R)
$ echo $pid
26323

$ cat /proc/26323/cmdline
/usr/lib/R/bin/exec/R--args-emessage('hello there')

Unix is neat


Indeed.  Couldn't agree more.  Thanks for the insight.



cheers,

Rolf



__
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] file.exists() on device files

2017-01-11 Thread Benjamin Tyner

Hi,

On my linux machine (Ubuntu, and also tested on RHEL), I am curious to 
know what might be causing file.exists (and also normalizePath) to not 
see the final device file here:


   > list.files("/dev/fd", full.names = TRUE)
   [1] "/dev/fd/0" "/dev/fd/1" "/dev/fd/2" "/dev/fd/3"
   > file.exists(list.files("/dev/fd", full.names = TRUE))
   [1]  TRUE  TRUE  TRUE FALSE
   > normalizePath(list.files("/dev/fd", full.names = TRUE))
   [1] "/dev/pts/2" "/dev/pts/2" "/dev/pts/2" "/dev/fd/3"
   Warning message:
   In normalizePath(list.files("/dev/fd", full.names = TRUE)) :
 path[4]="/dev/fd/3": No such file or directory

   > sessionInfo()
   R version 3.2.5 (2016-04-14)
   Platform: x86_64-pc-linux-gnu (64-bit)
   Running under: Ubuntu 16.04.1 LTS

   locale:
[1] LC_CTYPE=en_US.UTF-8   LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8   LC_NAME=C
[9] LC_ADDRESS=C   LC_TELEPHONE=C
   [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

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

Regards
Ben

__
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] modify the imported version of a function

2016-12-19 Thread Benjamin Tyner

Figured it out...in case it is useful to others:

   > library(lint)

   > lint.enclos <- parent.env(asNamespace("lint"))

   > stopifnot(all(c("perl", "regex") %in% ls(lint.enclos)))

   > lint.enclos$perl
   function (pattern)
   {
   message("perl is deprecated. Please use regexp instead")
   regex(pattern)
   }
   

   > unlockBinding("perl", lint.enclos)

   > lint.enclos$perl <- lint.enclos$regex

   > lint.enclos$perl
   function (pattern, ignore_case = FALSE, multiline = FALSE, comments 
= FALSE,

   dotall = FALSE, ...)
   {
   options <- stri_opts_regex(case_insensitive = ignore_case,
   multiline = multiline, comments = comments, dotall = dotall,
   ...)
   structure(pattern, options = options, class = c("regex",
   "pattern", "character"))
   }
   



On 12/16/2016 06:06 PM, Benjamin Tyner wrote:

Hi

I saw on the assignInNamespace help page, that it changes "the copy in 
the namespace, but not any copies already exported from the namespace, 
in particular an object of that name in the package (if already 
attached) and any copies already imported into other namespaces."


So now I'm wondering, whether there is a way to modify such copies 
already imported into other namespaces? For a concrete example, 
consider the lint package (now archived on CRAN) which includes a 
function check_pattern, which calls stringr:::perl, which has been 
deprecated in newer versions:


   > stringr:::perl
   function (pattern)
   {
   message("perl is deprecated. Please use regexp instead")
   regex(pattern)
   }
   

so, what if I wanted to directly replace stringr:::perl with 
stringr:::regex, in such a way that lint::check_pattern sees the 
replacement? Is there a way, preferably without having to attach 
stringr to the search path?


(I'm mostly just interested in learning about how namespace imports 
actually work; of course in this toy example one could use 
suppressMessages to hide that deprecation message).


Regards
Ben


__
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] modify the imported version of a function

2016-12-16 Thread Benjamin Tyner

Hi

I saw on the assignInNamespace help page, that it changes "the copy in 
the namespace, but not any copies already exported from the namespace, 
in particular an object of that name in the package (if already 
attached) and any copies already imported into other namespaces."


So now I'm wondering, whether there is a way to modify such copies 
already imported into other namespaces? For a concrete example, consider 
the lint package (now archived on CRAN) which includes a function 
check_pattern, which calls stringr:::perl, which has been deprecated in 
newer versions:


   > stringr:::perl
   function (pattern)
   {
   message("perl is deprecated. Please use regexp instead")
   regex(pattern)
   }
   

so, what if I wanted to directly replace stringr:::perl with 
stringr:::regex, in such a way that lint::check_pattern sees the 
replacement? Is there a way, preferably without having to attach stringr 
to the search path?


(I'm mostly just interested in learning about how namespace imports 
actually work; of course in this toy example one could use 
suppressMessages to hide that deprecation message).


Regards
Ben

__
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_DirtyImage and Rprof

2016-05-11 Thread Benjamin Tyner

Hello,

I have some code which was running in interactive mode while Rprof(..., 
line.profiling = TRUE). Near the end of my script, it opens up a 
pipe(..., open = "w") to a perl script, and at that point the execution 
gets stuck using 100% cpu.


(The perl script itself never showed up in pstree, as far as I can tell).

I did a "tail -f" on the file being written to by Rprof, and it was 
reporting "sys.save.image" over and over, and in fact an ".RData" file 
appeared when I had not asked for one, and I was able to load it later.


This got me curious, as nowhere in my code do I directly use that 
function. Looking through the source code for R, it appears that 
"sys.save.image" is called whenever an R_DirtyImage condition is triggered.


This was using R version 3.2.2 under RHEL. My efforts to create a 
reproducible example of this behavior have thus far been unsuccessful.


My questions: is there any documentation for R_DirtyImage, and how 
plausible is it that the R_DirtyImage condition was triggered by 
something Rprof did? The reason for my conjecture is that 
sys.save.image() calls closeAllConnections(), which I imagine might have 
interfered with the pipe that was open for writing, thus causing the 
stuck execution at that point.


If so, any advice for avoiding the R_DirtyImage condition while profiling?

If not, any conjectures for what might actually be going on? For what 
it's worth, I have observed a similar situation when using Rprof + 
system() instead of pipe(); for example:


   https://stat.ethz.ch/pipermail/r-help/2015-August/431286.html

Regards
Ben

__
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] choropleth packages (US)

2015-12-10 Thread Benjamin Tyner
Very nice Adrian. Is there a straightforward way to add Alaska and 
Hawaii at the lower left? (without resorting to choroplethr package)


On 12/10/2015 06:09 AM, Adrian Waddell wrote:

Hi,

You can also use the 'maps' package for the map data and the 'scales'
package for the color mapping.

E.g.

library(maps)
library(scales)

m <- map('state', fill=TRUE, plot=FALSE)

s_data <- tolower(rownames(USArrests))
s_map <- tolower(m$names)

mapping <- lapply(s_data, function(state) {
   which(grepl(state, s_map))
})
## check if the mapping is good!

col_pal <- col_numeric("Greens", domain=NULL, na.color = 'lightyellow')

cols <- rep('lightyellow', length(s_data))

Map(function(indices, col) {
   cols[indices] <<- col
}, mapping, col_pal(USArrests$UrbanPop))

map(m, col=cols, fill=TRUE)


Adrian



On Mon, Dec 7, 2015 at 9:34 AM, Erich Neuwirth
 wrote:

ggplot2 also can do this with
fortify
geom_polygon

Von meinem iPad gesendet


Am 06.12.2015 um 21:03 schrieb Benjamin Tyner :

Hi

I wish to draw a basic choropleth (US, by state) and am wondering if anyone has 
any recommendations? I've tried the following thus far:

1. choroplethr: this works, but required installation of 30+ dependencies. I 
would prefer something with fewer dependencies.
2. tmap: this also seems promising, but most of the examples I saw were 
specific to European maps. Can it be adapted for US?
3. statebins: doesn't draw true choropleths, but I liked that it doesn't have 
many dependencies.

Regards
Ben

__
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-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] choropleth packages (US)

2015-12-06 Thread Benjamin Tyner

Hi

I wish to draw a basic choropleth (US, by state) and am wondering if 
anyone has any recommendations? I've tried the following thus far:


1. choroplethr: this works, but required installation of 30+ 
dependencies. I would prefer something with fewer dependencies.
2. tmap: this also seems promising, but most of the examples I saw were 
specific to European maps. Can it be adapted for US?
3. statebins: doesn't draw true choropleths, but I liked that it doesn't 
have many dependencies.


Regards
Ben

__
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] efficiently multiply each column of a sparse Matrix by a sparse vector

2015-11-28 Thread Benjamin Tyner

Hi,

Say I have a sparse Matrix X, and a sparse vector (stored as a 1-column 
sparse Matrix A), with X and A having the same number of rows, and I 
wish to multiply each column of X by A, but would like the operation to 
take full advantage of the sparseness of both X and A. In other words I 
want the result to be another sparse Matrix but not having any zeros 
calculated or stored unnecessarily. For concreteness,


   library(Matrix)
   set.seed(6860)
   X <- sparseMatrix(i = sample(1:10, 5L),
 j = sample(1:10, 5L),
 x = rep(1, 5),
 dims = c(10L, 10L)
 )
   A <- sparseMatrix(i = sample(1:10, 5L),
 j = rep(1L, 5L),
 x = rep(1, 5),
 dims = c(10L, 1L)
 )

and observe that

   print(X * A[, 1L, drop=TRUE])

gives the following, in which three 0s are not represented sparsely,

   10 x 10 sparse Matrix of class "dgCMatrix"

[1,] . . . . . . . . . .
[2,] . . 1 . . . . . . .
[3,] . . . . . . . . . .
[4,] . . . . . 0 . . . .
[5,] . . . . . . 0 . . .
[6,] . 1 . . . . . . . .
[7,] . . . . . . . . . .
[8,] . . . . . . . . . .
[9,] . . . . . . . . . .
   [10,] 0 . . . . . . . . .

in other words I am wondering if there is a more efficient way to arrive 
at the same result as,


   print(X * A[, rep(1L, ncol(X)), drop=FALSE])

   10 x 10 sparse Matrix of class "dgCMatrix"

[1,] . . . . . . . . . .
[2,] . . 1 . . . . . . .
[3,] . . . . . . . . . .
[4,] . . . . . . . . . .
[5,] . . . . . . . . . .
[6,] . 1 . . . . . . . .
[7,] . . . . . . . . . .
[8,] . . . . . . . . . .
[9,] . . . . . . . . . .
   [10,] . . . . . . . . . .

without the additional overhead of duplicating A for ncol(X) times.

This seems like such a simple thing, but has me stumped. Any ideas?

Regards,
Ben

__
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] backsolve, chol, Matrix, and SparseM

2015-09-25 Thread Benjamin Tyner
Hi Martin,

Thanks for the remarks and examples, and for confirming that I was
indeed barking up the wrong tree with SparseM.

A. I assume that is a typo and you meant to say, no need for backsolve().
B. Absolutely; however, in this case I am taking advantage of
quadprog::solve.QP(..., factorized = TRUE) which requires the inverse of
the Cholesky factor; it turns out to be faster to compute this one time
upfront rather than have solve.QP(..., factorized = FALSE) do it over
and over again. Of course the holy grail would be a QP solver which
takes advantage of the various innovations from package:Matrix, but I
digress...
C. Agreed, assuming you are talking about Matrix::solve(X) on X of class
Matrix. On the other hand for a regular matrix x it is not difficult to
construct examples where backsolve(chol(x), diag(nrow(x))) is twice as
fast as base::solve(chol(x)), which led me down this path in the first
place.

By the way, is R-forge still the correct place to report bugs in
package:Matrix?

Regards
Ben


On 09/25/2015 04:25 AM, Martin Maechler wrote:
> Dear Ben,
>
>>>>>> Benjamin Tyner 
>>>>>> on Thu, 24 Sep 2015 13:47:58 -0400 writes:
> > Hi I have some code which does (on a symmetric matrix 'x')
>
> > backsolve(chol(x), diag(nrow(x)))
>
> > and I am wondering what is the recommended way to
> > accomplish this when x is also sparse (from
> > package:Matrix). I know that package:Matrix provides a
> > chol method for such matrices, but not a backsolve
> > method. On the other hand, package:SparseM does provide a
> > backsolve method, but doesn't actually return a sparse
> > matrix. Moreover, I am a little hesitant to use SparseM,
> > as the vignette seems to be from 2003.
>
> Roger Koenker has agreed in the past, that new projects should
> rather use Matrix.   SparseM has been the very first R package
> providing sparse matrix support.
>
>
> > I did notice that help(topic = "solve", package =
> > "Matrix") says "In ‘solve(a,b)’ in the ‘Matrix’ package,
> > ‘a’ may also be a ‘MatrixFactorization’ instead of
> > directly a matrix." which makes me think this is the right
> > way:
>
> > Matrix::solve(Cholesky(x), .sparseDiagonal(nrow(x)))
>
> > but unfortunately this didn't give the same result as:
>
> > Matrix::solve(chol(x), .sparseDiagonal(nrow(x)))
>
> > so I'm asking here in case someone has any suggestions.
>
> You don't give any examples.
> So a few remarks and a reproducible example to get more concrete
>
> A. As the Matrix package has classes for triangular matrices and
>   Matrix :: chol() returns them, there   is no need for
>   forwardsolve() or backwardsolve(), as just   solve() is always
>   enough.
>
> B. As Doug Bates has been teaching for many decennia, "it is
>   almost always computationally *wrong* to compute a matrix
>   inverse explicitly".
>   Rather computeA^{-1} B   or  A^{-1} x  {for vector x,
>   matrix B (but different from Identity).
>
> C. Inspite of B, there are cases (such as computing sandwich
>   estimates of covariance matrices) where you do want the inverse.
>   In that case,
>
>solve(A) is semantically equivalent to
>solve(A, diag(.))
>
>and almost always the *first* form is implempented more
>efficiently than the second.
>
> D. In Matrix,  use chol(.) ... unless you really read a bit
>about Cholesky(.) and its special purpose sparse cholesky decompositions.
>As mentioned above,  Matrix :: chol()  will return a
>"formally triangular" matrix, i.e., inheriting from
>"triangularMatrix"; in the sparse case, very typically of
>specific class "dtCMatrix".
>
> Here's a small reproducible example,
> please use it to ask further questions:
>
> *.R:
>
> library(Matrix)
> M <- as(diag(4)+1,"dsCMatrix")
> m <- as(M, "matrix") # -> traditional R matrix
> stopifnot( all(M == m) )
> M
> L <- Cholesky(M,super=TRUE,perm=FALSE) # a MatrixFactorization ("dCHMsuper")
> (L. <- as(L, "Matrix")) #-> lower-triagonal (sparseMatrix, specifically 
> "dtCMatrix")
> (cM <- chol(M))# *upper* triagonal ("dtCMatrix")
> (cm <- chol(m))#  upper  triagonal traditional matrix -- the same "of course" 
> :
> all.equal(as.matrix(cM), cm) # TRUE
>
> (r. <- backsolve(cm, diag(4)))# upper tri. (traditional) matrix
> (R. <- solve(cM) ) ## the "same"  (but nicer printing)
> all.equal

[R] backsolve, chol, Matrix, and SparseM

2015-09-24 Thread Benjamin Tyner
Hi

I have some code which does (on a symmetric matrix 'x')

backsolve(chol(x), diag(nrow(x)))

and I am wondering what is the recommended way to accomplish this when x
is also sparse (from package:Matrix). I know that package:Matrix
provides a chol method for such matrices, but not a backsolve method. On
the other hand, package:SparseM does provide a backsolve method, but
doesn't actually return a sparse matrix. Moreover, I am a little
hesitant to use SparseM, as the vignette seems to be from 2003.

I did notice that help(topic = "solve", package = "Matrix") says "In
‘solve(a,b)’ in the ‘Matrix’ package, ‘a’ may also be a
‘MatrixFactorization’ instead of directly a matrix." which makes me
think this is the right way:

Matrix::solve(Cholesky(x), .sparseDiagonal(nrow(x)))

but unfortunately this didn't give the same result as:

Matrix::solve(chol(x), .sparseDiagonal(nrow(x)))

so I'm asking here in case someone has any suggestions.

Regards,
Ben

__
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] Rprof and system

2015-08-11 Thread Benjamin Tyner
Hi

I have an R script which invokes WriteXLS() (from the package of the
same name) which as you may know, calls perl via system(). I've noticed
that when I enable profiling using Rprof(), when the script gets to the
part where perl is called, it gets "stuck": it just sits there using
99-100% CPU and around 10% of the RAM, and the perl command does not
even show up as a running process under 'top'. While it is churning
away, new profiling results continue to be written to the profiling file
(at which point the entries are all "system" / "WriteXLS").  I have also
noticed this behavior with other system() calls; not just perl. I've
also tried pipe(cmd, open = "r") as an alternative to system(cmd) and
the result is the same. I have tried in interactive as well as
non-interactive mode, and the issue occurs in both modes although seems
to be more common in the latter.

So, I'm wondering if perhaps Rprof() + system() not a recommended
combination ? I did notice from ?Rprof that "the profiler interrupts R
asynchronously"; though I do not know what that means, perhaps that is
somehow relevant? This is running on R version 3.2.1 under linux (RHEL6).

Any suggestions would be greatly appreciated.

Regards
Ben

__
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] package implementing continuous binomial?

2015-05-07 Thread Benjamin Tyner
Thanks David! I'll take a look at zipfR.

Regards
Ben

On 05/07/2015 03:10 PM, David Winsemius wrote:
> On May 6, 2015, at 7:00 PM, Benjamin Tyner wrote:
>
>> Hi
>>
>> I'm wondering if anyone is aware of an R package implementing (i.e.,
>> providing a pdf, cdf, and/or quantile function) for the continuous
>> binomial distribution? Specifically the one characterized here:
>>
>> http://www2.math.uni-paderborn.de/fileadmin/Mathematik/AG-Indlekofer/Workshop/Satellite_meeting/ilenko.pdf
>>
>> Figured I would check here first, before attempting to code it up myself.
> I found that reading the ArXiv version of that material was easier to 
> understand:
>
> http://arxiv.org/abs/1303.5990
>
> zipfR package has an implementation of the incomplete beta function that 
> might make some of the coding of the pdf and cdf more simple. 
>
> Searching done with Graves' very useful utility package: 
>
> library('sos')
> findFn("incomplete beta function")
>
> (I did't think that doing a search on "continuous Binomial" was likely to be 
> helpful, but I tried it anyway and did not find any functions named 
> "continuous binomial" in their help page titles.)
>


__
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] package implementing continuous binomial?

2015-05-06 Thread Benjamin Tyner
Hi

I'm wondering if anyone is aware of an R package implementing (i.e.,
providing a pdf, cdf, and/or quantile function) for the continuous
binomial distribution? Specifically the one characterized here:

http://www2.math.uni-paderborn.de/fileadmin/Mathematik/AG-Indlekofer/Workshop/Satellite_meeting/ilenko.pdf

Figured I would check here first, before attempting to code it up myself.

Regards
Ben

__
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] non-terminal token lacking children from utils::getParseData

2015-05-02 Thread Benjamin Tyner
Thank you Yihui for also reporting the bug here:

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

and thank you Duncan for finding the issue and fixing it! I definitely
like your idea to report a summary message instead of the long text string.

Regards
Ben

> I tried to reduce the offending portion as best I could to a
> more-or-less minimal example (1136 bytes), which can be downloaded via:
>
> wget https://www.dropbox.com/s/74rgxr5x2aalr99/badstring.R
>
> then once in R,
>
> > b <- parse(file = "~/badstring.R", keep.source = TRUE)
> > d <- getParseData(b, includeText = FALSE)
> > subset(d, line1 == 2L)
>line1 col1 line2 col2 id parent token terminal
> 10 25241 10 21  exprFALSE
> > subset(d, parent == 10)
> [1] line1col1 line2col2 id   parent   token   
> terminal
> <0 rows> (or 0-length row.names)
>
> here is my
>
> > sessionInfo()
> R version 3.0.2 (2013-09-25)
> Platform: x86_64-pc-linux-gnu (64-bit)
>
> locale:
>  [1] LC_CTYPE=en_US.UTF-8   LC_NUMERIC=C 
>  [3] LC_TIME=en_US.UTF-8LC_COLLATE=en_US.UTF-8   
>  [5] LC_MONETARY=en_US.UTF-8LC_MESSAGES=en_US.UTF-8  
>  [7] LC_PAPER=en_US.UTF-8   LC_NAME=C
>  [9] LC_ADDRESS=C   LC_TELEPHONE=C   
> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C  
>
> attached base packages:
> [1] stats graphics  grDevices utils datasets  methods   base
>
> note that while it says R version 3.0.2 above, I have seen the same
> behaviour under version 3.1.2 as well.
>
> Regards
> Ben
>
> On 02/19/2015 06:34 PM, Duncan Murdoch wrote:
> >/ On 19/02/2015 6:31 PM, B Tyner wrote:
> />>/ Hi,
> />>/
> />>/ I have run across a source file for which the return value
> />>/ of getParseData() includes a record having FALSE for $terminal, yet it is
> />>/ not the parent of any other tokens. Before I spend time constructing a
> />>/ reproducible example, I wanted to verify that this is in fact unexpected
> />>/ behavior (under R 3.1.2)?
> />/ Before I spend the time thinking about that, I'd like to see a
> />/ reproducible example.
> />/
> />/ Duncan Murdoch
> />/
> />


__
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] non-terminal token lacking children from utils::getParseData

2015-02-19 Thread Benjamin Tyner
I tried to reduce the offending portion as best I could to a
more-or-less minimal example (1136 bytes), which can be downloaded via:

wget https://www.dropbox.com/s/74rgxr5x2aalr99/badstring.R

then once in R,

> b <- parse(file = "~/badstring.R", keep.source = TRUE)
> d <- getParseData(b, includeText = FALSE)
> subset(d, line1 == 2L)
   line1 col1 line2 col2 id parent token terminal
10 25241 10 21  exprFALSE
> subset(d, parent == 10)
[1] line1col1 line2col2 id   parent   token   
terminal
<0 rows> (or 0-length row.names)
   
here is my

> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-pc-linux-gnu (64-bit)
   
locale:
 [1] LC_CTYPE=en_US.UTF-8   LC_NUMERIC=C 
 [3] LC_TIME=en_US.UTF-8LC_COLLATE=en_US.UTF-8   
 [5] LC_MONETARY=en_US.UTF-8LC_MESSAGES=en_US.UTF-8  
 [7] LC_PAPER=en_US.UTF-8   LC_NAME=C
 [9] LC_ADDRESS=C   LC_TELEPHONE=C   
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C  
   
attached base packages:
[1] stats graphics  grDevices utils datasets  methods   base

note that while it says R version 3.0.2 above, I have seen the same
behaviour under version 3.1.2 as well.

Regards
Ben

On 02/19/2015 06:34 PM, Duncan Murdoch wrote:
> On 19/02/2015 6:31 PM, B Tyner wrote:
>> Hi,
>>
>> I have run across a source file for which the return value
>> of getParseData() includes a record having FALSE for $terminal, yet it is
>> not the parent of any other tokens. Before I spend time constructing a
>> reproducible example, I wanted to verify that this is in fact unexpected
>> behavior (under R 3.1.2)?
> Before I spend the time thinking about that, I'd like to see a
> reproducible example.
>
> Duncan Murdoch
>
>


__
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] quadprog::solve.QP sometimes returns NaNs

2014-09-26 Thread Benjamin Tyner
Hello,

Here is an example; hopefully it is reproducible on others' platform:

library(quadprog)

n <- 66L

set.seed(6860)
X <- matrix(1e-20, n, n)
diag(X) <- 1
Dmat <- crossprod(X)
y <- seq_len(n)
dvec <- crossprod(X, y)

Amat <- diag(n)
bvec <- y + runif(n)

sol <- solve.QP(Dmat, dvec, Amat, bvec, meq = n)

print(sol$solution) # this gives all NaNs

under sessionInfo():

R version 3.0.2 (2013-09-25)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8   LC_NUMERIC=C 
 [3] LC_TIME=en_US.UTF-8LC_COLLATE=en_US.UTF-8   
 [5] LC_MONETARY=en_US.UTF-8LC_MESSAGES=en_US.UTF-8  
 [7] LC_PAPER=en_US.UTF-8   LC_NAME=C
 [9] LC_ADDRESS=C   LC_TELEPHONE=C   
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C  

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

other attached packages:
[1] quadprog_1.5-5

Any ideas?

Thanks
Ben

__
R-help@r-project.org mailing list
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] scaling of Amat still recommended for quadprog::solve.QP ?

2014-09-25 Thread Benjamin Tyner
Greetings,

I ran across this recommendation, to keep the norms of the columns of
the Amat on similar
scales,

https://stat.ethz.ch/pipermail/r-help/2007-September/141335.html

However, when I looked at the code, I noticed that the norms are already
being calculated:

c
c calculate the norm of each column of the A matrix
c
  do 51 i=1,q
 sum = 0.d0
 do 52 j=1,n
sum = sum + amat(j,i)*amat(j,i)
 52  continue
 work(iwnbv+i) = sqrt(sum)
 51   continue
  nact = 0
  iter(1) = 0
  iter(2) = 0
 50   continue

though I am not sure exactly how these norms get used subsequently. My
question is, is it no longer necessary to follow Berwin's recommendation
from 2007? Or are the norms being calculated for some other purpose, and
the recommendation still applies?

Regards
Ben

__
R-help@r-project.org mailing list
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] from cut.Date

2014-09-17 Thread Benjamin Tyner
Thanks Brian! 

The confusion was due to my failure to notice that these two functions have 
opposite defaults for 'right':

   > args(cut.default)
   function (x, breaks, labels = NULL, include.lowest = FALSE, right = TRUE, 
dig.lab = 3L, ordered_result = FALSE, ...) 

   > args(cut.Date)
   function (x, breaks, labels = NULL, start.on.monday = TRUE, right = FALSE, 
...) 

I suppose the latter does make sense, given that days, months, years etc are 
right-continuous functions of time.

Regards
Ben

> On 17/09/2014 12:04, Benjamin Tyner wrote:
> >/ Hello,
> />/
> />/ I'm wondering if this is expected?
> /
> It is as documented!
> >/
> />/  > cut(structure(1, class="Date"), structure(c(11100,1),
> />/ class="Date"))
> />/  [1] 
> />/  Levels: 2000-05-23
> />/
> />/ The help page says that "for ‘"Date"’ objects, only ‘"day"’, ‘"week"’,
> />/ ‘"month"’, ‘"quarter"’ and ‘"year"’ are allowed" [for the 'breaks'
> />/ argument]. Though I am not sure whether this statement is only
> />/ applicable in the context of the previous sentence about interval
> />/ specification (i.e., a roundabout way of saying that ‘"sec"’, ‘"min"’,
> />/ ‘"hour"’, and ‘"DSTday"’ are not allowed for 'Date' objects), or whether
> />/ it also means that a vector of cut points (as in my example) is likewise
> />/ not allowed? If the latter, then perhaps the function out to error out
> />/ rather than return  in this case?
> /
> The NA is correct: the value you pass is not covered by the 'breaks' you 
> specified.  As the help says
>
>   Using both ‘right = TRUE’ and ‘include.lowest = TRUE’ will include
>   both ends of the range of dates.
>
> With the default values, only the lower end is included.
>
> -- 
> Brian D. Ripley,  ripley at stats.ox.ac.uk 
> <https://stat.ethz.ch/mailman/listinfo/r-help>
> Emeritus Professor of Applied Statistics, University of Oxford
> 1 South Parks Road, Oxford OX1 3TG, UK


__
R-help@r-project.org mailing list
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] from cut.Date

2014-09-17 Thread Benjamin Tyner
Hello,

I'm wondering if this is expected?

> cut(structure(1, class="Date"), structure(c(11100,1),
class="Date"))
[1] 
Levels: 2000-05-23

The help page says that "for ‘"Date"’ objects, only ‘"day"’, ‘"week"’,
‘"month"’, ‘"quarter"’ and ‘"year"’ are allowed" [for the 'breaks'
argument]. Though I am not sure whether this statement is only
applicable in the context of the previous sentence about interval
specification (i.e., a roundabout way of saying that ‘"sec"’, ‘"min"’,
‘"hour"’, and ‘"DSTday"’ are not allowed for 'Date' objects), or whether
it also means that a vector of cut points (as in my example) is likewise
not allowed? If the latter, then perhaps the function out to error out
rather than return  in this case?

Regards
Ben

__
R-help@r-project.org mailing list
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] lattice: packet.number() versus panel.number()

2014-08-27 Thread Benjamin Tyner
Thanks David. It turns out that I was in fact using a custom index.perm,
but not on purpose. What happened was I used the "[" method on the
trellis object ( lattice:::`[.trellis`), which of course is nothing but
a short-cut for updating the index.perm. Lesson learned...

Regards,
Ben

On 08/27/2014 02:10 AM, David Winsemius wrote:
> On Aug 26, 2014, at 3:25 PM, Benjamin Tyner wrote:
>
>> Hi,
>>
>> According to
>> https://svn.r-project.org/R-packages/trunk/lattice/R/print.trellis.R,
>>
>>"[panel.number] is usually the same as, but can be different from
>> packet.number"
>>
>> and I had been under the impression that as long as the user is not
>> using a custom index.cond nor perm.cond, the panel.number would in fact
>> be the same as the packet.number.
>>
>> However, I have recently come across a case where the two are *not* the
>> same, even though I am not using a custom index.cond nor perm.cond.
>>
>> So my question is, what might be some other possible situations in which
>> the two would be expected to differ?
> The immediate hypothesis that leaps to mind is cases where there are multiple 
> pages. On each page I suspect the upper left numbering restarts with 1, but I 
> suspect the packet numbers are sequential increasing.
>


__
R-help@r-project.org mailing list
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] lattice: packet.number() versus panel.number()

2014-08-26 Thread Benjamin Tyner
Hi,

According to
https://svn.r-project.org/R-packages/trunk/lattice/R/print.trellis.R,

"[panel.number] is usually the same as, but can be different from
packet.number"

and I had been under the impression that as long as the user is not
using a custom index.cond nor perm.cond, the panel.number would in fact
be the same as the packet.number.

However, I have recently come across a case where the two are *not* the
same, even though I am not using a custom index.cond nor perm.cond.

So my question is, what might be some other possible situations in which
the two would be expected to differ?

Regards,
Ben

__
R-help@r-project.org mailing list
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] non-interactive subset, `$`, and quotes

2014-06-26 Thread Benjamin Tyner
Duncan,

How embarrassing! Thanks.

-Ben

On 06/26/2014 10:25 PM, Duncan Murdoch wrote:
> On 27/06/2014, 4:08 AM, Benjamin Tyner wrote:
>> Hi
>>
>> I know that subset() is not intended for use in programming. However I
>> am still curious to learn why, in non-interactive mode, if I take away
>> the quotes around 'bar'
>>
>>Rscript -e "foo <- list(bar = iris); head(subset(foo$'bar',
>> Species=='setosa'))"
>>  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
>>1  5.1 3.5  1.4 0.2  setosa
>>2  4.9 3.0  1.4 0.2  setosa
>>3  4.7 3.2  1.3 0.2  setosa
>>4  4.6 3.1  1.5 0.2  setosa
>>5  5.0 3.6  1.4 0.2  setosa
>>6  5.4 3.9  1.7 0.4  setosa
>>
>> then it results in an error:
>>
>>Rscript -e "foo <- list(bar = iris); head(subset(foo$bar,
>> Species=='setosa'))"
>>Error in subset.default(foo, Species == "setosa") :
>>  object 'Species' not found
>>Calls: head -> subset -> subset.default
>>Execution halted
>>
>> and also why this does not occur in interactive mode? My sessionInfo() is
>>
>>R Under development (unstable) (2014-06-26 r66043)
>>Platform: x86_64-unknown-linux-gnu (64-bit)
>>
>>locale:
>> [1] LC_CTYPE=en_US.UTF-8   LC_NUMERIC=C 
>> [3] LC_TIME=en_US.UTF-8LC_COLLATE=en_US.UTF-8   
>> [5] LC_MONETARY=en_US.UTF-8LC_MESSAGES=en_US.UTF-8  
>> [7] LC_PAPER=en_US.UTF-8   LC_NAME=C
>> [9] LC_ADDRESS=C   LC_TELEPHONE=C   
>>[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C  
>>
>>attached base packages:
>>[1] stats graphics  grDevices utils datasets  methods   base
>>
> Try replacing "Rscript -e" with "echo" to see what your shell is really
> sending as a command to execute.   I suspect it is treating $bar as a
> shell variable (see the error message, it has disappeared).
>


-- 
 


__
R-help@r-project.org mailing list
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] non-interactive subset, `$`, and quotes

2014-06-26 Thread Benjamin Tyner
Hi

I know that subset() is not intended for use in programming. However I
am still curious to learn why, in non-interactive mode, if I take away
the quotes around 'bar'

   Rscript -e "foo <- list(bar = iris); head(subset(foo$'bar',
Species=='setosa'))"
 Sepal.Length Sepal.Width Petal.Length Petal.Width Species
   1  5.1 3.5  1.4 0.2  setosa
   2  4.9 3.0  1.4 0.2  setosa
   3  4.7 3.2  1.3 0.2  setosa
   4  4.6 3.1  1.5 0.2  setosa
   5  5.0 3.6  1.4 0.2  setosa
   6  5.4 3.9  1.7 0.4  setosa

then it results in an error:

   Rscript -e "foo <- list(bar = iris); head(subset(foo$bar,
Species=='setosa'))"
   Error in subset.default(foo, Species == "setosa") :
 object 'Species' not found
   Calls: head -> subset -> subset.default
   Execution halted

and also why this does not occur in interactive mode? My sessionInfo() is

   R Under development (unstable) (2014-06-26 r66043)
   Platform: x86_64-unknown-linux-gnu (64-bit)

   locale:
[1] LC_CTYPE=en_US.UTF-8   LC_NUMERIC=C 
[3] LC_TIME=en_US.UTF-8LC_COLLATE=en_US.UTF-8   
[5] LC_MONETARY=en_US.UTF-8LC_MESSAGES=en_US.UTF-8  
[7] LC_PAPER=en_US.UTF-8   LC_NAME=C
[9] LC_ADDRESS=C   LC_TELEPHONE=C   
   [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C  

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

Regards
Ben

__
R-help@r-project.org mailing list
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] detecting the sourcing of site profile on Startup versus post-Startup

2014-04-24 Thread Benjamin Tyner
Jeff,

I absolutely agree it is a bad idea to rely on side effects.

I did figure out one way to skin this cat. It relies on an the following
from line 909 of src/main/main.c,

R_LoadProfile(R_OpenSiteFile(), baseEnv);
R_LockBinding(install(".Library.site"), R_BaseEnv);
R_LoadProfile(R_OpenInitFile(), R_GlobalEnv);

to illustrate, if one puts at the top of the site profile:

   if (bindingIsLocked(".Library.site", .BaseNamespaceEnv)) {
  # site profile has already finished loading;
  # put code here for that case. for example,
  if (identical(.BaseNamespaceEnv$.GoodJob, Sys.getpid())) {
  warning("you appear to be using the same file for both site
and user profiles, or to have sourced this file post-startup.")
  }
  warning("this file is not intended to be used in this fashion.")
   } else {
  # site profile is in process of loading;
  # put code here for that case. for example,
  message("good job! startup loaded the correct site profile.")
  .GoodJob <- Sys.getpid()
   }

Not exactly best practice to rely on an implementation detail, but I
found it interesting nevertheless.

Regards
Ben


On 04/23/2014 09:31 PM, Jeff Newmiller wrote:
> Regardless of whether this is possible, it seems like a bad idea (side 
> effects in a functional programming environment). If you want to do something 
> special in startup then write a different function that does that stuff and 
> then call the desired functions explicitly when you want them to be called.
> ---
> Jeff NewmillerThe .   .  Go Live...
> DCN:Basics: ##.#.   ##.#.  Live Go...
>   Live:   OO#.. Dead: OO#..  Playing
> Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
> /Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
> --- 
> Sent from my phone. Please excuse my brevity.
>
> On April 23, 2014 6:11:09 PM PDT, Benjamin Tyner  wrote:
>> Thanks  Duncan!  Yes, I considered taking advantage of .First, but was
>> concerned that the .First defined by the site profile could be masked
>> by a
>> possible .First defined by the user profile (I neglected to mention
>> that
>> "--no-init-profile"  [sic]  in  the  example  I gave was a simplifying
>>   assumption, sorry about that).
>>   On 04/23/2014 06:55 AM, Duncan Murdoch wrote:
>>
>> On 22/04/2014, 8:59 PM, Benjamin Tyner wrote:
>>
>> Greetings,
>> Is there any way to programmatically detect whether a piece of code is
>>   being run within the initial (Startup) sourcing of the site profile?
>>   For example, say I have a site profile, "/path/to/Rprofile.site". Is
>>   there any function "my_func" which would return different values for
>> these two instances:
>> Rscript --no-site-profile --no-init-profile -e
>>"sys.source('/path/to/Rprofile.site',  envir  = .BaseNamespaceEnv);
>> my_func()"
>> versus:
>> export R_PROFILE=/path/to/Rprofile.site
>> Rscript --no-init-profile -e "my_func()"
>>
>> The commandArgs() function could see the different command lines and
>> your
>> function could deduce the difference from that.
>> As far as I know, R keeps no other records of the startup process, but
>> if
>> you can modify other files, you could leave a record when .First was
>> run,
>>and see that it was run before Rprofile.site in the first case. See
>> ?Startup.
>> Duncan Murdoch
>>
>>   --
>>   
>>
>>
>> 
>>
>> __
>> R-help@r-project.org mailing list
>> 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
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] detecting the sourcing of site profile on Startup versus post-Startup

2014-04-23 Thread Benjamin Tyner

   Thanks  Duncan!  Yes, I considered taking advantage of .First, but was
   concerned that the .First defined by the site profile could be masked by a
   possible .First defined by the user profile (I neglected to mention that
   "--no-init-profile"  [sic]  in  the  example  I gave was a simplifying
   assumption, sorry about that).
   On 04/23/2014 06:55 AM, Duncan Murdoch wrote:

 On 22/04/2014, 8:59 PM, Benjamin Tyner wrote:

 Greetings,
 Is there any way to programmatically detect whether a piece of code is
 being run within the initial (Startup) sourcing of the site profile?
 For example, say I have a site profile, "/path/to/Rprofile.site". Is
 there any function "my_func" which would return different values for
 these two instances:
 Rscript --no-site-profile --no-init-profile -e
 "sys.source('/path/to/Rprofile.site',  envir  = .BaseNamespaceEnv);
 my_func()"
 versus:
 export R_PROFILE=/path/to/Rprofile.site
 Rscript --no-init-profile -e "my_func()"

 The commandArgs() function could see the different command lines and your
 function could deduce the difference from that.
 As far as I know, R keeps no other records of the startup process, but if
 you can modify other files, you could leave a record when .First was run,
 and see that it was run before Rprofile.site in the first case. See
 ?Startup.
 Duncan Murdoch

   --
   
__
R-help@r-project.org mailing list
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] detecting the sourcing of site profile on Startup versus post-Startup

2014-04-22 Thread Benjamin Tyner
Greetings,

Is there any way to programmatically detect whether a piece of code is
being run within the initial (Startup) sourcing of the site profile?

For example, say I have a site profile, "/path/to/Rprofile.site". Is
there any function "my_func" which would return different values for
these two instances:

   Rscript --no-site-profile --no-init-profile -e
"sys.source('/path/to/Rprofile.site', envir = .BaseNamespaceEnv); my_func()"

versus:

   export R_PROFILE=/path/to/Rprofile.site
   Rscript --no-init-profile -e "my_func()"

Regards,
Ben


__
R-help@r-project.org mailing list
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] package environment versus namespace environment

2014-03-09 Thread Benjamin Tyner

   Duncan,
   Thanks for the explanation and commentary. Starting to make more sense...so,
   long story short, it seems the first thing one should check is whether base
   imports utils:
  > packageDescription("base")
  Package: base
  Version: 3.0.1
  Priority: base
  Title: The R Base Package
  Author: R Core Team and contributors worldwide
  Maintainer: R Core Team [1]
  Description: Base R functions
  License: Part of R 3.0.1
  Built: R 3.0.1; ; 2013-10-16 10:50:56 UTC; unix
  -- File: /usr/lib/R/library/base/Meta/package.rds
   I'll interpret the lack of an "Imports:" line to mean that base does not
   import  any packages. So then the next thing to check is the enclosing
   environment:
  > parent.env(.BaseNamespaceEnv)
  
   Ah-ha! Unlike the base package environment, the base namespace environment
   has its enclosing environment already on the search path, so eventually
   leads back to utils. (This is the piece of the puzzle prompting my original
   question about "... machinery that allows ...")
   So hypothetically, if I had attached utils ahead of .GlobalEnv on the search
   path, then functions in the base namespace would no longer be able to see
   objects in utils? (I realize the answer may be vacuous, since library() does
   not honor pos=1).
   Regards,
   Ben
   On 03/09/2014 09:09 AM, Duncan Murdoch wrote:

 On 14-03-08 6:42 PM, Benjamin Tyner wrote:

 Duncan,
 Thank you for the informative link. So, do the loaded namespaces have an
 "ordering" akin to the package search path that determines that
 functions in the base namespace can see objects in the utils namespace?
 (I noticed that loadedNamespaces() just comes back in alphabetical order.)

 No.  The article that Henrik cited gives a reasonable description up until
 near  the  end, where (in my opinion) it makes things unnecessarily
 complicated.  I'd recommend that you stop reading around where he tries to
 explain the dotted lines.  In particular, ignore the second version of the
 "Map  of  the World"; the first one is accurate, the second is just
 misleading.
 In answer to your question:  Gupta's article misses the possibility of
 packages that are loaded but not in the search path.  In the notation of
 the first part of that article, loading a namespace just puts it in the
 middle two columns (i.e. creates the namespace and imports environments)
 without putting it in the search list.  That happens when you import or
 load a package without attaching it.  The search path imposes an ordering,
 things that aren't in it aren't ordered.
 Duncan Murdoch

 Regards
 Ben
 On 03/07/2014 11:46 AM, Duncan Murdoch wrote:

 On 07/03/2014 10:16 AM, Benjamin Tyner wrote:

 Hello,
 I realize that a function in  (for example, function
 "head1" below) is unable to see (without resorting to "::", anyway)
 objects in utils (for example, "head" below), since package:base is
 after package:utils on the search path.

 However, I'm wondering what is the machinery that allows a function in
  (for example, function "head2" below) to
 be able to see "head" just fine, without needing to resort to "::".

 See Luke Tierney's article in R News,
 Name space management for R. Luke Tierney, R News, 3(1):2-6, June 2003
 [2]<http://cran.r-project.org/doc/Rnews/Rnews_2003-1.pdf>
 There's a link to it from the R help system.  Run help.start(), then
 look at "Technical papers" in the Miscellaneous Material section.
 I believe most of what it says is still current; the only thing I can
 see at a glance that is no longer correct is that in those days
 namespaces were optional in packages.  Now all packages have namespaces.
 Duncan Murdoch

 I'm also wondering more generally, why there is a need (practically
 speaking) for a distinction between the environment associated with a
 package and the environment associated with the namespace.
 $ export R_PROFILE=/home/btyner/Rprofile.site
 $ cat /home/btyner/Rprofile.site
 sys.source("/home/btyner/head1.R", envir = baseenv())
 sys.source("/home/btyner/head2.R", envir = .BaseNamespaceEnv)
 $ cat /home/btyner/head1.R
 head1 <- function(x) head(x)
 $ cat /home/btyner/head2.R
 head2 <- function(x) head(x)
 $ Rscript -e "head1(letters)"
 Error in head1(letters) : could not find function "head"
 Execution halted
 $ Rscript -e "head2(letters)"
 [1] "a" "b" "c" "d" "e" "f"
 $ Rscript -e "sessionInfo()&q

Re: [R] package environment versus namespace environment

2014-03-08 Thread Benjamin Tyner

   Duncan,
   Thank you for the informative link. So, do the loaded namespaces have an
   "ordering" akin to the package search path that determines that functions in
   the base namespace can see objects in the utils namespace? (I noticed that
   loadedNamespaces() just comes back in alphabetical order.)
   Regards
   Ben
   On 03/07/2014 11:46 AM, Duncan Murdoch wrote:

 On 07/03/2014 10:16 AM, Benjamin Tyner wrote:

 Hello,
 I realize that a function in  (for example, function
 "head1" below) is unable to see (without resorting to "::", anyway)
 objects in utils (for example, "head" below), since package:base is
 after package:utils on the search path.

 However, I'm wondering what is the machinery that allows a function in
  (for example, function "head2" below) to
 be able to see "head" just fine, without needing to resort to "::".

 See Luke Tierney's article in R News,
 Name space management for R. Luke Tierney, R News, 3(1):2-6, June 2003
 [1]<http://cran.r-project.org/doc/Rnews/Rnews_2003-1.pdf>
 There's a link to it from the R help system.  Run help.start(), then look
 at "Technical papers" in the Miscellaneous Material section.
 I believe most of what it says is still current; the only thing I can see
 at a glance that is no longer correct is that in those days namespaces
 were optional in packages.  Now all packages have namespaces.
 Duncan Murdoch

 I'm also wondering more generally, why there is a need (practically
 speaking) for a distinction between the environment associated with a
 package and the environment associated with the namespace.
 $ export R_PROFILE=/home/btyner/Rprofile.site
 $ cat /home/btyner/Rprofile.site
 sys.source("/home/btyner/head1.R", envir = baseenv())
 sys.source("/home/btyner/head2.R", envir = .BaseNamespaceEnv)
 $ cat /home/btyner/head1.R
 head1 <- function(x) head(x)
 $ cat /home/btyner/head2.R
 head2 <- function(x) head(x)
 $ Rscript -e "head1(letters)"
 Error in head1(letters) : could not find function "head"
 Execution halted
 $ Rscript -e "head2(letters)"
 [1] "a" "b" "c" "d" "e" "f"
 $ Rscript -e "sessionInfo()"
 R version 3.0.1 (2013-05-16)
 Platform: x86_64-pc-linux-gnu (64-bit)
 locale:
  [1] LC_CTYPE=en_US.UTF-8   LC_NUMERIC=C
  [3] LC_TIME=en_US.UTF-8LC_COLLATE=en_US.UTF-8
  [5] LC_MONETARY=en_US.UTF-8LC_MESSAGES=en_US.UTF-8
  [7] LC_PAPER=C LC_NAME=C
  [9] LC_ADDRESS=C   LC_TELEPHONE=C
 [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
 attached base packages:
 [1] stats graphics  grDevices utils datasets  base
 Regards
 Ben
 __
 [2]R-help@r-project.org mailing list
 [3]https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide
 [4]http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.

   --
   

References

   1. http://cran.r-project.org/doc/Rnews/Rnews_2003-1.pdf
   2. mailto:R-help@r-project.org
   3. https://stat.ethz.ch/mailman/listinfo/r-help
   4. http://www.R-project.org/posting-guide.html
__
R-help@r-project.org mailing list
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] package environment versus namespace environment

2014-03-07 Thread Benjamin Tyner
Hello,

I realize that a function in  (for example, function
"head1" below) is unable to see (without resorting to "::", anyway)
objects in utils (for example, "head" below), since package:base is
after package:utils on the search path.

However, I'm wondering what is the machinery that allows a function in
 (for example, function "head2" below) to
be able to see "head" just fine, without needing to resort to "::".

I'm also wondering more generally, why there is a need (practically
speaking) for a distinction between the environment associated with a
package and the environment associated with the namespace.

   $ export R_PROFILE=/home/btyner/Rprofile.site

   $ cat /home/btyner/Rprofile.site
   sys.source("/home/btyner/head1.R", envir = baseenv())
   sys.source("/home/btyner/head2.R", envir = .BaseNamespaceEnv)

   $ cat /home/btyner/head1.R
   head1 <- function(x) head(x)

   $ cat /home/btyner/head2.R
   head2 <- function(x) head(x)

   $ Rscript -e "head1(letters)"
   Error in head1(letters) : could not find function "head"
   Execution halted

   $ Rscript -e "head2(letters)"
   [1] "a" "b" "c" "d" "e" "f"

   $ Rscript -e "sessionInfo()"
   R version 3.0.1 (2013-05-16)
   Platform: x86_64-pc-linux-gnu (64-bit)

   locale:
[1] LC_CTYPE=en_US.UTF-8   LC_NUMERIC=C 
[3] LC_TIME=en_US.UTF-8LC_COLLATE=en_US.UTF-8   
[5] LC_MONETARY=en_US.UTF-8LC_MESSAGES=en_US.UTF-8  
[7] LC_PAPER=C LC_NAME=C
[9] LC_ADDRESS=C   LC_TELEPHONE=C   
   [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C  

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

Regards
Ben

__
R-help@r-project.org mailing list
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] retaining comments within functions defined in Rprofile.site

2013-05-25 Thread Benjamin Tyner
Thank you Duncan; that is a most helpful explanation and suggestion.

On 05/25/2013 09:26 AM, Duncan Murdoch wrote:
> Those files are read in a hard-wired way quite early in the startup
> process, and there's no way to enable keep.source on them.  However,
> there's nothing to stop those files from calling source() on another
> file, and using keep.source=TRUE when they do so.  You could probably
> even hack something together so that Rprofile.site sourced itself, but
> it would be pretty easy to get into an infinite loop if you weren't
> careful...
>
>
> On Fri, May 24, 2013 at 5:46 PM, Benjamin Tyner  <mailto:bty...@gmail.com>> wrote:
>
> Hi,
>
> I have some functions defined within etc/Rprofile.site which contain
> embedded comments, but it seems the comments get stripped out when the
> site profile is sourced into the base namespace. I'm thinking I
> need to
> enable options(keep.source=TRUE) somewhere prior to the actual
> sourcing
> of the site profile, but haven't been able to figure it out yet. Does
> anyone have any suggestions?
>
> Thanks,
> Ben
>
>
> __
> R-help@r-project.org <mailto:R-help@r-project.org> mailing list
> 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
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] retaining comments within functions defined in Rprofile.site

2013-05-24 Thread Benjamin Tyner
Hi,

I have some functions defined within etc/Rprofile.site which contain
embedded comments, but it seems the comments get stripped out when the
site profile is sourced into the base namespace. I'm thinking I need to
enable options(keep.source=TRUE) somewhere prior to the actual sourcing
of the site profile, but haven't been able to figure it out yet. Does
anyone have any suggestions?

Thanks,
Ben

__
R-help@r-project.org mailing list
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] holding argument(s) fixed within lapply

2013-03-22 Thread Benjamin Tyner

For the record, one may also nest one do.call within another, e.g. via
functional::Curry

   lapply(X = mylist2,
  FUN = do.call,
  what = Curry(FUN = f2, y = Y)
  )



On 03/13/2013 12:22 PM, Blaser Nello wrote:
> One way is to use the do.call function. For example:
>
> ret2 <- lapply(X=mylist2, 
>FUN=do.call, 
>what=function(...) f2(y=Y, ...))
>
> Best, 
> Nello
>
> -Original Message-
> Date: Tue, 12 Mar 2013 22:37:52 -0400
> From: Benjamin Tyner 
> To: r-help@r-project.org
> Subject: Re: [R] holding argument(s) fixed within lapply
> Message-ID: <513fe680.2070...@gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
>
> Apologies; resending in plain text...
>
> Given a function with several arguments, I would like to perform an
> lapply (or equivalent) while holding one or more arguments fixed to some
> common value, and I would like to do it in as elegant a fashion as
> possible, without resorting to wrapping a separate wrapper for the
> function if possible. Moreover I would also like it to work in cases
> where one or more arguments to the original function has a default
> binding.
>
> # Here is an example; the original function
> f <- function(w, y, z){ w + y + z }
>
> # common value I would like y to take
> Y <- 5
>
> # I have a list of arguments for the lapply()
> mylist <- list(one = list(w = 1, z = 2),
>two = list(w = 3, z = 4)
>)
>
> # one way to do it involves a custom wrapper; I do not like this
> method
> ret <- lapply(FUN = function(x,...) f(w = x$w, z = x$z, ...),
>   X   = mylist,
>   y   = Y
>   )
>
> # another way
> ret <- lapply(FUN  = with.default,
>   X= mylist,
>   expr = f(w, y = Y, z)
>   )
>
> # yet another way
> ret <- lapply(FUN  = eval,
>   X= mylist,
>   expr = substitute(f(w, y = Y, z))
>   )
>
> # now, the part I'm stuck on is for a version of f where z has a
> default binding
> f2 <- function(w, y, z = 0){ w + y + z }
>
> # the same as mylist, but now z is optional
> mylist2 <- list(one = list(w = 1),
> two = list(w = 3, z = 4)
> )
>
> # undesired result (first element has length 0)
> ret2 <- lapply(FUN = function(x,...) f2(w = x$w, z = x$z, ),
>X   = mylist2,
>y   = Y
>)
>
> # errors out ('z' not found)
> ret2 <- lapply(FUN  = with.default,
>X= mylist2,
>expr = f2(w, y = Y, z)
>)
>
> # errors out again
> ret2 <- lapply(FUN  = eval,
>X= mylist2,
>expr = substitute(f2(w, y = Y, z))
>)
>
> # not quite...
> ret2 <- lapply(FUN = gtools::defmacro(y = Y, expr = f2(w, y = Y,
> z)),
>X   = mylist2
>)
>
> It seems like there are many ways to skin this cat; open to any and all
> guidance others care to offer.
>
> Regards,
> Ben
>


__
R-help@r-project.org mailing list
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] holding argument(s) fixed within lapply

2013-03-14 Thread Benjamin Tyner
Thank you Blaser; this is helpful.

On 03/13/2013 12:22 PM, Blaser Nello wrote:
> One way is to use the do.call function. For example:
>
> ret2 <- lapply(X=mylist2, 
>FUN=do.call, 
>what=function(...) f2(y=Y, ...))
>
> Best, 
> Nello
>
> -Original Message-
> Date: Tue, 12 Mar 2013 22:37:52 -0400
> From: Benjamin Tyner 
> To: r-help@r-project.org
> Subject: Re: [R] holding argument(s) fixed within lapply
> Message-ID: <513fe680.2070...@gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
>
> Apologies; resending in plain text...
>
> Given a function with several arguments, I would like to perform an
> lapply (or equivalent) while holding one or more arguments fixed to some
> common value, and I would like to do it in as elegant a fashion as
> possible, without resorting to wrapping a separate wrapper for the
> function if possible. Moreover I would also like it to work in cases
> where one or more arguments to the original function has a default
> binding.
>
> # Here is an example; the original function
> f <- function(w, y, z){ w + y + z }
>
> # common value I would like y to take
> Y <- 5
>
> # I have a list of arguments for the lapply()
> mylist <- list(one = list(w = 1, z = 2),
>two = list(w = 3, z = 4)
>)
>
> # one way to do it involves a custom wrapper; I do not like this
> method
> ret <- lapply(FUN = function(x,...) f(w = x$w, z = x$z, ...),
>   X   = mylist,
>   y   = Y
>   )
>
> # another way
> ret <- lapply(FUN  = with.default,
>   X= mylist,
>   expr = f(w, y = Y, z)
>   )
>
> # yet another way
> ret <- lapply(FUN  = eval,
>   X= mylist,
>   expr = substitute(f(w, y = Y, z))
>   )
>
> # now, the part I'm stuck on is for a version of f where z has a
> default binding
> f2 <- function(w, y, z = 0){ w + y + z }
>
> # the same as mylist, but now z is optional
> mylist2 <- list(one = list(w = 1),
> two = list(w = 3, z = 4)
> )
>
> # undesired result (first element has length 0)
> ret2 <- lapply(FUN = function(x,...) f2(w = x$w, z = x$z, ),
>X   = mylist2,
>y   = Y
>)
>
> # errors out ('z' not found)
> ret2 <- lapply(FUN  = with.default,
>X= mylist2,
>expr = f2(w, y = Y, z)
>)
>
> # errors out again
> ret2 <- lapply(FUN  = eval,
>X= mylist2,
>expr = substitute(f2(w, y = Y, z))
>)
>
> # not quite...
> ret2 <- lapply(FUN = gtools::defmacro(y = Y, expr = f2(w, y = Y,
> z)),
>X   = mylist2
>)
>
> It seems like there are many ways to skin this cat; open to any and all
> guidance others care to offer.
>
> Regards,
> Ben
>


__
R-help@r-project.org mailing list
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] holding argument(s) fixed within lapply

2013-03-12 Thread Benjamin Tyner
Apologies; resending in plain text...

Given a function with several arguments, I would like to perform an
lapply (or equivalent) while holding one or more arguments fixed to some
common value, and I would like to do it in as elegant a fashion as
possible, without resorting to wrapping a separate wrapper for the
function if possible. Moreover I would also like it to work in cases
where one or more arguments to the original function has a default binding.

# Here is an example; the original function
f <- function(w, y, z){ w + y + z }

# common value I would like y to take
Y <- 5

# I have a list of arguments for the lapply()
mylist <- list(one = list(w = 1, z = 2),
   two = list(w = 3, z = 4)
   )

# one way to do it involves a custom wrapper; I do not like this method
ret <- lapply(FUN = function(x,...) f(w = x$w, z = x$z, ...),
  X   = mylist,
  y   = Y
  )

# another way
ret <- lapply(FUN  = with.default,
  X= mylist,
  expr = f(w, y = Y, z)
  )

# yet another way
ret <- lapply(FUN  = eval,
  X= mylist,
  expr = substitute(f(w, y = Y, z))
  )

# now, the part I'm stuck on is for a version of f where z has a
default binding
f2 <- function(w, y, z = 0){ w + y + z }

# the same as mylist, but now z is optional
mylist2 <- list(one = list(w = 1),
two = list(w = 3, z = 4)
)

# undesired result (first element has length 0)
ret2 <- lapply(FUN = function(x,...) f2(w = x$w, z = x$z, ...),
   X   = mylist2,
   y   = Y
   )

# errors out ('z' not found)
ret2 <- lapply(FUN  = with.default,
   X= mylist2,
   expr = f2(w, y = Y, z)
   )

# errors out again
ret2 <- lapply(FUN  = eval,
   X= mylist2,
   expr = substitute(f2(w, y = Y, z))
   )

# not quite...
ret2 <- lapply(FUN = gtools::defmacro(y = Y, expr = f2(w, y = Y, z)),
   X   = mylist2
   )

It seems like there are many ways to skin this cat; open to any and all
guidance others care to offer.

Regards,
Ben




__
R-help@r-project.org mailing list
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] holding argument(s) fixed within lapply

2013-03-12 Thread Benjamin Tyner
|Hello,

Given a function with several arguments, I would like to perform an
lapply (or equivalent) while holding one or more arguments fixed to some
common value, and I would like to do it in as elegant a fashion as
possible, without resorting to wrapping a separate wrapper for the
function if possible. Moreover I would also like it to work in cases
where one or more arguments to the original function has a default binding.
|

|# Here is an example; the original function|||
|||f <- function(w, y, z){ w + y + z }|||

|||# common value I would like y to take|||
|||Y <- 5|||

|||# I have a list of arguments for the lapply()|||
|||mylist <- list(one = list(w = 1, z = 2),|||
|||   two = list(w = 3, z = 4)|||
|||   )|||

|||# one way to do it involves a custom wrapper||; I do not like
this method|
|||ret <- lapply(FUN = function(x,...) f(w = x$w, z = x$z, ...),|||
|||  X   = mylist,|||
|||  y   = Y|||
|||  )|||

|||# another way|||
|||ret <- lapply(FUN  = with.default,|||
|||  X= mylist,|||
|||  expr = f(w, y = Y, z)|||
|||  )|||

|||# yet another way|||
|||ret <- lapply(FUN  = eval,|||
|||  X= mylist,|||
|||  expr = substitute(f(w, y = Y, z))|||
|||  )|||

|||# now, the part I'm stuck on is for a version of f where z has a
default||binding|
|||f2 <- function(w, y, z = 0){ w + y + z }|||

|||# the same as mylist, but now z is optional|||
|||mylist2 <- list(one = list(w = 1),|||
|||two = list(w = 3, z = 4)|||
|||)|||

|||# undesired result||(first element has length 0)|
|||ret2 <- lapply(FUN = function(x,...) f2(w = x$w, z = x$z, ...),|||
|||   X   = mylist2,|||
|||   y   = Y|||
|||   )|||

|||# errors out ('z' not found)|||
|||ret2 <- lapply(FUN  = with.default,|||
|||   X= mylist2,|||
|||   expr = f2(w, y = Y, z)|||
|||   )|||

|||# errors out again|||
|||ret2 <- lapply(FUN  = eval,|||
|||   X= mylist2,|||
|||   expr = substitute(f2(w, y = Y, z))|||
|||   )||
||
||# not quite...||
||ret2 <- lapply(FUN = gtools::defmacro(y = Y, expr = f2(w, y = Y,
z)),||
||   X   = mylist2||
||   )|
||

|It seems like there are many ways to skin this cat; open to any and all
guidance others care to offer. |||

|Regards,
Ben
|

__
R-help@r-project.org mailing list
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] detecting entry into a recursive function

2013-02-17 Thread Benjamin Tyner
Thanks Jim -- I had considered this approach; is there any way to "hide" 
such arguments from users?


Jim Lemon wrote:

On 02/17/2013 12:55 PM, Benjamin Tyner wrote:

Given a function that calls itself, what's the best way to detect the
entry point? The best I came up with is:

IsEntryPoint <- function(){

par <- sys.call(-1L)[[1]]
grandpar <- sys.call(-2L)[[1]]

!identical(par, grandpar)
}

but this won't work for functions that don't directly call themselves;
for example, in this one the paste gets inserted in the call stack, so i
is always TRUE.

f <- function(d){

i <- IsEntryPoint()

if(d > 1L) paste(d, f(d-1L))
}

Any ideas?


Hi Ben,
There are a number of ways to do this, from simply having a flag that 
has one value at the initial call to the function and another value 
when the function calls itself. for an example of this, see the code 
for the "sizetree" function in the plotrix package, in particular the 
"firstcall" argument.


Jim



__
R-help@r-project.org mailing list
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] detecting entry into a recursive function

2013-02-16 Thread Benjamin Tyner
Given a function that calls itself, what's the best way to detect the 
entry point? The best I came up with is:


  IsEntryPoint <- function(){

par <- sys.call(-1L)[[1]]
grandpar <- sys.call(-2L)[[1]]

!identical(par, grandpar)
  }

but this won't work for functions that don't directly call themselves; 
for example, in this one the paste gets inserted in the call stack, so i 
is always TRUE.


  f <- function(d){

i <- IsEntryPoint()

if(d > 1L) paste(d, f(d-1L))
  }

Any ideas?

Regards,
Ben


__
R-help@r-project.org mailing list
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] draw.key; getting grobWidth to respect fontfamily="mono"

2012-09-12 Thread Benjamin Tyner

Many thanks Paul, that is a nifty way to do it.

Regards
Ben

Paul Murrell wrote:

Hi

On 07/09/12 09:35, Benjamin Tyner wrote:

Update: seems one way to skin this cat is to add

   gp = gpar(fontfamily="mono")

to the viewport() call itself. If anyone has any suggestions for a
robust way to extract this piece information from the key grob itself
(it's nested several levels deep), I'm all ears.


I'm going to try to blame 'lattice' for this problem (because it 
appears to be generating "strwidth" units for the text entries in the 
key rather than "grobwidth" units [so the 'gp' settings on the text 
grobs are being ignored when widths are calculated]).  (Though 
'lattice' is probably doing that to avoid using packGrob() and being 
further slowed down by 'grid'!)


However, since you are already deep enough into 'grid' to be using 
viewports and grobWidth() yourself, I suspect that you could get what 
you want by avoiding draw.key() altogether.  You could generate a text 
grob yourself (based on your 'GOFlist'), directly query it for its 
width, build a viewport based on that width (similar to the viewport 
that you are already creating), push the viewport and draw the grob.

Something like this ...

GOFgrob <- function(x) {
textGrob(c(x$title, x$text$label),
 x=.5, y=unit(.5, "npc") + unit(1:-1, "lines"),
 gp=gpar(fontfamily=c("sans", "mono", "mono")))
}

plot <- xyplot(actual + forecast ~ mm | cond,
   data = Data,
   layout = c(1,2),
   type = "l",
   panel = function(x,y,GOFlist,...){
 panel.xyplot(x,y,...)
 pn <- panel.number()
 key <- GOFgrob(GOFlist[[pn]])
 vp <- viewport(x = 1,
y = 1,
width = grobWidth(key) +
unit(2, "lines"),
height = grobHeight(key) +
 unit(1, "lines"),
just = c("right","top")
)
 pushViewport(vp)
 grid.draw(key)
 grid.rect()
 popViewport()
   },
   GOFlist = GOFlist
   )

Hope that helps.

Paul


Thanks
Ben

__
R-help@r-project.org mailing list
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
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] draw.key; getting grobWidth to respect fontfamily="mono"

2012-09-06 Thread Benjamin Tyner

Update: seems one way to skin this cat is to add

  gp = gpar(fontfamily="mono")

to the viewport() call itself. If anyone has any suggestions for a 
robust way to extract this piece information from the key grob itself 
(it's nested several levels deep), I'm all ears.


Thanks
Ben

__
R-help@r-project.org mailing list
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] draw.key; getting grobWidth to respect fontfamily="mono"

2012-09-06 Thread Benjamin Tyner

Hello

Within my panel function, I am using draw.key to create a custom key for 
each panel. I'm trying to use grobWidth to calculate the correct width 
for the viewport, but it appears to be disregarding the fontfamily when 
computing the width. Attached is an example; any ideas what I'm doing wrong?


Many thanks
Ben
build.args.GOF.legend <- function(actual,
  forecast,
  weight
  ){


  RMSE <- sqrt(sum(weight * (actual - forecast)^2)/sum(weight))
  RMSE.scaled <- sqrt(sum(weight * (actual - forecast)^2) * 
sum(weight))/sum(actual * weight)

  mat <- format(cbind(RMSE, RMSE.scaled))

  rownames(mat) <- "full sample"
  colnames(mat) <- c("RMSE","RMSE/MeanActual")

  txt <- capture.output(print.noquote(mat))

  args <- list(text = list(label  = txt,
   fontfamily = "mono"
   ),
   border = TRUE,
   title = "goodness of fit statistics",
   cex.title = 1
   )

  return(args)
}

Data <- data.frame(mm = rep(201001:201012, times=2L),
   actual = runif(12*2),
   forecast = runif(12*2),
   weight = c(cumsum(runif(12)), cumsum(runif(12))),
   cond   = gl(2, 12)
   )

GOFlist <- by(data = subset(Data, select = -c(cond, mm)),
  INDICES = Data$cond,
  FUN = function(x) do.call(build.args.GOF.legend, x),
  simplify = FALSE
  )

library(lattice)
library(grid)

plot <- xyplot(actual + forecast ~ mm | cond,
   data = Data,
   layout = c(1,2),
   type = "l",
   panel = function(x,y,GOFlist,...){
 panel.xyplot(x,y,...)
 pn <- panel.number()
 key <- draw.key(key = GOFlist[[pn]], draw = FALSE)
 vp <- viewport(x = 1,
y = 1,
width = grobWidth(key),
height = grobHeight(key),
just = c("right","top")
)
 pushViewport(vp)
 grid.draw(key)
 popViewport()
   },
   GOFlist = GOFlist
   )

print(plot)
__
R-help@r-project.org mailing list
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] optparse::parse_args, using equals sign or not

2012-02-08 Thread Benjamin Tyner

Hi

We've found that when using parse_args(..., positional_arguments=FALSE), 
it is permissible to invoke our script with either "--myfoo=bar" or 
"--myfoo bar"; that is, whether or not the equals sign is present makes 
no difference, and in fact both usage forms are demonstrated in the 
optparse vignette.


However, we've found that when using parse_args(..., 
positional_arguments=TRUE), it is no longer possible to use the equals 
sign; that is, "--myfoo=bar" will show up as a positional argument, with 
"myfoo" set to its default value instead.


Wondering if this is really the intended behavior? Some of our scripts 
use positional arguments and some do not, and we'd like to arrive at a 
standard usage across all scripts.


Many thanks,
Ben
__
R-help@r-project.org mailing list
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] try() with silent=TRUE not preventing printing of error message

2012-01-10 Thread Benjamin Tyner

Hello,

We're curious to know why, on builds lacking Tcl/Tk,

   try(library(tcltk),silent=TRUE)

still allows the message to be printed:

   Error in firstlib(which.lib.loc, package) :
 Tcl/Tk support is not available on this system

though it does succeed in trapping the error condition?

Thanks,
Ben

__
R-help@r-project.org mailing list
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] installing binary packages across windows cluster

2011-12-09 Thread Benjamin Tyner

Hi

Given a "cluster" of identical windows machines all running the same 
version of R, are there any circumstances where using install.packages() 
to install a package (say, one that doesn't have any dependencies) on 
just a single machine, then copying the resulting installed package 
subdirectory across the cluster into all the other machine's .Library 
subdirectories would be a bad idea?


Thanks,
Ben
__
R-help@r-project.org mailing list
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] package / function for monitoring processes?

2011-09-20 Thread Benjamin Tyner

Hi

I recall running across a function a while back which would return 
information about running processes (such as their cpu and memory 
usage), but I cannot seem to locate it. Wondering if someone would be 
kind enough to refresh my memory. I vaguely recall it was parsing the 
output of the 'ps' command.


Thanks,
Ben


__
R-help@r-project.org mailing list
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] 'Rscript -e' and stdout() puzzle

2011-06-23 Thread Benjamin Tyner

Thanks Bill! Next time, I'll try literacy.

William Dunlap wrote:

-Original Message-
From: r-help-boun...@r-project.org 
[mailto:r-help-boun...@r-project.org] On Behalf Of Benjamin Tyner

Sent: Thursday, June 23, 2011 5:31 PM
To: r-help@r-project.org
Subject: [R] 'Rscript -e' and stdout() puzzle

Hello,

I am curious to know why the output of

Rscript -e "cat(R.version.string,stdout())"

includes a trailing " 1", whereas

Rscript -e "cat(R.version.string)"



Use file=stdout().  Otherwise it prints the value of
as.character(stdout()), which is 1.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

  
does not. I have tried various mechanisms to subvert this 
behavior, such as


Rscript -e "invisible(con<-stdout()); cat(R.version.string, con);
rm(con); q()"

but the trailing " 1" remains.

Thanks,
Ben






__
R-help@r-project.org mailing list
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] 'Rscript -e' and stdout() puzzle

2011-06-23 Thread Benjamin Tyner

Hello,

I am curious to know why the output of

   Rscript -e "cat(R.version.string,stdout())"

includes a trailing " 1", whereas

   Rscript -e "cat(R.version.string)"

does not. I have tried various mechanisms to subvert this behavior, such as

   Rscript -e "invisible(con<-stdout()); cat(R.version.string, con);
   rm(con); q()"

but the trailing " 1" remains.

Thanks,
Ben


__
R-help@r-project.org mailing list
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] best practice(s) for retrieving a local variable from a closure

2011-04-09 Thread Benjamin Tyner

Thanks Bill and Gabor!

William Dunlap wrote:

-Original Message-
From: r-help-boun...@r-project.org 
[mailto:r-help-boun...@r-project.org] On Behalf Of Benjamin Tyner

Sent: Friday, April 08, 2011 6:48 PM
To: r-help@r-project.org
Subject: [R] best practice(s) for retrieving a local variable 
from a closure


Greetings,

Say I have defined

  mp <- function(a) function(x) x^a

  f2 <- mp(2)

and I would like to retrieve the "a" which is local to f2. 
Two options 
come to mind;


  get("a", envir=environment(f2))
  eval(substitute(a), environment(f2))

I'm curious if one of these is preferred over the other in terms of 
efficiency, robustness, aesthetics, etc. Or perhaps someone can share 
another way to skin this cat, besides wrappers around eval() like


  evalq(a, environment(f2))
  with(environment(f2), a)



You can also use a list-like notation with environments:
  > environment(f2)$a
  [1] 2
  > environment(f2)[["a"]]
  [1] 2
  > environment(f2)$a <- 3:1
  > f2(2)
  [1] 8 4 2

The above feels a bit like snooping where I wasn't invited.
You could do something like
  mq <- function(a) {
 force(a)
 list(getA = function()a,
  setA = function(newA) a <<- newA,
  fun = function(x)x^a
 )
  }
to make it clear that you expect people to look at or change
fun's 'a'.
  > f3 <- mq(3)
  > f3$fun(5)
  [1] 125
  > f3$getA()
  [1] 3
  > f3$setA(4:1)
  > f3$fun(3)
  [1] 81 27  9  3

I don't code like this much so haven't developed a sense of
aesthetics about these variants.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

  
Also, I'd like to confirm that I have used the correct terminology in 
the subject line, that "a" is a local variable with respect 
to f2, which 
is a closure?


Thanks,
Ben




__
R-help@r-project.org mailing list
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] best practice(s) for retrieving a local variable from a closure

2011-04-08 Thread Benjamin Tyner

Greetings,

Say I have defined

 mp <- function(a) function(x) x^a

 f2 <- mp(2)

and I would like to retrieve the "a" which is local to f2. Two options 
come to mind;


 get("a", envir=environment(f2))
 eval(substitute(a), environment(f2))

I'm curious if one of these is preferred over the other in terms of 
efficiency, robustness, aesthetics, etc. Or perhaps someone can share 
another way to skin this cat, besides wrappers around eval() like


 evalq(a, environment(f2))
 with(environment(f2), a)

Also, I'd like to confirm that I have used the correct terminology in 
the subject line, that "a" is a local variable with respect to f2, which 
is a closure?


Thanks,
Ben
__
R-help@r-project.org mailing list
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] lattice: par.settings: custom axis.line by panel

2011-04-08 Thread Benjamin Tyner

Thanks Deepayan!

I neglected to mention that I would want to retain ticks as well for the 
boxed panel(s), so perhaps something like


xyplot(y ~ x|f,
  data = Data,
  layout=c(2,1),
  scales=list(relation="free",alternating=FALSE),
  par.settings = list(axis.line=list(col = "transparent")),
  axis = function(side, line.col, ...){
  bool <- panel.number() %in% c(1)
  axis.default(side = side,
   line.col = if(bool) "black" else "transparent",
   ...)
  if(side=="bottom" & bool) grid::grid.rect(gp=grid::gpar(lty=1))
  }
  )

However, I noticed that if my panel function is drawing beyond the 
border, that it overlaps with the rectangle drawn by the axis function. 
For example, if I add the panel function:


panel=function(x,y,...){
panel.polygon(x = c(0.4,0.6,0.6,0.4),
  y = c(-10,-10,10,10),
  col = "yellow")
panel.xyplot(x,y,...)
  }

then the bottom border on panel 1 is obscured...any suggestions?

Thanks again,
Ben

Deepayan Sarkar wrote:

On Fri, Apr 8, 2011 at 6:10 AM, Benjamin Tyner  wrote:
  

Hello,

In lattice, is there a way to customize the axis.line by panel? For example,
say I have two panels:

Data <- data.frame(x=runif(10),
 y=rnorm(10),
 f=gl(2,5)
 )

library(lattice)
plot <- xyplot(y ~ x|f,
 data = Data,
 layout=c(2,1),
 scales=list(relation="free",alternating=FALSE),
 par.settings = list(axis.line=list(lwd=0))
 )

and desire lwd=0 for one panel and lwd=1 for the other. Can it be done?



No.

  

If not, can the desired behavior be mimicked by explicit use of (say)
grid::grid.rect() within the panel function?



Yes, but using the axis function would be better as panel function
clips by default (so technically only half of each line would be
visible).

xyplot(y ~ x|f,
   data = Data,
   layout=c(2,1),
   scales=list(relation="free",alternating=FALSE),
   par.settings = list(axis.line=list(col = "transparent")),
   axis = function(side, ...) {
   ## axis is called 4 times per panel; draw box only once
   if (side == "bottom")
   {
   box.lty <-
   if (panel.number() %in% c(1)) # add others as needed
   0
   else
   1
   grid::grid.rect(gp = grid::gpar(lty = box.lty))
   }
   axis.default(side = side, ...)
   })

-Deepayan
  


__
R-help@r-project.org mailing list
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] lattice: par.settings: custom axis.line by panel

2011-04-07 Thread Benjamin Tyner

Hello,

In lattice, is there a way to customize the axis.line by panel? For 
example, say I have two panels:


Data <- data.frame(x=runif(10),
  y=rnorm(10),
  f=gl(2,5)
  )

library(lattice)
plot <- xyplot(y ~ x|f,
  data = Data,
  layout=c(2,1),
  scales=list(relation="free",alternating=FALSE),
  par.settings = list(axis.line=list(lwd=0))
  )

and desire lwd=0 for one panel and lwd=1 for the other. Can it be done? 
If not, can the desired behavior be mimicked by explicit use of (say) 
grid::grid.rect() within the panel function?


Thanks,
Ben
__
R-help@r-project.org mailing list
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] dump() an object of type raw ?

2009-07-21 Thread Benjamin Tyner
Thank you David for taking the time to respond to my question. Perhaps I 
should clarify: the man page says "a 'dump' file can be 'source'd"; have 
I taken the documentation too literally in this case?


David Winsemius wrote:


On Jul 21, 2009, at 7:19 PM, Benjamin Tyner wrote:


Is there a way to do this? I tried

 x <- writeBin(pi, raw())
 dump("x","x.R")
 source("x.R")

but is seems x.R is not source()-able, as it contains an unexpected 
symbol.




Yes, "2d".I get:
> source("/Users/davidwinsemius/x.R")
Error in source("/Users/davidwinsemius/x.R") :
  /Users/davidwinsemius/x.R:2:8: unexpected symbol
1: x <-
2: c(18, 2d
  ^

So there was an good faith effort to source the file but the 
interpreter was not put on notice that it would be getting 
hexadecimal. The file contains:


x <-
c(18, 2d, 44, 54, fb, 21, 09, 40)

and when you try to execute that from the command line you get:

> x <-
+ c(18, 2d, 44, 54, fb, 21, 09, 40)
Error: syntax error

(R variable names cannot start with 2 and R wouldn't assume these are 
hexadecimal numbers.)



David Winsemius, MD
Heritage Laboratories
West Hartford, CT



__
R-help@r-project.org mailing list
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] dump() an object of type raw ?

2009-07-21 Thread Benjamin Tyner

Is there a way to do this? I tried

  x <- writeBin(pi, raw())
  dump("x","x.R")
  source("x.R")

but is seems x.R is not source()-able, as it contains an unexpected symbol.

Thanks
Ben

__
R-help@r-project.org mailing list
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] lattice::xyplot axis padding with fontfamily="mono"

2009-05-28 Thread Benjamin Tyner

Deepayan,

Many thanks for the quick response and suggested workaround.

Best,
Ben



Deepayan Sarkar wrote:

On Wed, May 27, 2009 at 4:38 PM, Benjamin Tyner  wrote:
  

Hello,

Say I have a predictor taking a very wide value:

 Data <- data.frame(pred="a",resp=1)

 print(xyplot(pred~resp, data=Data)) # enough y-axis padding to accommodate
the wide label

 print(xyplot(pred~resp, data=Data,scales=list(fontfamily="mono"))) # not
enough padding



It's a bug in the layout calculations (fontfamily is not used).

  

What's the recommended way to have enough padding allocated?



Ideally by fixing the bug, but that may not be easy to fix in the
short run. For one-off examples, you could increase the width
manually:

xyplot(pred~resp, data=Data,scales=list(fontfamily="mono"),
par.settings = list(layout.widths = list(axis.left = 1.1)))

-Deepayan
  


__
R-help@r-project.org mailing list
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] lattice::xyplot axis padding with fontfamily="mono"

2009-05-27 Thread Benjamin Tyner

Hello,

Say I have a predictor taking a very wide value:

  Data <- 
data.frame(pred="a",resp=1)


  print(xyplot(pred~resp, data=Data)) # enough y-axis padding to 
accommodate the wide label


  print(xyplot(pred~resp, data=Data,scales=list(fontfamily="mono"))) # 
not enough padding


What's the recommended way to have enough padding allocated?

Thank you
Ben
__
R-help@r-project.org mailing list
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] get() versus getAnywhere()

2009-04-16 Thread Benjamin Tyner
Many thanks Duncan. Perhaps this merits a more explicit note in the 
documentation?


Duncan Murdoch wrote:

On 4/16/2009 9:52 AM, Benjamin Tyner wrote:

Hi

Using R 2.8.1. I have list object called "AuxData". Inside a browser(),

   get("AuxData")

succeeds, while

   getAnywhere("AuxData")

fails with the error "no object named âAuxDataâ was found". I'm
curious to know if this could be a bug. If yes, I'll try to come up
with a reproducible example.


It's not a bug.  getAnywhere doesn't really mean "anywhere", it means 
"visible on the search path, registered as an S3 method or in a name 
space but not exported."  On the other hand, the browser will show you 
the local evaluation frame.  So I'd expect the browser to be able to 
see x below, but getAnywhere wouldn't find it, because it's not in one 
of the places listed above.


f <- function(x) {
  browser()
  getAnywhere("x")
}

f(1)



__
R-help@r-project.org mailing list
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] get() versus getAnywhere()

2009-04-16 Thread Benjamin Tyner
Hi

Using R 2.8.1. I have list object called "AuxData". Inside a browser(),

   get("AuxData")

succeeds, while

   getAnywhere("AuxData")

fails with the error "no object named âAuxDataâ was found". I'm
curious to know if this could be a bug. If yes, I'll try to come up
with a reproducible example.

Thanks
Ben

__
R-help@r-project.org mailing list
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] monotonic GAM with more than one term

2009-03-03 Thread Benjamin Tyner

Simon,

Many thanks, that is a very helpful example. It seems quite general in 
that it also works under bs="cr" as well as with fx=TRUE.


-Ben
There's an example at the end of the pcls help file in version 1.5-0 --- just 
submitted to CRAN.


best,
Simon
  
__
R-help@r-project.org mailing list
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] monotonic GAM with more than one term

2009-02-25 Thread Benjamin Tyner
Hi,

Does anyone know how to fit a GAM where one or more smooth terms are
constrained to be monotonic, in the presence of "by" variables or
other terms? I looked at the example in ?pcls but so far have not been
able to adapt it to the case where there is more than one predictor.
For example,

require(mgcv)

set.seed(0)
n<-100


# Generate data from a monotonic truth.
x<-runif(100)*4-1
x<-sort(x)

m <- structure(rep(1:2,50), .Label=c("one","two"), class="factor")

f<-as.integer(m) * exp(4*x)/(1+exp(4*x))
y<-f+rnorm(100)*0.1
plot(x,y,xlim=c(min(x), max(x)*2))
dat<-data.frame(x=x,m=m,y=y)



# Show regular spline fit (and save fitted object)
f.ug<-gam(y~m+s(x,k=10,by=m,bs="cr"))

bool <- m=="one"
yhat <- fitted(f.ug)

lines(x[bool],yhat[bool])
lines(x[!bool],yhat[!bool])

xx <- seq(max(x), 2*max(x), length=100)
mm <- structure(rep(1:2,50), .Label=c("one","two"), class="factor")
yy <- predict(f.ug, newdata=data.frame(m=mm,x=xx))

bool <- mm=="one"

lines(xx[bool],yy[bool], lty=2) # show effect of extrapolation
lines(xx[!bool],yy[!bool], lty=2)


# this is where I start running into trouble
   sm<-smoothCon(s(x,k=10,by=m,bs="cr"),dat,knots=NULL)[[1]]
   FF<-mono.con(sm$xp);   # get constraints
   G<-list(y=y,
   w=rep(1, n),
   X=sm$X,
   C=matrix(0,0,0),
   S = sm$S,
   off = 0,
   sp=f.ug$sp,
   p=sm$xp,
   Ain = FF$A,
   bin = FF$b
   )


p<-pcls(G)  # fit spline (using s.p. from unconstrained fit)
fv<-Predict.matrix(sm,data.frame(x=x))%*%p

# can we do this without calling smoothCon directly ?
# also having trouble here.
   f.nofit<-gam(y~m+s(x,k=10,by=m,bs="cr"),fit=FALSE)
   FF2 <- mono.con(f.nofit$smooth[[1]]$xp)

   stopifnot(identical(FF, FF2))

   G2 <- list(y = f.nofit$y,
  w = f.nofit$w,
  X = f.nofit$X,
  C = f.nofit$C,
  S = f.nofit$smooth[[1]]$S,
  off = f.nofit$off,
  sp = f.ug$sp,
  p = f.nofit$smooth[[1]]$xp,
  Ain = FF2$A,
  bin = FF2$b
  )

p2 <- pcls(G2)
fv2<-Predict.matrix(f.nofit$smooth[[1]],data.frame(x=x))%*%p2



Many thanks
-Ben

__
R-help@r-project.org mailing list
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] corMatrix crashes R 2.5.1 (windows XP) with corARMA structure

2007-10-01 Thread Benjamin Tyner
R-helpers,

n <- 100
arcoefs <- c(0.8)
macoefs <- c(-0.6)
p <- length(arcoefs)
q <- length(macoefs)
require(nlme)
tmp <- corARMA(value=c(arcoefs,macoefs), form=~1, p=p, q=q)
Sigma <- corMatrix(tmp, covariate = 1:n) # results in segfault

Have I used these commands in an improper way?

Thanks
Ben

__
R-help@r-project.org mailing list
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.