2

I have a rather long and convoluted query that I use as a starting point for doing some analysis. I have replicated that one from scratch using dbplyr so I can use dplyr verbs without getting the database into memory. How can I achieve this without having to replicate the starting query using dbplyr?

2
  • Are you asking for a way to automatically translate SQL into dplyr (so dbplyr can then translate it back into SQL)? An example would assist. Commented Nov 18, 2020 at 20:08
  • This post gives the SQL to do that: stackoverflow.com/questions/75524437/… Commented Feb 24, 2023 at 14:16

1 Answer 1

2

Take a look at this answer. It explicitly sets the SQL query for a dbplyr table. You can use the same idea:

# read file containing existing query
start_query = your_favorite_way_to_read_file_here(file_name)

# create connection
con = DBI::dbConnect(...)

# initial table
my_table = tbl(con, sql(start_query))

To verify that it has worked as intended try show_query(my_table) or head(my_table).

Note that dbplyr does not translate dplyr into highly optimized queries. So if your initial query is complex / slow to run, adding further manipulation via dbplyr could perform poorly. In this case it might be better to write your complex query to another table.

Sign up to request clarification or add additional context in comments.

1 Comment

"Adding further manipulation via dbplyr could perform poorly." Indeed! One way to " write your complex query to another table" would be simply to append %>% compute() to the end of the line creating my_table.

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.