Description Usage Arguments Value Author(s) See Also Examples
View source: R/createSQLFunc.R
These functions allow us to register R functions or C routines with an SQLite database so that the functions can be used directly within SQL queries as scalar and aggregate functions. This allows us to augment the functionality within SQL directly with R functions, or by C code in different DLLs/DSOs (compiled libraries). We can register scalar functions that return a value for each record on which they are called (e.g., log), and also aggregate functions that process multiple records and finally return a single result (e.g. average).
1 2 3 4 | createSQLFunction(db, func, nargs = -1L, name = substitute(func),
isRoutine = is.character(func), userData = NULL)
createSQLAggregateFunction(db, step, final, nargs = -1L, name = substitute(step),
isRoutine = FALSE, userData = NULL)
|
db |
the database in which the function is being registered |
func |
the scalar function or routine being registered.
This can be specified as an R function, or as the address of a C
routine, or as a description of a C routine obtained by
|
step |
part of an aggregate function. This is the per-record R function that is called for each record. |
final |
this is the final step in an aggregate function. This is
the R function that is called to compute the final result having
processed all the records in the |
name |
the name by which this function can be called in SQL |
nargs |
the number of arguments that should be specified in each call to this function. If this is -1, the function can be called with different numbers of arguments, i.e., it is variadic. This is more flexible and general, but has a slightly higher overhead. For aggregate functions, nargs is the number of inputs to the record/step function. The final function is called with no inputs. |
isRoutine |
a logical value, with |
userData |
data that is passed to the C routines each time they
are called. This is typically an |
These functions are used for their side effects of registering the functions to be used in SQL queries.
Duncan Temple Lang
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | library(RSQLite)
db = dbConnect(SQLite(), ":memory:")
ptr = getNativeSymbolInfo("myfloorFunc")$address
createSQLFunction(db, ptr, 1L, "myfloor")
dbGetQuery(db, "SELECT myfloor(2.3);")
# We can refer to a C routine by name and leave getNativeSymbolInfo()
# resolve it (without knowing the DLL/DSO). This is potentially "dangerous"!
createSQLFunction(db, "lastNChars", 2L)
dbGetQuery(db, "SELECT lastNChars('USA:22 September 1986', 4);")
createSQLFunction(db, nchar, 1L, "numChars")
dbGetQuery(db, "SELECT numChars('bob');")
dbGetQuery(db, "SELECT numChars('Robert');")
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.