[R-sig-DB] [R] Argument to database
Nilza BARROS
n||z@b@rro@ @end|ng |rom gm@||@com
Thu Oct 28 15:27:50 CEST 2010
OK, Thank you! You're rigth. It is better to sent my question to r-help.
But I will check optparse and getopt.
Bye and Best Regards,
Nilza
On Thu, Oct 28, 2010 at 1:13 PM, Sean Davis <sdavis2 using mail.nih.gov> wrote:
>
>
> On Thu, Oct 28, 2010 at 9:06 AM, Nilza BARROS <nilzabarros using gmail.com>wrote:
>
>> Dear R users,
>>
>> Thanks Sean for your idea. But since I am getting used to using R in my
>> all tasks I 'd like to know if the Rusers can show me a better way to use
>> arguments in Rscript or if the way I had applied (below here and in my
>> previous email) the arguments is correct.
>> What I used it the same way I am used to writing in my shell scripts.
>>
>>
> Ah. This would better have been written to R-help, as it has nothing to do
> with databases. However, it is a pretty simple answer. Look at the
> optparse and getopt packages on CRAN.
>
> Sean
>
>
>> I really hope someone can help me.
>> Thanks in advance
>>
>> *Below here the way I check the number of arguments. I am used to doing
>> that in my shell scripts.*
>>
>> ==================
>> #! /usr/bin/Rscript --vanilla
>> args <- commandArgs(TRUE)
>>
>> if (length(args)!=4 )
>> {
>> print("********************************************")
>> print("Enter the arguments Model (20,21,...) - Trim(01,02,03,04) - Year
>> (AAAA)")
>> print("********************************************")
>> stop("Incorrect arguments length")
>> }
>> npt.freq <- args[1]
>> npt.trim <-args[2]
>> npt.ano <- args[3]
>>
>>
>>
>> Nilza Barros
>>
>> On Tue, Oct 26, 2010 at 8:11 PM, Sean Davis <sdavis2 using mail.nih.gov> wrote:
>>
>>>
>>>
>>> On Tue, Oct 26, 2010 at 3:23 PM, Nilza BARROS <nilzabarros using gmail.com>wrote:
>>>
>>>> Dear Rusers,
>>>>
>>>> I am using Rscript and I'd like to use arguments as an input.
>>>> I need it because I will use these arguments to consult Mysql Databse
>>>> using
>>>> (SELECT) .
>>>> I need different select to each Model Type and date.
>>>>
>>>> ./GrafDens.R [ModelType trim date ]
>>>>
>>>> The script I am using it is working but I'd like to know if there is
>>>> something more efficient.
>>>>
>>>>
>>>> Below here what I am using:
>>>> ==================
>>>> #! /usr/bin/Rscript --vanilla
>>>> args <- commandArgs(TRUE)
>>>>
>>>> if (length(args)!=4 )
>>>> {
>>>> print("********************************************")
>>>> print("Enter the arguments Model (20,21,...) - Trim(01,02,03,04) -
>>>> Year
>>>> (AAAA)")
>>>> print("********************************************")
>>>> stop("Incorrect arguments length")
>>>> }
>>>> npt.freq <- args[1]
>>>> npt.trim <-args[2]
>>>> npt.ano <- args[3]
>>>>
>>>>
>>>> MES1.00 <- paste(npt.ano,npt.mes,"__","00",sep="")
>>>> MES2.00 <- paste(npt.ano,npt.mes,"__","00",sep="")
>>>> MES3.00 <- paste(npt.ano,npt.mes,"__","00",sep="")
>>>>
>>>>
>>>> ===How I use the strings above to SELECT my data:
>>>>
>>>>
>>> You could consider using sprintf() to generate your SQL string, as well.
>>>
>>>
>>>> drv=dbDriver("MySQL")
>>>> con=dbConnect(drv,dbname='xxx',user='xx',password='xx')
>>>> dados <- dbGetQuery(con,paste("SELECT
>>>> OBS_StationNo as station_no,
>>>> OBS_date as obsdate,
>>>> FCT_fdate as fdate,
>>>> FCT_mtype as fct_mtype,
>>>> FCT_mrun as fct_mrun,
>>>> FROM VWFct_Obs
>>>> WHERE
>>>> FCT_mtype=",npt.mtype,
>>>> " AND OBS_StationNo <> 'NULL'
>>>> AND (FCT_fdate LIKE ", paste("'",MES1.00,"'",sep=""),
>>>> " OR FCT_fdate LIKE ", paste("'",MES1.12,"'",sep=""),
>>>> " OR FCT_fdate LIKE ", paste("'",MES2.00,"'",sep=""),
>>>> " OR FCT_fdate LIKE ", paste("'",MES2.12,"'",sep=""),
>>>> " OR FCT_fdate LIKE ", paste("'",MES3.00,"'",sep=""),
>>>> "OR FCT_fdate LIKE " ,paste("'",MES3.12,"'",sep="")," )" ,sep=""))
>>>> #
>>>> ====================
>>>> --
>>>> Abra�o,
>>>> Nilza Barros
>>>>
>>>> [[alternative HTML version deleted]]
>>>>
>>>>
>>>
>>
>>
>> --
>> Abra�o,
>> Nilza Barros
>>
>
>
--
Abra�o,
Nilza Barros
[[alternative HTML version deleted]]
More information about the R-sig-DB
mailing list