pg_create_table | R Documentation |
Here we simply read a fraction of the file to detect column names and data types, then create the SQL create statement and finally add some modifiers. By default this function does not execute the query.
pg_create_table(
con,
file_path,
table,
sep = "auto",
nrows = 10000,
header = "auto",
na.strings = c("", "NA", "NULL"),
colClasses = NULL,
col.names,
encoding = "UTF-8",
...,
unlogged = FALSE,
drop_table = FALSE,
cascade = FALSE,
if_not_exists = FALSE,
execute = FALSE
)
con |
A database connection. |
file_path |
path to a text file (csv, tab-delimited, etc.) |
table |
The table name, passed on to
|
sep |
The separator between columns. Defaults to the character in the set |
nrows |
The maximum number of rows to read. Unlike |
header |
Does the first data line contain column names? Defaults according to whether every non-empty field on the first data line is type character. If so, or TRUE is supplied, any empty column names are given a default name. |
na.strings |
A character vector of strings which are to be interpreted as |
colClasses |
As in |
col.names |
A vector of optional names for the variables (columns). The default is to use the header column if present or detected, or if not "V" followed by the column number. This is applied after |
encoding |
default is |
... |
other arguments passed to data.table::fread |
unlogged |
logical, whether to create an UNLOGGED table |
drop_table |
logical, whether to drop the table before creating it |
cascade |
logical, whether to add CASCADE to the DROP statement |
if_not_exists |
logical, to add IF NOT EXISTS to the query |
execute |
logical, whether to execute the query using |
We use data.table::fread
to guess the data types in the text files
fread
detects all controls automatically (e.g. header, sep), but you
can always pass specific values to override the detection. This is handy,
for example, to override the type detected for some variables
(e.g. a numeric value that you would rather keep as a character).
colClasses = list( character = c("municipality_code", "state_code"), double = c("value"), integer = c("age") )
invisibly returns a character vector (length 1) with the sql statement
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.