Giter Site home page Giter Site logo

rquery's Introduction

rQuery

Bringing the jQuery syntax to R.

Install with devtools:

library(devtools)
install_github("rQuery","jamiefolson",subdir="pkg")

To show how things work, consider the following document fragment:

<html>
<body>
<ul class="level-1">
  <li class="item-i">I</li>
  <li class="item-ii">II
    <ul class="level-2">
      <li class="item-a">A</li>
      <li class="item-b">B
        <ul class="level-3">
          <li class="item-1">1</li>
          <li class="item-2">2</li>
          <li class="item-3">3</li>
        </ul>
      </li>
      <li class="item-c">C</li>
    </ul>
  </li>
  <li class="item-iii">III</li>
</ul>
</body>
</html>

Currently only a few jQuery functions are implemented. Since $ is a pretty important generic function, I thought it best not to overwrite it. Instead, you can access the basic jQuery selector with the function rQuery or its alias %$%.

> library(XML)
> library(rQuery)
Loading required package: selectr
> doc = htmlParse("tmp.html")
> `%$%`(doc,"ul.level-2")
[[1]]
<ul class="level-2"><li class="item-a">A</li>
      <li class="item-b">B
        <ul class="level-3"><li class="item-1">1</li>
          <li class="item-2">2</li>
          <li class="item-3">3</li>
        </ul></li>
      <li class="item-c">C</li>
    </ul> 

jQuery methods are implemented as S4 methods on the class rQueryResult.

> showMethods(classes="rQueryResult")
...
Function: rQuery.add (package rQuery)
rquery="rQueryResult"

Function: rQuery.addClass (package rQuery)
rquery="rQueryResult"
...
> rQuery.class(`%$%`(doc,"ul"))
[1] "level-1"

In addition to the regular R syntax for calling methods, rQuery emulates the jQuery "chaining" syntax.

> `%$%`(doc,"body")$find("ul.level-2")$attr(x="value")$addClass("myclass")$prepend("<li>New item</li>")$addClass("AB")
[[1]]
<li class="AB">New item</li> 

> doc
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><ul class="level-1">
<li class="item-i">I</li>
  <li class="item-ii">II
    <ul class="level-2,myclass" x="value">
<li class="AB">New item</li>
<li class="item-a">A</li>
      <li class="item-b">B
        <ul class="level-3">
<li class="item-1">1</li>
          <li class="item-2">2</li>
          <li class="item-3">3</li>
        </ul>
</li>
      <li class="item-c">C</li>
    </ul>
</li>
  <li class="item-iii">III</li>
</ul></body></html>

Package Documentation

You can find the generated package documentation here.

rquery's People

Contributors

jamiefolson avatar ramnathv avatar

Stargazers

 avatar Ali Zaidi avatar James Chang avatar Sarven Capadisli avatar Tyler Rinker avatar Bruno P. Kinoshita avatar

Watchers

 avatar Alex Bresler avatar James Cloos avatar  avatar James Chang avatar

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.