RMongo: Accessing MongoDB in R

I recently created RMongo, a database access layer to MongoDB in R as an R package.

To install RMongo:

install.packages(“RMongo”)

If that does not work, try downloading it from http://cran.r-project.org/web/packages/RMongo/index.html and run:

install.packages("~/Downloads/RMongo_XX.XX.XX.tar.gz", repos=NULL, type="source")

I tried to mimic the RMySQL commands in RMongo. Below are some example commands.

library(RMongo)

#ask for help
?RMongo

#connect to a database
mongo results names(results)
[1] “X_id” “name” “nutrient_definition_id” “description”
> results
X_id name nutrient_definition_id
1 4cd0f8e31e627d4e6600000e Adjusted Protein 257
2 4cd0f9061e627d4e6600001a Sodium 307

> results results
X_id name nutrient_definition_id
1 4cd0f9061e627d4e6600001a Sodium 307

> dbDisconnect(mongo)

 

RMongo is very alpha at this point. I built it as a quick way to prototype algorithms with data from mongoDB in R. Most of RMongo uses the mongo-java-driver to perform json-formatted queries. The R code in the package uses rJava to communicate with the mongo-java-driver.

Please report any bugs or necessary improvements. Or better yet, send in pull requests via the RMongo github project page!

32 thoughts on “RMongo: Accessing MongoDB in R

  1. Sami

    There seems to be some bug or odd behaviour when trying to use mongodb with RMongo version 0.0.18.Connection goes normally:mongo.con<-mongoDbConnect(“tickets”,host=”localhost”,port=27017)But any attempt to use the connection results in Java IO exception:dbShowCollections(mongo.con)Feb 4, 2011 6:17:10 PM com.mongodb.DBTCPConnector$MyPort errorSEVERE: MyPort.error calledjava.io.IOException: couldn't connect to [machine.name/127.0.1.1:27017] bc:java.net.ConnectException: Connection refused at com.mongodb.DBPort._open(DBPort.java:172) at com.mongodb.DBPort.go(DBPort.java:79) at com.mongodb.DBPort.call(DBPort.java:55) at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:201) at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:256) at com.mongodb.DB.getCollectionNames(DB.java:200) at com.quid.RMongo.dbShowCollections(RMongo.scala:24)Any help is appreciated.

    Reply
  2. Carlos Rodriguez

    0.18 installs but with the latest ver. 0.20 I get the errors below…. what am I doing wrong?

    ———————————————–
    c:>R CMD INSTALL RMongo_0.0.20.tar.gz
    R CMD INSTALL RMongo_0.0.20.tar.gz
    * installing to library ‘C:/Program Files/R/R-2.13.0/library’
    * installing *source* package ‘RMongo’ …
    ** libs
    C:/PROGRA~1/R/R-213~1.0/etc/i386/Makeconf:180: warning: overriding commands for target `.m.o’
    C:/PROGRA~1/R/R-213~1.0/etc/i386/Makeconf:173: warning: ignoring old commands for target `.m.o’
    ERROR: compilation failed for package ‘RMongo’
    * removing ‘C:/Program Files/R/R-2.13.0/library/RMongo’

    c:\>R CMD INSTALL RMongo_0.0.18.tar.gz
    R CMD INSTALL RMongo_0.0.18.tar.gz
    * installing to library ‘C:/Program Files/R/R-2.13.0/library’
    * installing *source* package ‘RMongo’ …
    ** R
    ** inst
    ** preparing package for lazy loading
    ** help
    *** installing help indices
    ** building package indices …
    ** testing if installed package can be loaded

    * DONE (RMongo)

    ——————————————————————————————

    Reply
  3. Pingback: #Rstats for Business Intelligence « DECISION STATS

  4. Many

    Hi,

    I want to try your RMongo (Just learning :-) but when I try:
    R CMD install RMongo*.tar.gz
    I get this error:
    install: missing destination file operand after `RMongo_0.0.21.tar.gz’
    Try `install –help’ for more information.

    I’m not sure what destination file should I point RMongo to

    Hope you can help me with this.

    Thanks in advance

    Reply
  5. Pingback: Main | Pearltrees

  6. Tammy

    I want to install RMongo on my Mac OS X 10.7.3 machine. Is the install package only for Windows operating systems?

    Reply
  7. Shannon

    Hi Tom-
    I am very new to all of this. I am trying to install RMongo running ubuntu 12.04 on 64-bit OS. I have in my downloads folder RMongo_0.0.21.tar.gz. I also have in the folder /home//R/x86_-pc-linux-gnu-library/2.14 both RMongo_0.0.21.tar.gz and RMongo, which was extracted from the tar file. I also have in the folder /usr/lib/R/site-library the following files rJava, RMongo, rmongodb, and RUnit.

    R CMD install RMongo_0.0.21.tar.gz
    install: missing destination file operand after `RMongo_0.0.21.tar.gz’
    Try `install –help’ for more information.
    So, I try:
    R CMD install RMongo_0.0.21.tar.gz /usr/lib/R/site-library
    install: cannot stat `RMongo_0.0.21.tar.gz’: No such file or directory

    When I try install.packages(“RMongo”), the result is:
    bash: syntax error near unexpected token `$’\342\200\234RMongo\342\200\235”

    I don’t know what to do now. Any assistance you can provide is much appreciated. Thanks in advance.

    Reply
    1. tommychheng Post author

      It looks like you need to run R CMD install RMongo_0.0.21.tar.gz in the same directory as you downloaded it. Can you try that and let me know if that works?

      Reply
    2. Shannon

      Thank you for your suggestion. I’ve tried installing from the Downloads directory where I now have RMongo_0.0.22.tar.gz. When I try using the install.packages command you suggest above, I get “bash: syntax error near unexpected token `Downloads/RMongo_0.0.21.tar.gz,’”.

      I have also tried:
      apt-get install r-base-core r-base
      And then in the Downloads directory I ran:
      R CMD intall RMongo_0.0.22.tar.gz
      I then get a prompt requesting a destination directory. Running the same command with a destination directory, nothing happens other than another prompt appearing. However, when I check the destination directory I see the file RMongo_0.0.21.tar.gz but it’s not installed. I tried installing from that directory, but then I get a request for another destination directory…

      Do you have any suggestions of what else I could try to get the installation to work?

      Reply
  8. sa

    if one uses basic authentication how can I switch from the admin db to mydb after i get authenticated.

    > mongo authenticated authenticated
    [1] TRUE

    now i want to connect to mydb

    Reply
    1. tommychheng Post author

      Hi,
      Does the below snippet help:

      mongo <- mongoDbConnect("test")
      username = ""
      password = ""
      authenticated <- dbAuthenticate(mongo, username, password)
      output <- dbInsertDocument(mongo, "test_data", ’{"foo": "bar"}’)
      output <- dbGetQuery(mongo, ’test_data’, ’{"foo": "bar"}’)

      Reply
  9. tamara

    > install.packages(‘rmongodb’)
    — Please select a CRAN mirror for use in this session —
    Loading Tcl/Tk interface … done
    trying URL ‘http://cran.cnr.Berkeley.edu/src/contrib/rmongodb_1.0.3.tar.gz’
    Content type ‘application/x-gzip’ length 108094 bytes (105 Kb)
    opened URL
    ==================================================
    downloaded 105 Kb

    * installing *source* package ‘rmongodb’ …
    ** package ‘rmongodb’ successfully unpacked and MD5 sums checked
    ** libs
    cc -I/usr/local/Cellar/r/2.15.1/R.framework/Resources/include -DNDEBUG -I/usr/local/include -D_MONGO_USE_GETADDRINFO -DR_SAFETY_NET -fPIC -g -O2 -c api.c -o api.o
    cc -I/usr/local/Cellar/r/2.15.1/R.framework/Resources/include -DNDEBUG -I/usr/local/include -D_MONGO_USE_GETADDRINFO -DR_SAFETY_NET -fPIC -g -O2 -c api_bson.c -o api_bson.o
    cc -I/usr/local/Cellar/r/2.15.1/R.framework/Resources/include -DNDEBUG -I/usr/local/include -D_MONGO_USE_GETADDRINFO -DR_SAFETY_NET -fPIC -g -O2 -c api_gridfs.c -o api_gridfs.o
    cc -I/usr/local/Cellar/r/2.15.1/R.framework/Resources/include -DNDEBUG -I/usr/local/include -D_MONGO_USE_GETADDRINFO -DR_SAFETY_NET -fPIC -g -O2 -c api_mongo.c -o api_mongo.o
    cc -I/usr/local/Cellar/r/2.15.1/R.framework/Resources/include -DNDEBUG -I/usr/local/include -D_MONGO_USE_GETADDRINFO -DR_SAFETY_NET -fPIC -g -O2 -c bson.c -o bson.o
    cc -I/usr/local/Cellar/r/2.15.1/R.framework/Resources/include -DNDEBUG -I/usr/local/include -D_MONGO_USE_GETADDRINFO -DR_SAFETY_NET -fPIC -g -O2 -c encoding.c -o encoding.o
    cc -I/usr/local/Cellar/r/2.15.1/R.framework/Resources/include -DNDEBUG -I/usr/local/include -D_MONGO_USE_GETADDRINFO -DR_SAFETY_NET -fPIC -g -O2 -c gridfs.c -o gridfs.o
    cc -I/usr/local/Cellar/r/2.15.1/R.framework/Resources/include -DNDEBUG -I/usr/local/include -D_MONGO_USE_GETADDRINFO -DR_SAFETY_NET -fPIC -g -O2 -c md5.c -o md5.o
    cc -I/usr/local/Cellar/r/2.15.1/R.framework/Resources/include -DNDEBUG -I/usr/local/include -D_MONGO_USE_GETADDRINFO -DR_SAFETY_NET -fPIC -g -O2 -c mongo.c -o mongo.o
    cc -I/usr/local/Cellar/r/2.15.1/R.framework/Resources/include -DNDEBUG -I/usr/local/include -D_MONGO_USE_GETADDRINFO -DR_SAFETY_NET -fPIC -g -O2 -c net.c -o net.o
    net.c:32:17: error: use of undeclared identifier ‘MSG_NOSIGNAL’
    int flags = MSG_NOSIGNAL;
    ^
    net.c:194:9: warning: implicit declaration of function ‘sigaction’ is invalid in C99 [-Wimplicit-function-declaration]
    if (sigaction(SIGPIPE, (struct sigaction *)NULL, &act) < 0)
    ^
    1 warning and 1 error generated.
    make: *** [net.o] Error 1
    ERROR: compilation failed for package ‘rmongodb’
    * removing ‘/usr/local/Cellar/r/2.15.1/R.framework/Versions/2.15/Resources/library/rmongodb’

    The downloaded source packages are in
    ‘/private/var/folders/x2/n0qjmjds2sn02ws63xk0p62w0000gn/T/RtmpNjqOfe/downloaded_packages’
    Warning message:
    In install.packages("rmongodb") :
    installation of package ‘rmongodb’ had non-zero exit status
    Help?

    Reply
  10. Lin

    Hey,
    I installed RMongo but cannot run it. Here is the message I get:

    library(RMongo)
    Loading required package: rJava
    Error in get(Info[i, 1], envir = env) :
    internal error -3 in R_decompress1
    Error: package ‘rJava’ could not be loaded

    local({pkg <- select.list(sort(.packages(all.available = TRUE)),graphics=TRUE)
    + if(nchar(pkg)) library(pkg, character.only=TRUE)})
    Error in get(Info[i, 1], envir = env) :
    internal error -3 in R_decompress1
    Error: package/namespace load failed for ‘rJava’

    I have installed rJava several times, but it doesn work out. Can you help me? Thanks!

    Reply
      1. Lin

        install.packages(“rJava”)
        Installing package(s) into ‘C:/Users/yelin/Documents/R/win-library/2.15′
        (as ‘lib’ is unspecified)
        Warning: package ‘rJava’ is in use and will not be installed
        > library(rJava)

        It seems like it fixes by itself now…but when I run the commands I got the error message again.
        output <-dbInsertDocument(mongo,"test_data",'{"foo":"bar"}')

        Error in .jcall(rmongo.object@javaMongo, "S", "dbInsertDocument", collection, :
        com.mongodb.MongoException$Network: can't say something

        Is this because of Java is not running correctly? Thanks!

      2. tommychheng Post author

        can you confirm it’s running by running the mongo shell?

        The error message you posted suggests that the mongo server is not running.

      1. SCallan

        I have Rmongo installed in my library. With mongodb running, in Rcmdr I load Rmongo and do a query.

  11. Lin

    So do you open Rcmdr first then load Rmongo package? But how do you use the commander to read mongodb file? Thanks!

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s