3

this is my first ever post and i am quite new to all of this, so i hope i am doing this right. my problem is the following: i wrote some R code to get some ID's from a Database, which are then used to brew templates. the template uses Knitr R to then build some querys around the ID's to get the data then used for the actual report.

the Script goes like this:

...
create.report() <- function(ID){

reportname <- "MyReport.Rnw"     
brew('template.rnw',reportname)

knit(reportname)

 ....
}

the template.rnw with knitr gets the ID passed by brew :

\section*{This is a Report for <%=ID%>}

<<loaddata, echo =FALSE>>=
query <- paste0("SELECT ... WHERE ID= ",<%=ID%>) # this gets us the Data for the report

data <- dbGetQuery(con,query) # Data from db is in Data now.

@
...

And this just worked fine for me. But now i want to pass more parameters from the Script to the template then just simple Input like an ID. For example i would like to pass a timespan like from="2010-01-01" ,to="2012-01-01"

to maby use this like

...
create.report() <- function(ID,fromdate,todate){

reportname <- "MyReport.Rnw"     
brew('template.rnw',reportname)

knit(reportname)

 ....
}

.rnw template

<<loaddata, echo =FALSE>>=
from <- paste0("AND date(...) =>'",<%=fromdate%,"'")
to <- paste0("AND date(...) =< '", <%=todate%>,"'")
query <- paste0("SELECT ... WHERE ID= ",<%=ID%>,from,to) # this gets us the Data 

data <- dbGetQuery(con,query) # Data from db is in Data now.

@
...    

but the Problem with that is, that he wont leave the fromdate and todate to be a string like "2010-01-01" and allways make it 2008.

is there any way to tell the Code Chunks not to do anything with the brew input before setting it, or maby pass the parameters from outside to the chunk so they dont have to be set in the chunk ?

I hope you can help me with this, or show me a proper way around it.

2 Answers 2

1

The problem can be solved by using the shQuote("string") -function. This will leave the date, in this case, fromdate and todate as-is. Example:

from <- paste0("AND date(...) =>'",<%=shQuote(fromdate)%,"'")

You might have to adjust the type depending on the OS you are working on.

0
1

It has been quite a long time since i posted that, but i remember it was to get fromdate as a string. Without the shQuote it allways got interpreted 2010-01-01 = 2010 - 2 = 2008 before the concatination with the rest of the strings paste0("AND date(...) =>, ...) happened. Hope that helps.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .