[R] Custom Command to Generate SQL
Gabor Grothendieck
ggrothendieck at gmail.com
Tue Jun 20 04:34:02 CEST 2006
Hi, Here is an example of paste:
> paste("and", 1:3, collapse = " ")
[1] "and 1 and 2 and 3"
in which and was prepended to each of the vector entries and then
everything was collapsed together. In our
situation we don't actually want the first 'and' so we substitute for
it, using sub,
the first portion of the SQL statement.
Suggest you turn on debugging:
debug(retrieve) # undebug(retrieve) will turn it back off
and run the function and at each try printing out various partial
expressions to see what they do.
Regards.
On 6/19/06, Alex Restrepo <alex_restrepo at hotmail.com> wrote:
> Hi Gabor:
>
> Thanks for the great example. I am an R newbie, so please forgive my
> question, but
> could you describe what the sub() function is doing in your example? Why is
> there an "and" in the first argument to the sub() function?
>
>
> Many Thanks:
>
> Alex
>
>
> >From: "Gabor Grothendieck" <ggrothendieck at gmail.com>
> >To: "Alex Restrepo" <alex_restrepo at hotmail.com>
> >CC: r-help at stat.math.ethz.ch
> >Subject: Re: [R] Custom Command to Generate SQL
> >Date: Mon, 19 Jun 2006 01:21:53 -0400
> >
> >This generates the sql statement so just pass that to your database:
> >
> >retrieve <- function(...) {
> > args <- list(...)
> > sub("and", "select * from people where",
> > paste(rbind("and", names(args), "=", dQuote(args)), collapse = " "))
> >}
> >
> ># test
> >retrieve(firstname = "JOHN", middlename = "WILLIANS", lastname = "FORD")
> >
> >On 6/19/06, Alex Restrepo <alex_restrepo at hotmail.com> wrote:
> >>Hi:
> >>
> >>I would like to create a custom command in R which generates SQL, which is
> >>then processed via RODBC.
> >>
> >>For example, the user would type:
> >>
> >> retrieve firstName('JOHN') middlleName('WILLIAMS')
> >>lastName('FORD')
> >>
> >>This would generate the following SQL which would then be processed by
> >>RODBC:
> >>
> >>Select *
> >> from people
> >> where firstName = 'JOHN' and
> >> middleName = 'WILLIAMS' and
> >> lastName = 'FORD'
> >>
> >>Does anyone have a recommendation? Any ideas would be greatly
> >>appreciated.
> >>
> >>Alex
> >>
> >>______________________________________________
> >>R-help at stat.math.ethz.ch mailing list
> >>https://stat.ethz.ch/mailman/listinfo/r-help
> >>PLEASE do read the posting guide!
> >>http://www.R-project.org/posting-guide.html
> >>
>
>
>
More information about the R-help
mailing list