Code Monkey home page Code Monkey logo

rdata's Introduction

rdata

A wrapper around Renjin to provide a convenient way to load the contents of file saved in R's RData format in Clojure.

One might want to do this because they have found an interesting dataset that has been published in this format.

Installation

We have not yet released to clojars, so the recommended installation is with deps.edn:

For deps.edn users:

appliedsciencestudio/rdata {:git/url "https://github.com/appliedsciencestudio/rdata/"
                            :sha "151e6dead06b38995f1f30b09d954a060f7a2a9c"}

Because of a deps.edn issue with transitive dependencies which depend on non-standard repositories, you must have the key/value pair "bedatadriven" {:url "https://nexus.bedatadriven.com/content/groups/public/" in the :mvn/repos of your deps.edn file. For instance:

:mvn/repos {"central" {:url "https://repo1.maven.org/maven2/"}
            "clojars" {:url "https://clojars.org/repo"}
            "bedatadriven" {:url "https://nexus.bedatadriven.com/content/groups/public/"}}

Run the tests:

clj -A:test:runner

You can also build a deployable jar of this library:

$ clojure -A:jar

then install it locally:

$ clojure -A:install

Usage

This library exports a single useful function, read-rdata, which -- somewhat predictably -- reads a file saved in the RData format used by R.

The file contents are returned as nested maps (RData files can contain arbitrarily nested data). The top-most level of the returned structure is a key/value mapping from name to dataset, while the leaf nodes will always be vectors of some primitive type (int, double, inst, and so on).

The R attributes stored with each value are attached to the Clojure translation of that value as Clojure metadata.

(def mers
  (read-rdata "test/data/mers_korea_2015.RData" {:key-fn keyword}))

(keys mers)
;;=> (:mers_korea_2015)

(-> mers :mers_korea_2015 keys)
;;=> (:linelist :contacts)

(-> mers :mers_korea_2015 :linelist keys)
;;=> (:id :age :age_class :sex :place_infect :reporting_ctry :loc_hosp :dt_onset :dt_report :week_report :dt_start_exp :dt_end_exp :dt_diag :outcome :dt_death)

(-> mers :mers_korea_2015 :linelist :place_infect)
[1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2]

;; hm, place 1 or place 2? Maybe the metadata can tell us what this means...
(-> mers :mers_korea_2015 :linelist :place_infect meta)
;;=> {:class ["factor"], :levels ["Middle East" "Outside Middle East"]}

;; Ah, it's a two value factor (note that R values start from 1, so one
;; must decrement the factor's index to look it up in the vector held in
;; the meta.

License

Copyright © 2020 Applied Science

Distributed under the MIT License.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.