{"id":663,"date":"2012-10-25T12:46:55","date_gmt":"2012-10-25T12:46:55","guid":{"rendered":"http:\/\/garysieling.com\/blog\/?p=663"},"modified":"2012-10-25T12:46:55","modified_gmt":"2012-10-25T12:46:55","slug":"building-a-json-webservice-in-r","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/building-a-json-webservice-in-r\/","title":{"rendered":"Building a JSON webservice in R"},"content":{"rendered":"<p>R is a programming language for mathematics and statistics. There are several R libraries available to support web development, including rjson and RJSONIO (note case &#8211; R library names are case sensitive). RJSONIO is based on rjson, but with modifications to improve performance working with large JSON payloads.<\/p>\n<p>The example below returns the data required to render a scatter chart with a best fit line like this:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/172.104.26.128\/wp-content\/uploads\/2012\/10\/r-project-chart.png\" alt=\"Chart in R-Studio\" title=\"Chart in R-Studio\" width=\"441\" height=\"390\" class=\"alignnone size-full wp-image-664\" srcset=\"https:\/\/www.garysieling.com\/blog\/wp-content\/uploads\/2012\/10\/r-project-chart.png 441w, https:\/\/www.garysieling.com\/blog\/wp-content\/uploads\/2012\/10\/r-project-chart-300x265.png 300w\" sizes=\"(max-width: 441px) 100vw, 441px\" \/><\/p>\n<p>This requires that you&#8217;ve already set up <a href=\"http:\/\/garysieling.com\/blog\/building-a-statistical-significance-testing-web-service-powered-by-r\">rApache<\/a>. To install RJSONIO, check http:\/\/cran.r-project.org\/src\/contrib\/ to find the latest version, and use that filename in the following. It appears that CRAN removes the old versions- unfortunately they don&#8217;t have an easy way to just pull the latest.<\/p>\n<pre>\nwget http:\/\/cran.r-project.org\/src\/contrib\/RJSONIO_1.0-1.tar.gz\nR CMD INSTALL RJSONIO_1.0-1.tar.gz\n<\/pre>\n<p>The following code will pull JSON data from the URL, parse it, and return a linear model. This includes information for the graph, error bars, best fit line, axis intercepts, etc. For a charting package like <a href=\"http:\/\/garysieling.com\/blog\/tag\/extjs\">ExtJS<\/a>, the data may need to be re-formatted to render successfully, which fortunately R is quite good at.<\/p>\n<pre lang=\"R\">\nsetContentType(\"application\/json\")\n\nlibrary(RJSONIO)\nlibrary(utils)\n\ncat(GET$data)\n\njson <- fromJSON(URLdecode(GET$data))\nfit <- lm(x ~ y)\n\ncat(toJSON(fit))\n<\/pre>\n<p>Strictly speaking, this should be a POST, but this makes the example easier to read.<\/p>\n<p>Some examples of this use the basicJSONHandler, like below. I found that this does not return numerics for JSON arrays, and opted not to use it.<\/p>\n<pre>\nh = basicJSONHandler()\nx = fromJSON(\"[1, 2, 3]\", h)\n<\/pre>\n<p>If you've read this far, you may enjoy my review of the <a href=\"http:\/\/garysieling.com\/blog\/book-review-r-cookbook\">R Cookbook.<\/a><\/p>\n<p>This URL accepts and returns JSON results:<br \/>\nhttp:\/\/localhost:8081\/R\/fit.R?data={%22x%22:[1.5,2,7,8,15],%22y%22:[1.5,2,7,8,15]}<\/p>\n<p>Results:<\/p>\n<pre>\n{ \"assign\" : [ 0,\n      1\n    ],\n  \"call\" : { \"\" : \"lm\",\n      \"formula\" : [ \"~\",\n          \"x\",\n          \"y\"\n        ]\n    },\n  \"coefficients\" : { \"(Intercept)\" : 0.95408999999999999,\n      \"y\" : 0.78297000000000005\n    },\n  \"df.residual\" : 3,\n  \"effects\" : { \"\" : 0.23488999999999999,\n      \"(Intercept)\" : -13.864000000000001,\n      \"y\" : 8.5699000000000005\n    },\n  \"fitted.values\" : { \"1\" : 2.1284999999999998,\n      \"2\" : 2.52,\n      \"3\" : 6.4348999999999998,\n      \"4\" : 7.2179000000000002,\n      \"5\" : 12.699\n    },\n  \"model\" : { \"x\" : [ 1,\n          3,\n          6,\n          9,\n          12\n        ],\n      \"y\" : [ 1.5,\n          2,\n          7,\n          8,\n          15\n        ]\n    },\n  \"qr\" : { \"pivot\" : [ 1,\n          2\n        ],\n      \"qr\" : [ { \"(Intercept)\" : -2.2361,\n            \"y\" : -14.981999999999999\n          },\n          { \"(Intercept)\" : 0.44721,\n            \"y\" : 10.945\n          },\n          { \"(Intercept)\" : 0.44721,\n            \"y\" : -0.17422000000000001\n          },\n          { \"(Intercept)\" : 0.44721,\n            \"y\" : -0.26557999999999998\n          },\n          { \"(Intercept)\" : 0.44721,\n            \"y\" : -0.90512999999999999\n          }\n        ],\n      \"qraux\" : [ 1.4472,\n          1.2826\n        ],\n      \"rank\" : 2,\n      \"tol\" : 9.9999999999999995e-08\n    },\n  \"rank\" : 2,\n  \"residuals\" : { \"1\" : -1.1285000000000001,\n      \"2\" : 0.47997000000000001,\n      \"3\" : -0.43489,\n      \"4\" : 1.7821,\n      \"5\" : -0.69865999999999995\n    },\n  \"terms\" : [ \"~\",\n      \"x\",\n      \"y\"\n    ],\n  \"xlevels\" : {  }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>R is a programming language for mathematics and statistics. There are several R libraries available to support web development, including rjson and RJSONIO (note case &#8211; R library names are case sensitive). RJSONIO is based on rjson, but with modifications to improve performance working with large JSON payloads. The example below returns the data required &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/building-a-json-webservice-in-r\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Building a JSON webservice in R&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4,6,27],"tags":[85,322,450,451,589],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/663"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=663"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/663\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}