L
L
lPolar2015-04-03 20:02:37
Python
lPolar, 2015-04-03 20:02:37

String formatting in R?

Hello.
Faced with the task of generating sql code in R.
Let's say I need to make several substitutions in a text string.
In python, this example would look like this:

sql_params = {'col1_type':'cat','col1_date':'date'}
string_to_format = """
select * from
(select * from some_table where col1_type='{'col1_type'}' and something=2) x1
union
(select * from some_table where col1_type='{'col1_type'}' and col1_date=to_date('{col1_date}','YYYYMMDD')) x2
"""
full_query = string_to_format.format(**sql_params)

What does R have for such operations? I tried sprintf, but if my parameter is often repeated in the request, I have to write it everywhere.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya Kashnitsky, 2015-11-18
@ikashnitsky

I'm not sure, but maybe there are simpler solutions to the problem itself?
there is a package sqldf
and there is a function sqlfrom the packagedplyr

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question