R4CouchDB


https://github.com/wactbprot/R4CouchDB

 

Description

A R convenience layer for CouchDB. R4CouchDB unites RCurl and RJSONIO and implements the CouchDB API.

 

Installation

R4CouchDB is part of the cran cosmos so the package can be installed by:

R> install.packages("R4CouchDB")

 

Example

The example presumes a running CouchDB on http://localhost:5984. The plan is to store some data to a CouchDB document get it back and make a plot (some eye candy) out of it.


At first load the R4CouchDB package as usual:

R> library("R4CouchDB")

Then we generate the connection or interface list (here assigned to the variable cdb) by:

R> cdb <- cdbIni()

cdb makes some default assumptions such as:

R> cdb$serverName
[1] "localhost"

or

R> cdb$port
[1] "5984"

Now straight forward: we make a database http://localhost:5984/r-example, load some fancy volcano data, and write the data (plus some demo info) to a new database document:

R> cdb$newDBName <- "r-example"
R> cdb           <- cdbMakeDB(cdb)
R> data(volcano)
R> rdoc <- list(date = date(), 
                data = volcano, 
                dim  = dim(volcano))
R> cdb$dataList  <- rdoc
R> res           <- cdbAddDoc(cdb)$res
R> res
$ok
[1] TRUE
$id
[1] "26659bf9e39fb9ad6278325963643246"
$rev
[1] "1-44e4052a2ef00a8574cad2210e1cfc8e"

We now make the plot of the volcano with the data from the database. Getting back the data is done with:

R> cdb$id           <- res$id
R> cdoc             <- cdbGetDoc(cdb)$res

regenerate the volcano matrix from the result of the database query (cdoc) by:

R> mat <- matrix(data = t(unlist( cdoc$data )), 
                 nrow = cdoc$dim[[2]],  
                 ncol = cdoc$dim[[1]])

and here we are:

R> png("r-example.png")
R> persp(mat, 
         theta = 120,
         phi   = 30, 
         col   = "green3",
         shade = 0.75 )
R> dev.off()

Finally we can store the diagram as png in the database document as an attachment by:

R> cdb$fileName <- "r-example.png"
R> cdbAddAttachment(cdb)$res
$ok
[1] TRUE
$id
[1] "26659bf9e39fb9ad6278325963643246"
$rev
[1] "2-02eeabde33aaafc8c6d270203fd0dba7"

sofa

 

https://github.com/ropensci/sofa

 

Description

An easy interface to CouchDB from R. Targeted at CouchDB v2+, but much of the package should work with older versions.

 

Installation

 

R> install.packages("sofa")

 

 Load the package
R> library('sofa')
Make a client
R> client <- Cushion$new()
R> client
<sofa - cushion> 
  transport: http
  host: 127.0.0.1
  port: 5984
  path: 
  type: 
  user: 
  pwd:
Ping the server
R> ping(client)
$couchdb
[1] "Welcome"

$version
[1] "2.0.0"

$vendor
$vendor$name
[1] "The Apache Software Foundation"
Create a new database
R> db_create(client, dbname = 'cats')
$ok
[1] TRUE
List your databases
R> db_list(client)
[1] "cats"
Create a document
R> doc1 <- '{"name":"leo", "type":"cat", "color": "red"}'
R> doc_create(client, doc1, dbname = "cats", docid = "doc1")
$ok
[1] TRUE

$id
[1] "doc1"

$rev
[1] "1-a48c98c945bcc05d482bc6f938c89882"
Query the database

Return all cats that are red

R> db_query(client, dbname = "cats", selector = list(color = "red"))$docs
[[1]]
[[1]]$`_id`
[1] "e6bb43092edaf8fd987434b8a30cfbdf"

[[1]]$`_rev`
[1] "1-08aef850a23f5ff95869c9cf5d9604dc"

[[1]]$name
[1] "leo"

[[1]]$color
[1] "red"

[[1]]$type
[1] "cat"
Bulk create documents

Create documents in bulk from a data.frame, list, or JSON as a character string.

R> db_create(client, dbname = "bulktest")
R> db_bulk_create(client, dbname = "bulktest", mtcars)