'R Package passing function through to new function

I'm creating a new package as a learning exercise. I've selected a few functions that serve the purpose I need and want to bundle them together into a single, new, package that I can then apply gWidgets to to make them GUI driven.

Finding documentation on how to pass existing functions has been pretty sparse and I am a novice at this. Any assistance would be appreciated.

I've added the necessary imports in my Description using usethis:use_package() updated the NAMESPACE (using Roxygen) and created the .R files using a Stackoverflow as a framework. a sample .R looks like this:

#'ODBC list drivers
#'
#'@export
odbcListDrivers <- function() {
odbc::odbcListDrivers()
}

This Works. But when it comes to the point I need to pass information with a more advanced function:

#'    
#' u/export    
#'    
DBconnect <- function() {    
DBI::dbConnect()    
}

I get an unused argument error when I try to run the test code.

con <- DBconnect(RMySQL::MySQL(),    
+ dbname = "sakila",    
+ host = "192.168.50.71",    
+ port = 49153,    
+ user = "root",    
+ password = "Camplovers01")

Error in DBconnect(RMySQL::MySQL(), dbname = "sakila", host = "192.168.50.71", :    
unused arguments (dbname = "sakila", host = "192.168.50.71", port = 49153, user = "root", password = "Camplovers01")

The question then is when I pass a function like above how can I make sure I pass the correct arguments? Function I am trying to pass



Solution 1:[1]

Ok, asked and then answered by myself. The answer is in the error, unused arguments. I need to place the arguments from the source function in the function I create. These will pass through to the original function and return the correct response. The odbcListDrivers worked as it had no arguments to pass or expect. Example Old .R file

#'    
#' u/export    
#'    
DBconnect <- function() {    
DBI::dbConnect()    
}

This of course fails as was the reason for my question. New .R file

#'
#' @export
#'
DBconnect <- function(dsn = NULL,
                      ...,
                      timezone = "UTC",
                      timezone_out = "UTC",
                      encoding = "",
                      bigint = c("integer64", "integer", "numeric", "character"),
                      timeout = Inf,
                      driver = NULL,
                      server = NULL,
                      database = NULL,
                      uid = NULL,
                      pwd = NULL,
                      dbms.name = NULL,
                      .connection_string = NULL) {
  DBI::dbConnect()
}

Finding the arguments of the function was a simple matter of reviewing the .R file in the GitHub repo for the Package in question.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 General Douglas MacArthur