Put some cushions on the sofa

June 21, 2013

(This article was first published on Recology - R, and kindly contributed to R-bloggers)

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')

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

[1] "Welcome"

[1] "1.2.1"
[1] "Welcome"

[1] "f1cb5d2e881bcb529d2eb2d04f548683"

[1] "1.3.0"

[1] "1.3.0r1"

[1] "Iris Couch"
[1] "Welcome"

[1] "1.0.2"

[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
sofa_createdb(dbname = "hello_world", "iriscouch")  # iriscouch
sofa_createdb(dbname = "hello_world", "cloudant")  # cloudant

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
[1] TRUE

[1] "da2b0d1eb457dc764a6283fa59001606"

[1] "1-5406480672da172726810767e7d0ead3"
sofa_writedoc("iriscouch", dbname = "helloworld", doc = doc)  # iriscouch
[1] TRUE

[1] "0c0858b75a81c464a74119ca2400135d"

[1] "1-5406480672da172726810767e7d0ead3"
sofa_writedoc("cloudant", dbname = "helloworld", doc = doc)  # cloudant
[1] TRUE

[1] "b77808eae8ae8d79ae78a373bf5b02d1"

[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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)