Site icon R-bloggers

Put some cushions on the sofa

[This article was first published on Recology - R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I posted earlier this week about sofa (here), introducing a package I started recently that interacts with CouchDB from R. There’s been a fair amount of response at least in terms of page views, so I’ll take that as a sign to keep going.

One thing that would be nice while you are CouchDB-ing is to interact with local and remote databases. I have incorporated the ability to interact with remote CouchDB databases in many of the functions, not all though. The remote data stores supported right now are Cloudant and Iriscouch.

Hadley Wickham suggested that a package called sofa should have something called cushion. And so it must be. It’s just a small function, called cushion, which puts a cushion on the sofa, or in reality, sets up your authentication for remote data stores. cushion just writes your username and password to your options list and then the functions look for your authentication details via getOption. Of course these auth details aren’t stored permanently – when you restart R you have to write them again to options. You can store them permanently in your .Rprofile file if you like, usally at ~/.Rprofile by putting in entry like options(cloudant.pwd = “mycoolpassword”).


Load sofa

# install.packages('devtools'); library(devtools); install_github('sofa', 'schamberlain')
library(sofa)

Put a cushion on the sofa – that is, save your auth details

cushion(iriscouch_name = "yourusername", iriscouch_pwd = "yourpwd", 
    cloudant_name = "yourusername", cloudant_pwd = "yourpwd")

Ping each server

sofa_ping()

$couchdb
[1] "Welcome"

$version
[1] "1.2.1"

sofa_ping("iriscouch")

$couchdb
[1] "Welcome"

$uuid
[1] "f1cb5d2e881bcb529d2eb2d04f548683"

$version
[1] "1.3.0"

$vendor
$vendor$version
[1] "1.3.0r1"

$vendor$name
[1] "Iris Couch"

sofa_ping("cloudant")

$couchdb
[1] "Welcome"

$version
[1] "1.0.2"

$cloudant_build
[1] "1323"

Now we’ll do similar tasks on a local and two remote databases (cloudant and iriscouch)

Create a database

sofa_createdb(dbname = "hello_world")  # local

  ok 
TRUE 

sofa_createdb(dbname = "hello_world", "iriscouch")  # iriscouch

  ok 
TRUE 

sofa_createdb(dbname = "hello_world", "cloudant")  # cloudant

  ok 
TRUE 

Listing your databases is a simple task

List your databases

sofa_listdbs()  # local

 [1] "_replicator"                "_users"                    
 [3] "alm_couchdb"                "alm_db"                    
 [5] "cheese"                     "dudedb"                    
 [7] "example"                    "foobar"                    
 [9] "foodb"                      "hello_world"               
[11] "helloworld"                 "rplos_db"                  
[13] "shit"                       "shitty"                    
[15] "shitty2"                    "sofadb"                    
[17] "test_suite_db"              "test_suite_db/with_slashes"
[19] "test_suite_reports"         "testr2couch"               
[21] "twitter_db"                

sofa_listdbs("iriscouch")  # iriscouch

[1] "_replicator" "_users"      "foobar"      "hello_world" "helloworld" 
[6] "mustache"    "stuff"       "thing"      

sofa_listdbs("cloudant")  # cloudant

[1] "dudedb"         "foobar"         "hello_world"    "helloworld"    
[5] "mustache"       "thingsandstuff"

Write a document to a database

doc <- "{\"name\":\"dude\",\"icecream\":\"rocky road\"}"
sofa_writedoc(dbname = "helloworld", doc = doc)  # local

$ok
[1] TRUE

$id
[1] "da2b0d1eb457dc764a6283fa59001606"

$rev
[1] "1-5406480672da172726810767e7d0ead3"

sofa_writedoc("iriscouch", dbname = "helloworld", doc = doc)  # iriscouch

$ok
[1] TRUE

$id
[1] "0c0858b75a81c464a74119ca2400135d"

$rev
[1] "1-5406480672da172726810767e7d0ead3"

sofa_writedoc("cloudant", dbname = "helloworld", doc = doc)  # cloudant

$ok
[1] TRUE

$id
[1] "b77808eae8ae8d79ae78a373bf5b02d1"

$rev
[1] "1-5406480672da172726810767e7d0ead3"

There’s lots more you can do of course…

Thoughts? Feelings? Criticism?

To leave a comment for the author, please follow the link and comment on their blog: Recology - R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.