# NOT RUN {
(x <- Cushion$new())
if ("drinksdb" %in% db_list(x)) {
invisible(db_delete(x, dbname="drinksdb"))
}
db_create(x, dbname='drinksdb')
# create an attachment on an existing document
## create a document first
doc <- '{"name":"stuff", "drink":"soda"}'
doc_create(x, dbname="drinksdb", doc=doc, docid="asoda")
## create a csv attachment
row.names(mtcars) <- NULL
file <- tempfile(fileext = ".csv")
write.csv(mtcars, file = file, row.names = FALSE)
doc_attach_create(x, dbname="drinksdb", docid="asoda",
attachment=file, attname="mtcarstable.csv")
## create a binary (png) attachment
file <- tempfile(fileext = ".png")
png(file)
plot(1:10)
dev.off()
doc_attach_create(x, dbname="drinksdb", docid="asoda",
attachment=file, attname="img.png")
## create a binary (pdf) attachment
file <- tempfile(fileext = ".pdf")
pdf(file)
plot(1:10)
dev.off()
doc_attach_create(x, dbname="drinksdb", docid="asoda",
attachment=file, attname="plot.pdf")
# get info for an attachment (HEAD request)
doc_attach_info(x, "drinksdb", docid="asoda", attname="mtcarstable.csv")
doc_attach_info(x, "drinksdb", docid="asoda", attname="img.png")
doc_attach_info(x, "drinksdb", docid="asoda", attname="plot.pdf")
# get an attachment (GET request)
res <- doc_attach_get(x, "drinksdb", docid="asoda",
attname="mtcarstable.csv", as = "text")
read.csv(text = res)
doc_attach_get(x, "drinksdb", docid="asoda", attname="img.png")
doc_attach_get(x, "drinksdb", docid="asoda", attname="plot.pdf")
# delete an attachment
doc_attach_delete(x, "drinksdb", docid="asoda", attname="mtcarstable.csv")
doc_attach_delete(x, "drinksdb", docid="asoda", attname="img.png")
doc_attach_delete(x, "drinksdb", docid="asoda", attname="plot.pdf")
# }
Run the code above in your browser using DataLab