Learn R Programming

⚠️There's a newer version (0.4.0) of this package.Take me there.

sofa

An easy interface to CouchDB from R

Note: Check out R4couchdb, another R package to interact with CouchDB.

CouchDB versions

sofa is built targeting CouchDB v2 or greater.

CouchDB Info

Connect to CouchDB

This may be starting it on your terminal/shell

couchdb

Or opening the CouchDB app on your machine, or running it in Docker. Whatever it is, start it up.

You can interact with your CouchDB databases as well in your browser. Navigate to http://localhost:5984/_utils

Install sofa

From CRAN

install.packages("sofa")

Development version from GitHub

devtools::install_github("ropensci/sofa")
library('sofa')

Cushions

Cushions? What? Since it's couch we gotta use cushions somehow. cushions are a connection class containing all connection info to a CouchDB instance. See ?Cushion for help.

As an example, connecting to a Cloudant couch:

z <- Cushion$new(
  host = "stuff.cloudant.com",
  transport = 'https',
  port = NULL,
  user = 'foobar',
  pwd = 'things'
)

Break down of parameters:

  • host: the base url, without the transport (http/https)
  • path: context path that is appended to the end of the url
  • transport: http or https
  • port: The port to connect to. Default: 5984. For Cloudant, have to set to NULL
  • user: User name for the service.
  • pwd: Password for the service, if any.
  • headers: headers to pass in all requests

If you call Cushion$new() with no arguments you get a cushion set up for local use on your machine, with all defaults used.

x <- Cushion$new()

Ping the server

x$ping()
#> $couchdb
#> [1] "Welcome"
#> 
#> $version
#> [1] "2.1.1"
#> 
#> $features
#> $features[[1]]
#> [1] "scheduler"
#> 
#> 
#> $vendor
#> $vendor$name
#> [1] "The Apache Software Foundation"

Nice, it's working.

Create a new database, and list available databases

#> $ok
#> [1] TRUE
db_create(x, dbname = 'sofadb')
#> $ok
#> [1] TRUE

see if its there now

db_list(x)
#> [1] "cats"       "flights"    "sofadb"     "testing123"

Create documents

Write a document WITH a name (uses PUT)

doc1 <- '{"name":"sofa","beer":"IPA"}'
doc_create(x, doc1, dbname = "sofadb", docid = "a_beer")
#> $ok
#> [1] TRUE
#> 
#> $id
#> [1] "a_beer"
#> 
#> $rev
#> [1] "1-a48c98c945bcc05d482bc6f938c89882"

Write a json document WITHOUT a name (uses POST)

doc2 <- '{"name":"sofa","icecream":"rocky road"}'
doc_create(x, doc2, dbname = "sofadb")
#> $ok
#> [1] TRUE
#> 
#> $id
#> [1] "901e4bf214fb50db456d3ef8ec0516c9"
#> 
#> $rev
#> [1] "1-fd0da7fcb8d3afbfc5757d065c92362c"

More docs

See the vignettes for more documentation.

Meta

Copy Link

Version

Install

install.packages('sofa')

Monthly Downloads

537

Version

0.3.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Scott Chamberlain

Last Published

January 3rd, 2018

Functions in sofa (0.3.0)

Cushion

sofa connection client
uuids

Get uuids.
sofa-package

R client for CouchDB.
attach_get

Get an attachment.
attachments

Work with attachments
db_explain

Explain API
db_index

Create and get database indexes
design_search

Search design documents
doc_create

Create documents to a database.
db_changes

List changes to a database.
db_compact

Request compaction of the specified database
db_revisions

Get document revisions.
design

Work with design documents
doc_delete

Delete a document in a database.
doc_get

Get a document from a database.
databases

Work with databases in your CouchDB's.
db_alldocs

List all docs in a given database.
db_query

Query a database.
db_replicate

Upload (replicate) a local database to a remote database server, e.g., Cloudant, Iriscouch
documents

Work with documents in your CouchDB's.
membership

membership
db_bulk_create

Create documents via the bulk API
session

session
db_bulk_update

Create documents via the bulk API
restart

Restart your Couchdb instance
db_create

Create a database.
db_delete

Delete a database.
doc_head

Get header info for a document
doc_update

Update a document.
active_tasks

active tasks
db_info

List database info.
db_list

List all databases.
parse_df

Parse data.frame to json or list by row or column
ping

Ping a CouchDB server