Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Update to most recent sofa version (0.2.0)

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:

Code Block
languageerl
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:

Code Block
R> library("R4CouchDB")

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

Code Block
R> cdb <- cdbIni()

cdb makes some default assumptions such as:

Code Block
R> cdb$serverName
[1] "localhost"

or

Code Block
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:

Code Block
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:

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

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

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

and here we are:

Code Block
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:

Code Block
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. sofa uses httr and  plyr.

Installation

Targeted at CouchDB v2+, but much of the package should work with older versions.

 

Installation

  sofa can be installed from github using devtools:

Code Block
R> install.packages("devtools")
R> devtools::install_github("ropensci/sofa")

 

Example

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

$vendor
$vendor$version$vendor$name
[1] "1.5.0-1"
$vendor$name
[1] "HomebrewThe Apache Software Foundation"
Create a
CouchDB
new database
:
Code Block
R> db_create(client, dbname = 'sofadbcats')
$ok
[1] TRUE
List your databases
:
Code Block
R> db_list(client)
[1] "_replicator" "_users"      "cachecall"   "mran"        "mydb"        "sofadb" 
Write and get documents:
cats"
Create a document
Code Block
R> doc1 <- '{"name":"sofa"leo", "type":"cat", "cakecolor": "cheesecakered"}'
R> doc_create(client, doc1, dbname = "sofadbcats", docid = "a_cakedoc1")
$ok
[1] TRUE

$id
[1] "a_cakedoc1"

$rev
[1] "1-6917fa3cbed14ea90de280d9663c5d4c"

R> doc_get(dbname="sofadb", docid="a_cake")
a48c98c945bcc05d482bc6f938c89882"
Query the database

Return all cats that are red

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

[[1]]$`_rev`
[1] "1-6917fa3cbed14ea90de280d9663c5d4c08aef850a23f5ff95869c9cf5d9604dc"
$name
[[1] "sofa"
$cake]]$name
[1] "cheesecake"

List changes:

Code Block
R> changes(dbname="sofadb")
$results
$resultsleo"

[[1]]$color
$results[[1]]$seq
[1] 1
$results "red"

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

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

Code Block
R> db_create(client, dbname = "bulktest")
R> db_bulk_create(client, dbname = "bulktest", mtcars)
_cake"
$results[[1]]$changes
$results[[1]]$changes[[1]]
$results[[1]]$changes[[1]]$rev
[1] "1-6917fa3cbed14ea90de280d9663c5d4c"
$last_seq
[1] 1