[Rd] use() might take a list of packages

2011-11-11 Thread Timothy Bates
I heard that a function called use() had been mooted as being more intuitively 
named than library()

If so, it might be handy if this worked

use(c("MASS","car”))

currently this fails…

library(c("MASS","car"))
__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Question on parsing R code from C

2011-11-11 Thread KR
First of all thanks a lot to you both for all the replies, they have been of 
great help to me!

I got the basic embedding running, however I still have some issues to solve in 
order to complete the interface to R I am working on:

1. I initialize with Rf_initEmbeddR().
- Is there a way to have more than one R "state" (per single thread)? Or is 
everything necessarily global?
- I also read somewhere that the fpu settings may be touched. Is there a way to 
make sure that this is not the case? Is this related to fpu_setup()?

2. I create a string and parse it via Rf_mkString() and R_parseVector() (yes I 
protect/un-protect the SEXPs).
- Is it possible to obtain the precise error message, like "unexpected symbol 
in..." (as would be reported by R.exe) in case of error as a const char* string?
- If I pass a wrongly escaped string (for instance 'ggsave("C:\gtest.png")', 
please notice the missing \) I get on stderr: Error '\g' is an unrecognized 
escape and I get a crash. This does not happen if for instance i try to parse 
'rnorm(10a10)' in which case I get the error flag and the NULL ptr return. I 
suspect I need to initialize something / set a callback to avoid this but I am 
not sure...

3. I eval with R_tryEvalSilent().
- Again, is it possible to obtain the precise error message in case of 
evaluation error as would be reported from R.exe?

4. Regarding the returned result of evaluation.
- What is the correct way to obtain a const char* string representing the 
result 
as would be "printed" in the R shell by executing a command (for instance 
"summary(c(1,2,3))") ?
- Why is the return type (as reported from typeof in R and TYPEOF in C) of 
"summary(c(1,2,3))" a double?

Any help / feedback on these issues would be greatly appreciated.

Thanks again.

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


Re: [Rd] use() might take a list of packages

2011-11-11 Thread Keith Jewell

"Timothy Bates"  wrote in message 
news:1b5e1d00-397b-429b-b20c-0fba06084...@gmail.com...
I heard that a function called use() had been mooted as being more 
intuitively named than library()

If so, it might be handy if this worked

use(c("MASS","car”))

currently this fails…

library(c("MASS","car"))

This seems to work, and doesn't seem much effort

> sapply(c("MASS","car"), library, character.only = TRUE)

KJ

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


Re: [Rd] Question on parsing R code from C

2011-11-11 Thread Simon Urbanek

On Nov 10, 2011, at 6:24 PM, KR wrote:

> First of all thanks a lot to you both for all the replies, they have been of 
> great help to me!
> 
> I got the basic embedding running, however I still have some issues to solve 
> in 
> order to complete the interface to R I am working on:
> 
> 1. I initialize with Rf_initEmbeddR().
> - Is there a way to have more than one R "state" (per single thread)? Or is 
> everything necessarily global?

There are no threads in R. You have only one, global instance of R.


> - I also read somewhere that the fpu settings may be touched. Is there a way 
> to 
> make sure that this is not the case? Is this related to fpu_setup()?
> 

All following answers assume that you're not running REPL. If you did, you 
would get all the errors on the console callback, so I'm assuming that's not 
what you want.


> 2. I create a string and parse it via Rf_mkString() and R_parseVector() (yes 
> I 
> protect/un-protect the SEXPs).
> - Is it possible to obtain the precise error message, like "unexpected symbol 
> in..." (as would be reported by R.exe) in case of error as a const char* 
> string?

AFAIR you have to evaluate parse(text=...) for that, there is no C-level access 
to parser errors.


> - If I pass a wrongly escaped string (for instance 'ggsave("C:\gtest.png")', 
> please notice the missing \) I get on stderr: Error '\g' is an unrecognized 
> escape and I get a crash. This does not happen if for instance i try to parse 
> 'rnorm(10a10)' in which case I get the error flag and the NULL ptr return. I 
> suspect I need to initialize something / set a callback to avoid this but I 
> am 
> not sure...
> 

If you get a crash, you're not setting up you R correctly. If your R quits then 
you are in non-interactive mode and you didn't setup an error handler.


> 3. I eval with R_tryEvalSilent().
> - Again, is it possible to obtain the precise error message in case of 
> evaluation error as would be reported from R.exe?
> 

I prefer using Rf_eval() and try(..., silent=TRUE) - you can check on the class 
of the result to see if there was an error.


> 4. Regarding the returned result of evaluation.
> - What is the correct way to obtain a const char* string representing the 
> result 
> as would be "printed" in the R shell by executing a command (for instance 
> "summary(c(1,2,3))") ?

See ?capture.output


> - Why is the return type (as reported from typeof in R and TYPEOF in C) of 
> "summary(c(1,2,3))" a double?
> 

Because the constants 1, 2 and 3 are all doubles. For example 1L is an integer 
and "1" is a string.

BTW: you are asking a lot of questions the are answered in the Rserve FAQ 
(since what you do is exactly what Rserve provides) so you may want to have a 
quick look:
http://rforge.net/Rserve/faq.html

Cheers,
Simon



> Any help / feedback on these issues would be greatly appreciated.
> 
> Thanks again.
> 
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 
> 

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


Re: [Rd] Question on parsing R code from C

2011-11-11 Thread Simon Urbanek

On Nov 11, 2011, at 10:08 AM, Simon Urbanek wrote:

> 
> On Nov 10, 2011, at 6:24 PM, KR wrote:
> 
>> First of all thanks a lot to you both for all the replies, they have been of 
>> great help to me!
>> 
>> I got the basic embedding running, however I still have some issues to solve 
>> in 
>> order to complete the interface to R I am working on:
>> 
>> 1. I initialize with Rf_initEmbeddR().
>> - Is there a way to have more than one R "state" (per single thread)? Or is 
>> everything necessarily global?
> 
> There are no threads in R. You have only one, global instance of R.
> 
> 
>> - I also read somewhere that the fpu settings may be touched. Is there a way 
>> to 
>> make sure that this is not the case? Is this related to fpu_setup()?
>> 
> 
> All following answers assume that you're not running REPL. If you did, you 
> would get all the errors on the console callback, so I'm assuming that's not 
> what you want.
> 
> 
>> 2. I create a string and parse it via Rf_mkString() and R_parseVector() (yes 
>> I 
>> protect/un-protect the SEXPs).
>> - Is it possible to obtain the precise error message, like "unexpected 
>> symbol 
>> in..." (as would be reported by R.exe) in case of error as a const char* 
>> string?
> 
> AFAIR you have to evaluate parse(text=...) for that, there is no C-level 
> access to parser errors.
> 
> 
>> - If I pass a wrongly escaped string (for instance 'ggsave("C:\gtest.png")', 
>> please notice the missing \) I get on stderr: Error '\g' is an unrecognized 
>> escape and I get a crash. This does not happen if for instance i try to 
>> parse 
>> 'rnorm(10a10)' in which case I get the error flag and the NULL ptr return. I 
>> suspect I need to initialize something / set a callback to avoid this but I 
>> am 
>> not sure...
>> 
> 
> If you get a crash, you're not setting up you R correctly. If your R quits 
> then you are in non-interactive mode and you didn't setup an error handler.
> 
> 
>> 3. I eval with R_tryEvalSilent().
>> - Again, is it possible to obtain the precise error message in case of 
>> evaluation error as would be reported from R.exe?
>> 
> 

For completeness: geterrmessage() R function returns the last error message 


> I prefer using Rf_eval() and try(..., silent=TRUE) - you can check on the 
> class of the result to see if there was an error.
> 
> 
>> 4. Regarding the returned result of evaluation.
>> - What is the correct way to obtain a const char* string representing the 
>> result 
>> as would be "printed" in the R shell by executing a command (for instance 
>> "summary(c(1,2,3))") ?
> 
> See ?capture.output
> 
> 
>> - Why is the return type (as reported from typeof in R and TYPEOF in C) of 
>> "summary(c(1,2,3))" a double?
>> 
> 
> Because the constants 1, 2 and 3 are all doubles. For example 1L is an 
> integer and "1" is a string.
> 
> BTW: you are asking a lot of questions the are answered in the Rserve FAQ 
> (since what you do is exactly what Rserve provides) so you may want to have a 
> quick look:
> http://rforge.net/Rserve/faq.html
> 
> Cheers,
> Simon
> 
> 
> 
>> Any help / feedback on these issues would be greatly appreciated.
>> 
>> Thanks again.
>> 
>> __
>> R-devel@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>> 
>> 
> 

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


[Rd] One step way to create data frame with variable "variable names"?

2011-11-11 Thread Paul Johnson
Suppose

plotx <- "someName"
modx <- "otherName"
plotxRange <- c(10,20)
modxVals <- c(1,2,3)

It often happens I want to create a dataframe or object with plotx or
modx as the variable names.  But can't understand syntax to do that.

I can get this done in 2 steps, creating the data frame and then
assigning names, as in

newdf <- data.frame( c(1, 2, 3, 4), c(4, 4, 4, 4))
colnames(newdf) <- c(plotx, modx)

I was trying to hit this in one step, but can't find how.  If I could
get this in one step, it would simplify some book keeping, because
this current method requires me to build up the name vector to match
newdf, and sometimes I make mistakes.

After the data.frame already exists, I understand I can insert new
variables using the variable names with syntax like

newdf[[plotx]] <- c(23, 23, 23, 23)

PJ

-- 
Paul E. Johnson
Professor, Political Science
1541 Lilac Lane, Room 504
University of Kansas

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


Re: [Rd] One step way to create data frame with variable "variable names"?

2011-11-11 Thread Gabor Grothendieck
On Fri, Nov 11, 2011 at 10:25 AM, Paul Johnson  wrote:
> Suppose
>
> plotx <- "someName"
> modx <- "otherName"
> plotxRange <- c(10,20)
> modxVals <- c(1,2,3)
>
> It often happens I want to create a dataframe or object with plotx or
> modx as the variable names.  But can't understand syntax to do that.
>
> I can get this done in 2 steps, creating the data frame and then
> assigning names, as in
>
> newdf <- data.frame( c(1, 2, 3, 4), c(4, 4, 4, 4))
> colnames(newdf) <- c(plotx, modx)
>
> I was trying to hit this in one step, but can't find how.  If I could
> get this in one step, it would simplify some book keeping, because
> this current method requires me to build up the name vector to match
> newdf, and sometimes I make mistakes.
>
> After the data.frame already exists, I understand I can insert new
> variables using the variable names with syntax like
>
> newdf[[plotx]] <- c(23, 23, 23, 23)
>


Try this:

newdf <- setNames(data.frame( c(1, 2, 3, 4), c(4, 4, 4, 4)), c(plotx, modx))

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

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


Re: [Rd] One step way to create data frame with variable "variable names"?

2011-11-11 Thread William Dunlap
> plotx <- "someName"
> modx <- "otherName"
> data.frame( structure(list(c(1, 2, 3, 4)), names=plotx), structure(list(c(4, 
> 4, 4, 4)), names=modx))
  someName otherName
11 4
22 4
33 4
44 4

(I think this is more of an R-help question.)

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> -Original Message-
> From: r-devel-boun...@r-project.org [mailto:r-devel-boun...@r-project.org] On 
> Behalf Of Paul Johnson
> Sent: Friday, November 11, 2011 7:25 AM
> To: R Devel List
> Subject: [Rd] One step way to create data frame with variable "variable 
> names"?
> 
> Suppose
> 
> plotx <- "someName"
> modx <- "otherName"
> plotxRange <- c(10,20)
> modxVals <- c(1,2,3)
> 
> It often happens I want to create a dataframe or object with plotx or
> modx as the variable names.  But can't understand syntax to do that.
> 
> I can get this done in 2 steps, creating the data frame and then
> assigning names, as in
> 
> newdf <- data.frame( c(1, 2, 3, 4), c(4, 4, 4, 4))
> colnames(newdf) <- c(plotx, modx)
> 
> I was trying to hit this in one step, but can't find how.  If I could
> get this in one step, it would simplify some book keeping, because
> this current method requires me to build up the name vector to match
> newdf, and sometimes I make mistakes.
> 
> After the data.frame already exists, I understand I can insert new
> variables using the variable names with syntax like
> 
> newdf[[plotx]] <- c(23, 23, 23, 23)
> 
> PJ
> 
> --
> Paul E. Johnson
> Professor, Political Science
> 1541 Lilac Lane, Room 504
> University of Kansas
> 
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

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


[Rd] Use of Matrix within packages in R-2.14.0

2011-11-11 Thread Bryan W. Lewis
Dear R-devel readers:

  I am really stuck trying resolving an issue with the use of the
Matrix in one of my packages, irlba, with R-2.14.0. When I use
crossprod with at least one sparse argument in the packaged code I
receive the error:

Error in crossprod(x, y) :
  requires numeric/complex matrix/vector arguments

However, when I run the code outside of the context of the package it
works fine.

I have constructed the following trivial dummy package to replicate
the problem, the contents of which follow:

DESCRIPTION file:
Package: dummy
Type: Package
Title: Dummy package for testing
Version: 0.0.0
Date: 2011-10-15
Author: B. W. Lewis 
Maintainer: B. W. Lewis 
Description: A bogus test package
Depends: Matrix
License: GPL

NAMESPACE file:
export("test")

R/test.R file:
`test` <- function(A)
{
  x = crossprod(A)
  return(0)
}


To replicate the problem, create a package called "dummy" from the
above three files, install it, and run from R-2.14.0:

library("dummy")
A = Matrix(0,10,10)
test(A)

I have tested this with Ubuntu 10.10 GNU/Linux (64-bit), and 32-bit
Windows versions of R-2.14.0.

Any help in resolving this issue is greatly appreciated!

Thanks,

Bryan

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


Re: [Rd] Use of Matrix within packages in R-2.14.0

2011-11-11 Thread Douglas Bates
On Nov 11, 2011 6:13 PM, "Bryan W. Lewis"  wrote:
">
> Dear R-devel readers:
>
>  I am really stuck trying resolving an issue with the use of the
> Matrix in one of my packages, irlba, with R-2.14.0. When I use
> crossprod with at least one sparse argument in the packaged code I
> receive the error:
>
> Error in crossprod(x, y) :
>  requires numeric/complex matrix/vector arguments
You must create a NAMESPACE file and import the crossprod function from the
Matrix package.

> However, when I run the code outside of the context of the package it
> works fine.
>
> I have constructed the following trivial dummy package to replicate
> the problem, the contents of which follow:
>
> DESCRIPTION file:
> Package: dummy
> Type: Package
> Title: Dummy package for testing
> Version: 0.0.0
> Date: 2011-10-15
> Author: B. W. Lewis 
> Maintainer: B. W. Lewis 
> Description: A bogus test package
> Depends: Matrix
> License: GPL
>
> NAMESPACE file:
> export("test")
>
> R/test.R file:
> `test` <- function(A)
> {
>  x = crossprod(A)
>  return(0)
> }
>
>
> To replicate the problem, create a package called "dummy" from the
> above three files, install it, and run from R-2.14.0:
>
> library("dummy")
> A = Matrix(0,10,10)
> test(A)
>
> I have tested this with Ubuntu 10.10 GNU/Linux (64-bit), and 32-bit
> Windows versions of R-2.14.0.
>
> Any help in resolving this issue is greatly appreciated!
>
> Thanks,
>
> Bryan
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

[[alternative HTML version deleted]]

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