Collection of functions 4 R and CouchDB interaction
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

         __ _  _     ___                 _        ___  ___ 
        /__\ || |   / __\___  _   _  ___| |__    /   \/ __\
       / \// || |_ / /  / _ \| | | |/ __| '_ \  / /\ /__\//
      / _  \__   _/ /__| (_) | |_| | (__| | | |/ /_// \/  \
      \/ \_/  |_| \____/\___/ \__,_|\___|_| |_/___,'\_____/

A Collection of R functions for CouchDB access

The R4CouchDB package provides a collection of functions for basic database and document management operations such as add and delete.

Note: Check out sofa, from @recology_ another R package to interact with CouchDB.

Note: I added a example of R4CouchDB usage to the (new) CouchDB wiki


The R4CouchDB package is reviewed in the book XML and Web Technologies for Data Sciences with R by Deborah Nolan and Duncan Temple Lang published in 2014 on Springer.


  • Thanks to @parisni for the contribution of cdbAddDocS()


Every cdbFunction() gets and returns a list() containing the complete connection set up and the data. With the command

R> cdb <- cdbIni()

such a list() can be generated. It contains some default values e.g. the

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


R> cdb$digits
R> [1] 7

With a database running on http://localhost:5984 the R-command:

R> cdbListDB( cdbIni() )

lists the databases.

Getting started

R> install.packages("R4CouchDB")

Note: Be aware of the fact that beside R-base-devel must be installed RCurl needs libcurl-devel. The package name may differ e.g.:

  • openSuse people say $> zypper in libcurl-devel
  • ubuntu useRs run $> sudo apt-get install libcurl4-openssl-dev

  • an further way for those who have devtools installed is

R> library(devtools)
R> install_github('R4CouchDB', 'wactbprot', subdir = 'R4CouchDB')

(see e.g.

  • open R shell and load library with:
R> library(R4CouchDB)
  • generate a connection object (a simple list()) with:
R> foo <- cdbIni()
  • play around with foo
R> foo$queryParam <- "count=10"
R> cdbGetUuidS(foo)$res
  • see test session in the example folder

  • become angry and write a issue


Since version 0.6 a test session is available. The functions below the folder tests in the files test/test*.r assume a writable CouchDB instance running on http://localhost:5984. Run the tests e.g. with:

$> cd R4CouchDB/tests 
$> R -f run.r



If you get somenthing like this:

$> untar2(tarfile, files, list, exdir) : unsupported entry type ‘x’

on installation you can try

$> export R_INSTALL_TAR=tar

and than

$> R CMD INSTALL R4CouchDB_latest_


In the function cdbIni() I added with 0.1.2 the lines:

       cdb$toJSON <- function(lst){
         jsn <- toJSON(lst,
                collapse = "",
                digits = digits)
         jsn <- gsub("\\r","\\\\r",jsn)

The point is: one can have a


in the database but one can not send it back this way. A \r is here replaced by \\r resulting in \r in the database. The gsub() function behaves like this:

R> gsub("\\r","\\\\r","\r")
R> [1] "\\r"
R> gsub("\\r","\\\\r","\\r")
R> [1] "\\r"
R> gsub("\\r","\\\\r","\\\r")
R> [1] "\\\\r"
R> gsub("\\r","\\\\r","\\\\r")
R> [1] "\\\\r"

I'm not happy with this but I have no better solution for the moment.


Numbers are converted to JSON with 7 digits by default.

R> cdbIni()$digits
R> [1] 7

Adjust this to your needs by:

R> cdb        <- cdbIni()
R> cdb$digits <- 13


R>cdb        <- cdbIni(digits=13)