I'm creating an R package where the user will first have to connect to a database using the DBI and odbc packages. Subsequent functions will then need to query the database database and then conduct some sort of analysis. So the workflow for the user is (1) connect to the database; (2) do something that depends on the connection. e.g.
connect_to_db()
analyze_using_db_connection()
I'm not sure of the best setup to make things easy on the user and myself, the developer. As I see it, here are my options:
(1) for every function in the package have a con
argument where the user passes the connection (this seems less than ideal for anyone using the package)
(2) Use some sort of class system like R6 and store the connection as a data field. All subsequent functions (methods) that require the connection are member of the class (this seems like an overkill use of OOP)
(3) Have an argument for the connection in each function that needs the connection. If the connection is missing, have the function look for a connection con
in the parent environment and check that con
is the right type with each function call (I don't love this situation either; it seems tenuous to me)
Or is there another preferred method when writing a package.
Any advice would be greatly appreciated