View source: R/dbTableFromDSV.R
| dbTableFromDSV | R Documentation |
The dbTableFromDSV() function reads the data from a DSV file
and copies it to a table in a SQLite database. If table does
not exist, it will create it.
The dbTableFromDSV() function reads the data from a DSV file
and copies it to a table in a SQLite database. If table does
not exist, it will create it.
dbTableFromDSV(
input_file,
dbcon,
table_name,
header = TRUE,
sep = ",",
dec = ".",
grp = "",
id_quote_method = "DB_NAMES",
col_names = NULL,
col_types = NULL,
col_import = NULL,
drop_table = FALSE,
auto_pk = FALSE,
build_pk = FALSE,
pk_fields = NULL,
constant_values = NULL,
chunk_size = 0,
...
)
input_file |
character, the file name (including path) to be read. |
dbcon |
database connection, as created by the dbConnect function. |
table_name |
character, the name of the table. |
header |
logical, if |
sep |
character, field delimiter (e.g., "," for CSV, "\t" for TSV) in the input file. Defaults to ",". |
dec |
character, decimal separator (e.g., "." or "," depending on locale) in the input file. Defaults to ".". |
grp |
character, character used for digit grouping. It defaults
to |
id_quote_method |
character, used to specify how to build the SQLite
columns' names using the fields' identifiers read from the input file.
For details see the description of the |
col_names |
character vector, names of the columuns in the input file.
Used to override the field names derived from the input file (using the
quote method selected by |
col_types |
character vector of classes to be assumed for the columns
of the input file. Must be of the same length of the number of columns
in the input file. If not null, it will override the data types guessed
from the input file.
If |
col_import |
can be either:
|
drop_table |
logical, if |
auto_pk |
logical, if |
build_pk |
logical, if |
pk_fields |
character vector, the list of the fields' names that
define the |
constant_values |
a one row data frame whose columns will be added to the table in the database. The additional table columns will be named as the data frame columns, and the corresponding values will be associeted to each record imported from the input file. It is useful to keep track of additional information (e.g., the input file name, additional context data not available in the data set, ...) when loading the content of multiple input files in the same table. |
chunk_size |
integer, the number of lines in each "chunk" (i.e. block
of lines from the input file). Setting its value to a positive integer
number, will process the input file by blocks of |
... |
additional arguments passed to |
integer, the number of records in table_name after reading data
from input_file.
# Create a temporary database and load CSV data
library(RSQLite.toolkit)
# Set up database connection
dbcon <- dbConnect(RSQLite::SQLite(), file.path(tempdir(), "example.sqlite"))
# Get path to example data
data_path <- system.file("extdata", package = "RSQLite.toolkit")
# Load abalone CSV data with automatic primary key
dbTableFromDSV(
input_file = file.path(data_path, "abalone.csv"),
dbcon = dbcon,
table_name = "ABALONE",
drop_table = TRUE,
auto_pk = TRUE,
header = TRUE,
sep = ",",
dec = "."
)
# Check the imported data
dbListFields(dbcon, "ABALONE")
head(dbGetQuery(dbcon, "SELECT * FROM ABALONE"))
# Load data with specific column selection
dbTableFromDSV(
input_file = file.path(data_path, "abalone.csv"),
dbcon = dbcon,
table_name = "ABALONE_SUBSET",
drop_table = TRUE,
header = TRUE,
sep = ",",
dec = ".",
col_import = c("Sex", "Length", "Diam", "Whole")
)
head(dbGetQuery(dbcon, "SELECT * FROM ABALONE_SUBSET"))
# Check available tables
dbListTables(dbcon)
# Clean up
dbDisconnect(dbcon)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.