Put some cushions on the sofa
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
<span class="c1"># install.packages('devtools'); library(devtools); install_github('sofa', 'schamberlain')</span>
library<span class="p">(</span>sofa<span class="p">)</span>
Put a cushion on the sofa – that is, save your auth details
cushion<span class="p">(</span>iriscouch_name <span class="o">=</span> <span class="s">"yourusername"</span><span class="p">,</span> iriscouch_pwd <span class="o">=</span> <span class="s">"yourpwd"</span><span class="p">,</span>
cloudant_name <span class="o">=</span> <span class="s">"yourusername"</span><span class="p">,</span> cloudant_pwd <span class="o">=</span> <span class="s">"yourpwd"</span><span class="p">)</span>
Ping each server
sofa_ping<span class="p">()</span>
$couchdb
[1] "Welcome"
$version
[1] "1.2.1"
sofa_ping<span class="p">(</span><span class="s">"iriscouch"</span><span class="p">)</span>
$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<span class="p">(</span><span class="s">"cloudant"</span><span class="p">)</span>
$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<span class="p">(</span>dbname <span class="o">=</span> <span class="s">"hello_world"</span><span class="p">)</span> <span class="c1"># local</span>
ok
TRUE
sofa_createdb<span class="p">(</span>dbname <span class="o">=</span> <span class="s">"hello_world"</span><span class="p">,</span> <span class="s">"iriscouch"</span><span class="p">)</span> <span class="c1"># iriscouch</span>
ok
TRUE
sofa_createdb<span class="p">(</span>dbname <span class="o">=</span> <span class="s">"hello_world"</span><span class="p">,</span> <span class="s">"cloudant"</span><span class="p">)</span> <span class="c1"># cloudant</span>
ok
TRUE
Listing your databases is a simple task
List your databases
sofa_listdbs<span class="p">()</span> <span class="c1"># local</span>
[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<span class="p">(</span><span class="s">"iriscouch"</span><span class="p">)</span> <span class="c1"># iriscouch</span>
[1] "_replicator" "_users" "foobar" "hello_world" "helloworld"
[6] "mustache" "stuff" "thing"
sofa_listdbs<span class="p">(</span><span class="s">"cloudant"</span><span class="p">)</span> <span class="c1"># cloudant</span>
[1] "dudedb" "foobar" "hello_world" "helloworld"
[5] "mustache" "thingsandstuff"
Write a document to a database
doc <span class="o"><-</span> <span class="s">"{\"name\":\"dude\",\"icecream\":\"rocky road\"}"</span>
sofa_writedoc<span class="p">(</span>dbname <span class="o">=</span> <span class="s">"helloworld"</span><span class="p">,</span> doc <span class="o">=</span> doc<span class="p">)</span> <span class="c1"># local</span>
$ok
[1] TRUE
$id
[1] "da2b0d1eb457dc764a6283fa59001606"
$rev
[1] "1-5406480672da172726810767e7d0ead3"
sofa_writedoc<span class="p">(</span><span class="s">"iriscouch"</span><span class="p">,</span> dbname <span class="o">=</span> <span class="s">"helloworld"</span><span class="p">,</span> doc <span class="o">=</span> doc<span class="p">)</span> <span class="c1"># iriscouch</span>
$ok
[1] TRUE
$id
[1] "0c0858b75a81c464a74119ca2400135d"
$rev
[1] "1-5406480672da172726810767e7d0ead3"
sofa_writedoc<span class="p">(</span><span class="s">"cloudant"</span><span class="p">,</span> dbname <span class="o">=</span> <span class="s">"helloworld"</span><span class="p">,</span> doc <span class="o">=</span> doc<span class="p">)</span> <span class="c1"># cloudant</span>
$ok
[1] TRUE
$id
[1] "b77808eae8ae8d79ae78a373bf5b02d1"
$rev
[1] "1-5406480672da172726810767e7d0ead3"
There’s lots more you can do of course…
Thoughts? Feelings? Criticism?
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.