View source: R/dbTableFromView.R
| dbTableFromView | R Documentation |
The dbTableFromView() function creates a table in a SQLite database
from a view already present in the same database.
dbTableFromView(
view_name,
dbcon,
table_name,
drop_table = FALSE,
build_pk = FALSE,
pk_fields = NULL
)
view_name |
character, name of the view. |
dbcon |
database connection, as created by the dbConnect function. |
table_name |
character, the name of the table. |
drop_table |
logical, if |
build_pk |
logical, if |
pk_fields |
character vector, the list of the fields' names that
define the |
integer, the number of records in table_name after writing data
from the input view.
# Create a temporary database and demonstrate view to table conversion
library(RSQLite.toolkit)
# Set up database connection
dbcon <- dbConnect(RSQLite::SQLite(), file.path(tempdir(), "example.sqlite"))
# Load some sample data first
data_path <- system.file("extdata", package = "RSQLite.toolkit")
dbTableFromDSV(
input_file = file.path(data_path, "abalone.csv"),
dbcon = dbcon,
table_name = "ABALONE",
drop_table = TRUE,
header = TRUE,
sep = ",",
dec = "."
)
# Create a view with aggregated data
dbExecute(dbcon, "DROP VIEW IF EXISTS VW_ABALONE_SUMMARY;")
dbExecute(dbcon,
"CREATE VIEW VW_ABALONE_SUMMARY AS
SELECT SEX,
COUNT(*) as COUNT,
AVG(LENGTH) as AVG_LENGTH,
AVG(WHOLE) as AVG_WEIGHT
FROM ABALONE
GROUP BY SEX"
)
# Convert the view to a permanent table
dbTableFromView(
view_name = "VW_ABALONE_SUMMARY",
dbcon = dbcon,
table_name = "ABALONE_STATS",
drop_table = TRUE
)
# Check the result
dbListTables(dbcon)
dbGetQuery(dbcon, "SELECT * FROM ABALONE_STATS")
# Clean up
dbDisconnect(dbcon)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.