Title: | Produce R Functions to Create HTML Forms Based on SQL Meta Data |
---|---|
Description: | Offers meta programming style tools to generate configurable R functions that produce HTML forms based on table input and SQL meta data. Also generates functions for collecting the parameters of those HTML forms after they are submitted. Useful for quickly generating HTML forms based on existing SQL tables. To use the resultant functions, the output files containing those functions must be read into the R environment (perhaps using base::source()). |
Authors: | Timothy Conwell |
Maintainer: | Timothy Conwell <[email protected]> |
License: | GPL (>= 2) |
Version: | 0.1.0 |
Built: | 2025-02-16 04:15:44 UTC |
Source: | https://github.com/cran/dbWebForms |
Based on the columns in a table, produces a R function that returns a data.table of HTTP parameters extracted from a list of HTPP params. You can create such a list easily using serverUtils::paramList.
createFetchParamsFunction( filepath, function_name, x, id_col, type_col, exclude = c() )
createFetchParamsFunction( filepath, function_name, x, id_col, type_col, exclude = c() )
filepath |
A string, the file path including the name and file type extension where the output will be written. |
function_name |
A string, the name of the function to be written to the file path. |
x |
A data.table, should have a column of input ids (used for input names as well), and a column of HTML input types. |
id_col |
A string, the column of x containing the HTML input ids. |
type_col |
A string, the column of x containing the HTML input types. |
exclude |
A character vector, ids included here will not be included as parameters for the resultant function. |
A data.table, the HTML input type and attributes likely associated with the SQL data type.
createFetchParamsFunction( filepath = paste0(tempdir(), "/", "testHTMLFormFunction.R"), function_name = "fetch_example_function", x = as.data.table( list( ids = c("user_id", "user_name", "first_name", "last_name", "state"), types = c("number", "text", "text", "text", "text") ) ), id_col = "ids", type_col = "types", exclude = c("user_id") )
createFetchParamsFunction( filepath = paste0(tempdir(), "/", "testHTMLFormFunction.R"), function_name = "fetch_example_function", x = as.data.table( list( ids = c("user_id", "user_name", "first_name", "last_name", "state"), types = c("number", "text", "text", "text", "text") ) ), id_col = "ids", type_col = "types", exclude = c("user_id") )
Based on the columns in a table, produces a R function with parameters for each column that produces a HTML form when called.
createHTMLFormFunction( filepath, function_name, form_method = "POST", add_csrf_param = TRUE, form_class = NULL, input_class = NULL, submit_class = NULL, submit_label = "Save", required_span_class = NULL, required_span_label = NULL, x, id_col, type_col, labels = c(), select = c(), exclude = c(), optional = c(), custom_input_types = list() )
createHTMLFormFunction( filepath, function_name, form_method = "POST", add_csrf_param = TRUE, form_class = NULL, input_class = NULL, submit_class = NULL, submit_label = "Save", required_span_class = NULL, required_span_label = NULL, x, id_col, type_col, labels = c(), select = c(), exclude = c(), optional = c(), custom_input_types = list() )
filepath |
A string, the file path including the name and file type extension where the output will be written. |
function_name |
A string, the name of the function to be written to the file path. |
form_method |
A string, the method attribute for the HTML form tag, likely "GET" or "POST". |
add_csrf_param |
TRUE/FALSE, if TRUE, adds a parameter for passing a value to a hidden input with a name of "crsf_token". |
form_class |
A string, the class attribute for the HTML form tag. |
input_class |
A string, the class attribute for the div tag wrapping the HTML inputs. |
submit_class |
A string, the class attribute for the HTML button tag used to submit the form. |
submit_label |
A string, the label attribute for the HTML button tag used to submit the form. |
required_span_class |
A string, the class attribute for the HTML span tag optionally added to required fields. |
required_span_label |
A string, the message for the HTML span tag optionally added to required fields. |
x |
A data.table, should have a column of input ids (used for input names as well), and a column of HTML input types. |
id_col |
A string, the column of x containing the HTML input ids. |
type_col |
A string, the column of x containing the HTML input types (usually created by calling dbTypeToHTMLInputType()) . |
labels |
A named character vector, names are the ids of the inputs and values are the labels to use. If a column is not specified here, the default label is the result of namesToLabels() called for each input id. |
select |
A character vector, ids included here will become select tags rather than input tags and a function parameter will be added to pass options. |
exclude |
A character vector, ids included here will not be included as parameters for the resultant function. |
optional |
A character vector, ids included here will not be marked as required inputs in the relevant HTML tags. |
custom_input_types |
A named list of character vectors, names are the column ids, character vectors must have an entry named type with a value for the HTML input type to be used and additional attributes can be included as subsequent named values in the character vector. |
A character vector, the HTML input type and attributes likely associated with the SQL data type.
createHTMLFormFunction( filepath = paste0(tempdir(), "/", "testHTMLFormFunction.R"), function_name = "example_function", form_method = "POST", add_csrf_param = TRUE, form_class ="pure-form pure-form-aligned", input_class = "pure-control-group", submit_class = "pure-button pure-button-primary", submit_label = "Save", required_span_class = "pure-form-message-inline", required_span_label = "Required field", x = as.data.table( list( ids = c("user_id", "user_name", "first_name", "last_name", "state"), types = c("number", "text", "text", "text", "text") ) ), id_col = "ids", type_col = "types", labels = c(user_name = "Account Name"), select = c("state"), exclude = c("user_id"), optional = c("first_name", "last_name"), custom_input_types = list(user_name = c(type = "email", minlength = 5)) )
createHTMLFormFunction( filepath = paste0(tempdir(), "/", "testHTMLFormFunction.R"), function_name = "example_function", form_method = "POST", add_csrf_param = TRUE, form_class ="pure-form pure-form-aligned", input_class = "pure-control-group", submit_class = "pure-button pure-button-primary", submit_label = "Save", required_span_class = "pure-form-message-inline", required_span_label = "Required field", x = as.data.table( list( ids = c("user_id", "user_name", "first_name", "last_name", "state"), types = c("number", "text", "text", "text", "text") ) ), id_col = "ids", type_col = "types", labels = c(user_name = "Account Name"), select = c("state"), exclude = c("user_id"), optional = c("first_name", "last_name"), custom_input_types = list(user_name = c(type = "email", minlength = 5)) )
Query INFORMATION_SCHEMA or equivalent SQL meta data to obtain column names and types for a table.
dbTableInfo( con = NULL, sql = c("MariaDB", "Microsoft SQL Server", "MySQL", "PostgreSQL", "SQLite"), table_catalog = NULL, table_schema = NULL, table_name )
dbTableInfo( con = NULL, sql = c("MariaDB", "Microsoft SQL Server", "MySQL", "PostgreSQL", "SQLite"), table_catalog = NULL, table_schema = NULL, table_name )
con |
A database connection that can be passed to DBI::dbSendQuery/DBI::dbGetQuery. |
sql |
A string, the type of SQL database con is connected to; must be one of c("MariaDB", "Microsoft SQL Server", "MySQL", "PostgreSQL", "SQLite"). |
table_catalog |
A string, the catalog (usually a database name) name of the SQL table to return column meta data for. Not used if sql = "SQLite". |
table_schema |
A string, the schema name of the SQL table to return column meta data for. |
table_name |
A string, the name of the SQL table to return column meta data for. |
A data.table, two columns, "column_name" has the names of the columns in the specified SQL table and "data_type" has the data types for each column. If con is NULL, returns the SQL string for querying the meta data but does not execute the statement.
dbTableInfo( con = NULL, sql = "PostgreSQL", table_catalog = "db1", table_schema = "public", table_name = "table1" )
dbTableInfo( con = NULL, sql = "PostgreSQL", table_catalog = "db1", table_schema = "public", table_name = "table1" )
Convert SQL data types to likely HTML input types
dbTypeToHTMLInputType(db_type)
dbTypeToHTMLInputType(db_type)
db_type |
A string, the SQL data type to convert to HTML input type. |
A character vector, the HTML input type and attributes likely associated with the SQL data type.
dbTypeToHTMLInputType("int")
dbTypeToHTMLInputType("int")
Convert strings to title case, splitting strings into separate words based on a separator.
namesToLabels(x, split = "_")
namesToLabels(x, split = "_")
x |
A string. |
split |
A string, used to split x into constituent words to be converted to title case. |
A string, converted to title case with split words separated with a space character.
namesToLabels("date_of_birth", split = "_")
namesToLabels("date_of_birth", split = "_")
Add single quotes to strings, useful for converting R strings into SQL formatted strings.
quoteText(x, char_only = TRUE)
quoteText(x, char_only = TRUE)
x |
A string. |
char_only |
TRUE/FALSE, if TRUE, adds quotes only if is.character(x) is TRUE. |
A string, with single quotes added to match postgreSQL string formatting.
quoteText("Sample quotes.")
quoteText("Sample quotes.")