This question has been asked many times, but I have not been able to find a satisfactory solution. I use Linux and R. The dylr
package displays some warning messages. When executing this script I get the following warning message:
source("/home/code-server/Workspace/test/new.R", encoding = "UTF-8")
Attaching package: ‘dplyr’
The following objects are masked from ‘package:stats’:
filter, lag
The following objects are masked from ‘package:base’:
intersect, setdiff, setequal, union
warning messages from top-level task callback 'vsc.workspace'
Warning message:
Database is garbage-collected, use dbDisconnect(con, shutdown=TRUE) or duckdb::duckdb_shutdown(drv) to avoid this.
The source code:
# Add libraries
library(dplyr)
library(DBI)
library(duckdb)
path <- "data/"
setwd(path)
# write to disk as "Example", other defaults to in memory
con <- DBI::dbConnect(duckdb::duckdb(), "test-2022")
duckdb::duckdb_read_csv(
conn = con, name = "Example_csv", files = "1a.csv",
header = TRUE, delim = ","
)
Here some lines of Code to show the table. Unfortunately, these lines of code do not work.
con <- dbConnect(duckdb::duckdb(), dbdir = "test-2022", read_only = FALSE)
dbListTables(con)
dbDisconnect(con, shutdown = TRUE)
dbWriteTable(con, "test", test)
res = dbGetQuery(con, "SELECT * FROM "test-2022")
print(res)
Regarding your issues:
this is happening because the file you source in
source("/home/code-server/Workspace/test/new.R", encoding = "UTF-8")
does not disconnect fromcon
. As the warning indicates, you adddbDisconnect(con, shutdown = TRUE)
at the end of that file.Because you don't do so, the connection is later garbage collected and produces this warning. You can reproduce this by running the code below in a new session:
you can find more info on the garbage collectionn here (https://adv-r.hadley.nz/names-values.html#gc)
There are at least to errors here:
You close the connection
con
(usingdbDisconnect
) but then you attempt to write the tabletest
to the same connection (usingdbWriteTable
). This will not work because the connection has been disconnected.As already mentioned above, the query in
dbGetQuery
is not properly constructed since you hare using nested double quotes. Moreoved, you are trying to import data from a table with the name of the database file when you set it up indbConnect
above instead of the importing data fromtest
At least, that is what I think you are doing, since you dind't share that many details on the error message.
This is what I think the code should look like:
I hope it was helpful :)