Skip to content

Instantly share code, notes, and snippets.

@Roon
Forked from sebkopf/README.md
Created December 9, 2015 18:50
Show Gist options
  • Select an option

  • Save Roon/c9080fd43da27735be44 to your computer and use it in GitHub Desktop.

Select an option

Save Roon/c9080fd43da27735be44 to your computer and use it in GitHub Desktop.

Revisions

  1. @sebkopf sebkopf revised this gist Mar 13, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion wa_lib.R
    Original file line number Diff line number Diff line change
    @@ -30,7 +30,7 @@ WolframClient <- setRefClass(

    if (info$success != "true" || info$error != "false")
    stop("There was a problem with this query. Please check the query infos:\n",
    paste(paste(names(info), info, sep = " = "), sep = "\n"))
    paste(paste(names(info), info, sep = " = "), collapse = "\n"))

    return(result)
    }
  2. @sebkopf sebkopf revised this gist Mar 13, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Wolfram Alpha API from R

    The attached code file provides an easy basic interface to the Wolfram Alpha API. Inspired by a the [wolframalpha](https://pypi.python.org/pypi/wolframalpha/1.0) module available for Python.
    The attached code file provides an easy basic interface to the Wolfram Alpha API. Inspired by the [wolframalpha](https://pypi.python.org/pypi/wolframalpha/1.0) module available for Python.

    ```r
    source("wa_lib.R")
  3. @sebkopf sebkopf revised this gist Mar 13, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Wolfram API from R
    # Wolfram Alpha API from R

    The attached code file provides an easy basic interface to the Wolfram Alpha API. Inspired by a the [wolframalpha](https://pypi.python.org/pypi/wolframalpha/1.0) module available for Python.

  4. @sebkopf sebkopf revised this gist Mar 13, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Wolfram API from R

    The attached code file provides an easy basic interface to the Wolfram Alpha API. Inspired by a the [!wolframalpha](https://pypi.python.org/pypi/wolframalpha/1.0) module available for Python.
    The attached code file provides an easy basic interface to the Wolfram Alpha API. Inspired by a the [wolframalpha](https://pypi.python.org/pypi/wolframalpha/1.0) module available for Python.

    ```r
    source("wa_lib.R")
  5. @sebkopf sebkopf revised this gist Mar 13, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Wolfram API from R

    The attached code file provides an easy basic interface to the Wolfram Alpha API. Inspired by a the [wolframalpha](!https://pypi.python.org/pypi/wolframalpha/1.0) module available for Python.
    The attached code file provides an easy basic interface to the Wolfram Alpha API. Inspired by a the [!wolframalpha](https://pypi.python.org/pypi/wolframalpha/1.0) module available for Python.

    ```r
    source("wa_lib.R")
  6. @sebkopf sebkopf revised this gist Mar 13, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Wolfram API from R

    The attached code file provides an easy basic interface to the Wolfram Alpha API.
    The attached code file provides an easy basic interface to the Wolfram Alpha API. Inspired by a the [wolframalpha](!https://pypi.python.org/pypi/wolframalpha/1.0) module available for Python.

    ```r
    source("wa_lib.R")
  7. @sebkopf sebkopf revised this gist Mar 13, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    # Wolfram API from R

    The attached code file provides an easy basic interface to the Wolfram Alpha API.

    ```r
    library(XML)
    library(plyr)
    source("wa_lib.R")
    ```

  8. @sebkopf sebkopf revised this gist Mar 13, 2015. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,8 @@
    # Wolfram API from R

    ```r
    library(XML)
    library(plyr)
    source("wa_lib.R")
    ```

    @@ -25,6 +27,7 @@ result <- wa$query("pi")
    ```

    ```r
    library(knitr)
    kable(result$get_plaintext(), format = "html")
    ```

  9. @sebkopf sebkopf created this gist Mar 13, 2015.
    149 changes: 149 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,149 @@
    # Wolfram API from R

    ```r
    source("wa_lib.R")
    ```

    ## Initialize client

    This requires an app ID. Sign into your wolfram account to generate one.

    ```r
    app_id <- ""
    wa <- WolframClient(app_id)
    ```

    # Ask a simple question


    ```r
    result <- wa$query("pi")
    ```

    ```
    ## Querying wolfram alpha...
    ```

    ```r
    kable(result$get_plaintext(), format = "html")
    ```

    <table>
    <thead>
    <tr>
    <th style="text-align:left;"> pod </th>
    <th style="text-align:left;"> id </th>
    <th style="text-align:left;"> plaintext </th>
    </tr>
    </thead>
    <tbody>
    <tr>
    <td style="text-align:left;"> Input </td>
    <td style="text-align:left;"> Input </td>
    <td style="text-align:left;"> pi </td>
    </tr>
    <tr>
    <td style="text-align:left;"> Decimal approximation </td>
    <td style="text-align:left;"> DecimalApproximation </td>
    <td style="text-align:left;"> 3.1415926535897932384626433832795028841971693993751058... </td>
    </tr>
    <tr>
    <td style="text-align:left;"> Property </td>
    <td style="text-align:left;"> Property </td>
    <td style="text-align:left;"> pi is a transcendental number </td>
    </tr>
    <tr>
    <td style="text-align:left;"> Number line </td>
    <td style="text-align:left;"> NumberLine </td>
    <td style="text-align:left;"> </td>
    </tr>
    <tr>
    <td style="text-align:left;"> Continued fraction </td>
    <td style="text-align:left;"> ContinuedFraction </td>
    <td style="text-align:left;"> [3; 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 14, 2, 1, 1, 2, 2, 2, 2, 1, 84, 2, 1, 1, 15, ...] </td>
    </tr>
    <tr>
    <td style="text-align:left;"> Alternative representations </td>
    <td style="text-align:left;"> AlternativeRepresentations:MathematicalFunctionIdentityData </td>
    <td style="text-align:left;"> pi = 180 ° </td>
    </tr>
    <tr>
    <td style="text-align:left;"> Alternative representations </td>
    <td style="text-align:left;"> AlternativeRepresentations:MathematicalFunctionIdentityData </td>
    <td style="text-align:left;"> pi = -i log(-1) </td>
    </tr>
    <tr>
    <td style="text-align:left;"> Alternative representations </td>
    <td style="text-align:left;"> AlternativeRepresentations:MathematicalFunctionIdentityData </td>
    <td style="text-align:left;"> pi = cos^(-1)(-1) </td>
    </tr>
    <tr>
    <td style="text-align:left;"> Series representations </td>
    <td style="text-align:left;"> SeriesRepresentations:MathematicalFunctionIdentityData </td>
    <td style="text-align:left;"> pi = 4 sum_(k=0)^infinity (-1)^k/(2 k+1) </td>
    </tr>
    <tr>
    <td style="text-align:left;"> Series representations </td>
    <td style="text-align:left;"> SeriesRepresentations:MathematicalFunctionIdentityData </td>
    <td style="text-align:left;"> pi = -2+2 sum_(k=1)^infinity 2^k/(binomial(2 k, k)) </td>
    </tr>
    <tr>
    <td style="text-align:left;"> Series representations </td>
    <td style="text-align:left;"> SeriesRepresentations:MathematicalFunctionIdentityData </td>
    <td style="text-align:left;"> pi = sum_(k=0)^infinity (50 k-6)/(2^k binomial(3 k, k)) </td>
    </tr>
    <tr>
    <td style="text-align:left;"> Integral representations </td>
    <td style="text-align:left;"> IntegralRepresentations:MathematicalFunctionIdentityData </td>
    <td style="text-align:left;"> pi = 2 integral_0^infinity 1/(t^2+1) dt </td>
    </tr>
    <tr>
    <td style="text-align:left;"> Integral representations </td>
    <td style="text-align:left;"> IntegralRepresentations:MathematicalFunctionIdentityData </td>
    <td style="text-align:left;"> pi = 4 integral_0^1 sqrt(1-t^2) dt </td>
    </tr>
    <tr>
    <td style="text-align:left;"> Integral representations </td>
    <td style="text-align:left;"> IntegralRepresentations:MathematicalFunctionIdentityData </td>
    <td style="text-align:left;"> pi = 2 integral_0^infinity (sin(t))/t dt </td>
    </tr>
    </tbody>
    </table>

    # Ask a more specific question


    ```r
    result <- wa$query("ion equivalents of zinc chloride")
    ```

    ```
    ## Querying wolfram alpha...
    ```

    ```r
    kable(result$get_plaintext(), format = "html")
    ```

    <table>
    <thead>
    <tr>
    <th style="text-align:left;"> pod </th>
    <th style="text-align:left;"> id </th>
    <th style="text-align:left;"> plaintext </th>
    </tr>
    </thead>
    <tbody>
    <tr>
    <td style="text-align:left;"> Input interpretation </td>
    <td style="text-align:left;"> Input </td>
    <td style="text-align:left;"> zinc chloride | ion equivalents </td>
    </tr>
    <tr>
    <td style="text-align:left;"> Result </td>
    <td style="text-align:left;"> IonProperties:ChemicalData </td>
    <td style="text-align:left;"> Zn^(2+) (zinc) | 1
    Cl^(-) (chloride) | 2 </td>
    </tr>
    </tbody>
    </table>
    71 changes: 71 additions & 0 deletions wa_lib.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    #' Basic Wolfram Alpha client and result objects
    library(XML)
    library(plyr)

    WolframClient <- setRefClass(
    "WolframClient",
    fields = list(
    app_id = "character"
    ),

    methods = list(

    initialize = function(app_id) {
    "initialize Wolfram Alpha Client"

    app_id <<- app_id
    },

    query = function(query, quiet = TRUE) {
    "run a query against the wolfram alpha database"

    message("Querying wolfram alpha...")
    wa_url <- paste0("http://api.wolframalpha.com/v2/query?input=",
    URLencode(query), "&appid=", app_id)

    destfile <- tempfile()
    download.file(wa_url, destfile, method = "wget", quiet = quiet)
    result <- WolframResult(xmlInternalTreeParse(destfile))
    info <- result$get_info()

    if (info$success != "true" || info$error != "false")
    stop("There was a problem with this query. Please check the query infos:\n",
    paste(paste(names(info), info, sep = " = "), sep = "\n"))

    return(result)
    }
    )
    )

    WolframResult <- setRefClass(
    "WolframResult",
    fields = list(
    doc = "XMLInternalDocument"
    ),

    methods = list(
    initialize = function(doc) {
    doc <<- doc
    },

    xml = function() doc,

    get_info = function() {
    as.list(xpathApply(doc, "//queryresult", xmlAttrs)[[1]])
    },

    get_pods = function() {
    xpathApply(doc, "//pod")
    },

    get_plaintext = function() {
    ldply(get_pods(), function(pod) {
    data.frame(
    pod = xmlGetAttr(pod, "title"),
    id = xmlGetAttr(pod, "id"),
    plaintext = unlist(xpathApply(pod, "subpod/plaintext", xmlValue))
    )
    })
    }
    )
    )