Re: [R] Read code from character string

2010-06-20 Thread Johannes Huesing
Gabor Grothendieck ggrothendi...@gmail.com [Sat, Jun 19, 2010 at 11:19:35AM 
CEST]:
 On Sat, Jun 19, 2010 at 4:56 AM, Johannes Huesing johan...@huesing.name 
 wrote:
  A rule would be a data structure containing the ID of the rule, the
  rule in human readable language, an expression evaluating variables
  within the environment of the appropriate data frame (and resolving to
  a logical vector), possibly the data frame itself, and the query
  message (possibly as an sprintf expression). The data structure may be
  an S4 object or a list. In our current workflow, we manage the
  validation rules using a spreadsheet, and import them into a
  competitor's analysis software.
 
  I could pass
 
  function(df) with(df, (vsstresn  30 | vsstresn  130)  vstestcd == HR)
 
 Another possibility would be to use an sql statement or fragment so if
 you passed this sql fragment:
 
fragment - (vsstresn  30 or vsstresn  130) and vstestcd = 'HR'
 
  # then your program would run this code:
 
df - data.frame(vsstresn = seq(10, 200, 10), vstestcd = rep(c(HR, 
  RH), c(16, 4)))
library(sqldf)
sqldf(paste(select * from df where, fragment))
   vsstresn vstestcd
 1   10   HR
 2   20   HR
 3  140   HR
 4  150   HR
 5  160   HR

Many thanks! (ok, next time I will post a data example myself.)

Is this a corollary of fortune 106: If the answer is eval(parse()),
you should reconsider the language?

-- 
Johannes Hüsing   There is something fascinating about science. 
  One gets such wholesale returns of conjecture 
mailto:johan...@huesing.name  from such a trifling investment of fact.  
  
http://derwisch.wikidot.com (Mark Twain, Life on the Mississippi)

__
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] Read code from character string

2010-06-19 Thread Johannes Huesing
Greg Snow greg.s...@imail.org [Sat, Jun 19, 2010 at 12:44:48AM CEST]:
 There are a lot of other reasons to install the fortunes package that just 
 the one fortune, there is much wisdom, some wit, (and then there are mine) 
 throughout the package.
 

Sorry, but I am subscribing to r-help already. I could not possibly
handle more wisdom and wit than this.

 There could be other ways to accomplish your goals, if you let us know more 
 about what you are trying to do, we may be able to help you find a better way 
 (that does not mean that you cannot still use eval and parse, but you may 
 learn something, and/or avoid future pitfalls).

Ok, I want to demonstrate for educational purposes how to construct a
set of rules to validate a set of clinical data, and to make it issue
and export appropriate query messages. 

A rule would be a data structure containing the ID of the rule, the
rule in human readable language, an expression evaluating variables
within the environment of the appropriate data frame (and resolving to
a logical vector), possibly the data frame itself, and the query
message (possibly as an sprintf expression). The data structure may be
an S4 object or a list. In our current workflow, we manage the
validation rules using a spreadsheet, and import them into a
competitor's analysis software.

I could pass 

function(df) with(df, (vsstresn  30 | vsstresn  130)  vstestcd == HR) 

as an argument, but function(df) with(df, is sort of redundant, as I
expect it to be in every expression, plus it doesn't add much clarity
for the people writing and reading these conditions.

[...]
 Personally I have never regretted trying not to underestimate my own future
 stupidity.
-- Greg Snow (explaining why eval(parse(...)) is often suboptimal, 
 answering
   a question triggered by the infamous fortune(106))
   R-help (January 2007)

Don't get me started about my current stupidity.

-- 
Johannes Hüsing   There is something fascinating about science. 
  One gets such wholesale returns of conjecture 
mailto:johan...@huesing.name  from such a trifling investment of fact.  
  
http://derwisch.wikidot.com (Mark Twain, Life on the Mississippi)

__
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] Read code from character string

2010-06-19 Thread Gabor Grothendieck
On Sat, Jun 19, 2010 at 4:56 AM, Johannes Huesing johan...@huesing.name wrote:
 A rule would be a data structure containing the ID of the rule, the
 rule in human readable language, an expression evaluating variables
 within the environment of the appropriate data frame (and resolving to
 a logical vector), possibly the data frame itself, and the query
 message (possibly as an sprintf expression). The data structure may be
 an S4 object or a list. In our current workflow, we manage the
 validation rules using a spreadsheet, and import them into a
 competitor's analysis software.

 I could pass

 function(df) with(df, (vsstresn  30 | vsstresn  130)  vstestcd == HR)

Another possibility would be to use an sql statement or fragment so if
you passed this sql fragment:

   fragment - (vsstresn  30 or vsstresn  130) and vstestcd = 'HR'

 # then your program would run this code:

   df - data.frame(vsstresn = seq(10, 200, 10), vstestcd = rep(c(HR, RH), 
 c(16, 4)))
   library(sqldf)
   sqldf(paste(select * from df where, fragment))
  vsstresn vstestcd
1   10   HR
2   20   HR
3  140   HR
4  150   HR
5  160   HR

__
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] Read code from character string

2010-06-18 Thread Johannes Huesing
Peter Langfelder peter.langfel...@gmail.com [Thu, Jun 17, 2010 at 09:38:45PM 
CEST]:
 eval(parse(text=print(9**2)))

cheers, I overlooked the text option.

 
 On Thu, Jun 17, 2010 at 12:32 PM, Johannes Huesing
 johan...@huesing.name wrote:
  Dear expRts,
  I have a character string, say a - print(9**2). How do I execute
  the contents of the string, parsed as R code? Do I have to open a
  connection and use cat(a), and parse it at the other end?

-- 
Johannes Hüsing   There is something fascinating about science. 
  One gets such wholesale returns of conjecture 
mailto:johan...@huesing.name  from such a trifling investment of fact.  
  
http://derwisch.wikidot.com (Mark Twain, Life on the Mississippi)

__
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] Read code from character string

2010-06-18 Thread Greg Snow
You should also look at fortune(106) and think about possible other solutions 
to your overall objective.

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.s...@imail.org
801.408.8111


 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-
 project.org] On Behalf Of Johannes Huesing
 Sent: Friday, June 18, 2010 12:17 AM
 To: Peter Langfelder
 Cc: r-h...@stat.math.ethz.ch
 Subject: Re: [R] Read code from character string
 
 Peter Langfelder peter.langfel...@gmail.com [Thu, Jun 17, 2010 at
 09:38:45PM CEST]:
  eval(parse(text=print(9**2)))
 
 cheers, I overlooked the text option.
 
 
  On Thu, Jun 17, 2010 at 12:32 PM, Johannes Huesing
  johan...@huesing.name wrote:
   Dear expRts,
   I have a character string, say a - print(9**2). How do I execute
   the contents of the string, parsed as R code? Do I have to open a
   connection and use cat(a), and parse it at the other end?
 
 --
 Johannes Hüsing   There is something fascinating about
 science.
   One gets such wholesale returns of
 conjecture
 mailto:johan...@huesing.name  from such a trifling investment of fact.
 http://derwisch.wikidot.com (Mark Twain, Life on the
 Mississippi)
 
 __
 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] Read code from character string

2010-06-18 Thread Johannes Huesing
Greg Snow greg.s...@imail.org [Fri, Jun 18, 2010 at 04:57:03PM CEST]:
 You should also look at fortune(106) and think about possible other solutions 
 to your overall objective.

I am not installing fortune solely for this purpose but I understand
that anything which smells like macro expansion is frowned upon in R
circles.

Yet there are situations where i want to treat code as data and
data as code. As far as I gather this works only with closures in R,
not with general S-expressions, so sometimes I have to resort to
character strings.

-- 
Johannes Hüsing   There is something fascinating about science. 
  One gets such wholesale returns of conjecture 
mailto:johan...@huesing.name  from such a trifling investment of fact.  
  
http://derwisch.wikidot.com (Mark Twain, Life on the Mississippi)

__
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] Read code from character string

2010-06-18 Thread Greg Snow
There are a lot of other reasons to install the fortunes package that just the 
one fortune, there is much wisdom, some wit, (and then there are mine) 
throughout the package.

There could be other ways to accomplish your goals, if you let us know more 
about what you are trying to do, we may be able to help you find a better way 
(that does not mean that you cannot still use eval and parse, but you may learn 
something, and/or avoid future pitfalls).

 fortune(106)

If the answer is parse() you should usually rethink the question.
   -- Thomas Lumley
  R-help (February 2005)

 fortune('106')

Personally I have never regretted trying not to underestimate my own future
stupidity.
   -- Greg Snow (explaining why eval(parse(...)) is often suboptimal, answering
  a question triggered by the infamous fortune(106))
  R-help (January 2007)


-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.s...@imail.org
801.408.8111


 -Original Message-
 From: Johannes Huesing [mailto:johan...@huesing.name]
 Sent: Friday, June 18, 2010 2:57 PM
 To: Greg Snow
 Cc: Peter Langfelder; r-h...@stat.math.ethz.ch
 Subject: Re: [R] Read code from character string
 
 Greg Snow greg.s...@imail.org [Fri, Jun 18, 2010 at 04:57:03PM CEST]:
  You should also look at fortune(106) and think about possible other
 solutions to your overall objective.
 
 I am not installing fortune solely for this purpose but I understand
 that anything which smells like macro expansion is frowned upon in R
 circles.
 
 Yet there are situations where i want to treat code as data and
 data as code. As far as I gather this works only with closures in R,
 not with general S-expressions, so sometimes I have to resort to
 character strings.
 
 --
 Johannes Hüsing   There is something fascinating about
 science.
   One gets such wholesale returns of
 conjecture
 mailto:johan...@huesing.name  from such a trifling investment of fact.
 http://derwisch.wikidot.com (Mark Twain, Life on the
 Mississippi)
__
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] Read code from character string

2010-06-17 Thread Johannes Huesing
Dear expRts,
I have a character string, say a - print(9**2). How do I execute
the contents of the string, parsed as R code? Do I have to open a 
connection and use cat(a), and parse it at the other end?

Cheers


Johannes
-- 
Johannes Hüsing   There is something fascinating about science. 
  One gets such wholesale returns of conjecture 
mailto:johan...@huesing.name  from such a trifling investment of fact.  
  
http://derwisch.wikidot.com (Mark Twain, Life on the Mississippi)

__
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] Read code from character string

2010-06-17 Thread Peter Langfelder
eval(parse(text=print(9**2)))

On Thu, Jun 17, 2010 at 12:32 PM, Johannes Huesing
johan...@huesing.name wrote:
 Dear expRts,
 I have a character string, say a - print(9**2). How do I execute
 the contents of the string, parsed as R code? Do I have to open a
 connection and use cat(a), and parse it at the other end?

__
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.