In the following code, I show how to create a new table by sending a sql query to the database. What if I mutate the data in a pipeline? How do I write it into a new table if it is bigger than RAM such that collecting the data is not possible?
library("tidyverse")
library("duckdb")
#'
#' Create a new table with data in database
#'
con <- dbConnect(duckdb(), dbdir = "test.duckdb")
con |> dbWriteTable("mtcars", mtcars)
#'
#' Create a copy of the data in the databse
#'
con |> dbExecute("create table mtcars2 as select * from mtcars")
mtcars2 <- con |> tbl("mtcars2")
#'
#' Modify a column and write a new table
#'
mtcars |>
mutate(mpg2 = mpg * 2)
# this step is missing
I'm not quite sure if it's about writing a new table or overwriting / altering existing one, but when writing result to a new table with compute() you can set a name and create a persistent table with temporary = FALSE: