View source: R/dbTableFromXlsx.R
| dbTableFromXlsx | R Documentation |
The dbTableFromXlsx() function creates a table in a SQLite database from a
range of an Excel worksheet.
The dbTableFromXlsx() function reads the data from a range of
an Excel worksheet. If table does not exist, it will
create it.
dbTableFromXlsx(
input_file,
dbcon,
table_name,
sheet_name,
first_row,
cols_range,
header = TRUE,
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,
...
)
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. |
sheet_name |
character, the name of the worksheet containing the data table. |
first_row |
integer, the row number where the data table starts. If present, it is the row number of the header row, otherwise it is the row number of the first row of data. |
cols_range |
integer, a numeric vector specifying which columns in the worksheet to be read. |
header |
logical, if |
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. Defults to |
... |
additional arguments passed to |
integer, the number of records in table_name after reading data
from input_file.
# Create a temporary database and load Excel 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")
# Check if Excel file exists (may not be available in all installations)
xlsx_file <- file.path(data_path, "stock_portfolio.xlsx")
fschema <- file_schema_xlsx(xlsx_file, sheet_name="all period",
first_row=2, cols_range="A:S", header=TRUE,
id_quote_method="DB_NAMES", max_lines=10)
fschema[, c("col_names", "src_names")]
# Load Excel data from specific sheet and range
dbTableFromXlsx(
input_file = xlsx_file,
dbcon = dbcon,
table_name = "PORTFOLIO_PERF",
sheet_name = "all period",
first_row = 2,
cols_range = "A:S",
drop_table = TRUE,
col_import = c("ID", "Large_B_P", "Large_ROE", "Large_S_P",
"Annual_Return_7", "Excess_Return_8", "Systematic_Risk_9")
)
# Check the imported data
dbListFields(dbcon, "PORTFOLIO_PERF")
head(dbGetQuery(dbcon, "SELECT * FROM PORTFOLIO_PERF"))
# Clean up
dbDisconnect(dbcon)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.