0

I have an expression max(51). I want to replace the parameter in the expression i.e. 51 with the values in the database with respect to this ID. So my replaced expression will be as below:

max(dbGetQuery(con,"SELECT VALUE FROM STUD WHERE ID=51"))

where con is MySQL connection object. I have tried the following to extract the parameter from expression

PARAMETER <- gsub(".*\\((.*)\\)$", "\\1", "MAX(51)");

Now My final expression will become something like this:

max(dbGetQuery(con,"SELECT VALUE FROM STUD WHERE ID=PARAMETER"));

So how this replacement can be done in R? This is just a single operand expression. Can it be possible for more complex expressions like

max(51)+min(52)-53

Thanks..

4
  • post your attempts.. Commented Apr 12, 2015 at 9:07
  • Do you already have an SQL query that you need to search and replace, or do you construct the query inside R? Commented Apr 12, 2015 at 9:41
  • I have the SQL query ready. Commented Apr 12, 2015 at 9:56
  • 1
    See: stackoverflow.com/questions/17435086/… Commented Apr 12, 2015 at 10:54

1 Answer 1

0

I have achieved this in the following way:

operand <- "max(51)";
PARAMETER <- gsub(".*\\((.*)\\)$", "\\1", operand);
QUERY=gsub("PARAMETER",PARAMETER,"SELECT VALUE FROM STUD WHERE ID='PARAMETER'");
Final_SubExpression <- gsub(PARAMETER,"dbGetQuery(con,QUERY)",operand);
Result <- eval(parse(text=Final_SubExpression));
Result;

OR

operand <- "max(51)";
PARAMETER <- gsub(".*\\((.*)\\)$", "\\1", operand);
QUERY=sprintf("SELECT VALUE FROM STUD WHERE ID = '%s'", PARAMETER);
Final_SubExpression <- gsub(PARAMETER,"dbGetQuery(con,QUERY)",operand);
Result <- eval(parse(text=Final_SubExpression));
Result;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.