-
Notifications
You must be signed in to change notification settings - Fork 4
/
view_csv.R
58 lines (51 loc) · 1.26 KB
/
view_csv.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#' Create a database connection from a CSV file. Internal only function.
#' @noRd
#'
#' @param csv_file_path A string. The csv file path.
#' @param table_name A string. The name of the table in the duckdb database.
#'
#' @importFrom duckdb duckdb
#' @importFrom DBI dbConnect
#' @importFrom DBI dbWriteTable
#' @importFrom readr read_csv
#'
#' @return A DBI database connection object
#'
create_csv_con <- function(csv_file_path, table_name = "csv_file"){
# Create duckdb instance
drv <- duckdb::duckdb()
con <- DBI::dbConnect(drv)
# Read csv file
csv_data <- readr::read_csv(
csv_file_path,
show_col_types = FALSE
)
# Write to db
DBI::dbWriteTable(
con,
table_name,
csv_data,
overwrite = TRUE,
temporary = FALSE,
row.names = FALSE
)
return (con)
}
#' View CSV
#' @description Upload a csv file to a in-memory duckdb database and write queries
#' against it.
#'
#' @param csv_file_path A string. The csv file path.
#' @param table_name A string. The name of the table in the duckdb database.
#'
#' @return A shiny app instance.
#' @export
#'
view_csv <- function(csv_file_path, table_name = "csv_file"){
con <- create_csv_con(
csv_file_path,
table_name = "csv_file"
)
# View file
octopus::view_database(con)
}