-1

http://snomedct.t3as.org/ This is a web service that will analyse English clinical text, and report any concepts that can be detected.
For e.g.- I have headache. It will identify headache as a Symptom.

Now what I would like to do is send the sentence to the web service through R, and get the table back from the web page to R for further analysis purpose.

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
user3117837
  • 87
  • 1
  • 8
  • 1
    have you tried anything or want us to code this for you? – milos.ai Sep 03 '15 at 19:07
  • Take a look at the [rvest](http://blog.rstudio.org/2014/11/24/rvest-easy-web-scraping-with-r/) package. – ulfelder Sep 03 '15 at 20:22
  • 1
    Sounds like an _awesome_ opportunity to sit down with a cold beverage and peruse the [CRAN Task View on Web Technologies](https://cran.rstudio.com/web/views/WebTechnologies.html). – hrbrmstr Sep 03 '15 at 22:53
  • @hrbrmstr I have tried the httr package. I am more into data analysis, and this is a completely new thing for me. Note sure how to send the text from R to the web service. # TExt note to be sent to Snomed Browser Note<- c("I am suffering from severe headache", "I had a sleepless night") library(httr) url<-"http://snomedct.t3as.org/snomed-coder- web/rest/v1.0/snomedctCodes" GET(url) – user3117837 Sep 04 '15 at 03:55

1 Answers1

10

If we take their example curl command-line:

curl -s --request POST \
        -H "Content-Type: application/x-www-form-urlencoded" \
        --data-urlencode "The patient had a stroke." \
        http://snomedct.t3as.org/snomed-coder-web/rest/v1.0/snomedctCodes

that can be translated to httr pretty easily.

The -s means "silent" (no progress meter or error messages) so we don't really have to translate that.

Any -H means to add a header to the request. This particular Content-Type header can be handled better with the encode parameter to httr::POST.

The --data-urlencode parameter says to URL encode that string and put it in the body of the request.

Finally, the URL is the resource to call.

library(httr)

result <- POST("http://snomedct.t3as.org/snomed-coder-web/rest/v1.0/snomedctCodes", 
               body="The patient had a stroke.",
               encode="form")

Since you don't do this regularly, you can wrap the POST call with with_verbose() to see what's going on (look that up in the httr docs).

There are a ton of nuances that one should technically do after this (like check the HTTP status code with stop_for_status(), warn_for_status() or even just status_code(), but for simplicity let's assume the call works (this one is their example so it does work and returns a 200 HTTP status code which is A Good Thing).

By default, that web service is returning JSON, so we need to convert it to an R object. While httr does built-in parsing, I like to use the jsonlite package to process the result:

dat <- jsonlite::fromJSON(content(result, as="text"),  flatten=TRUE)

The fromJSON function takes a few parameters that are intended to help shape JSON into a reasonable R data structure (many APIs return horrible JSON and/or XML). This API would fit into the "horrible" category. The data in dat is pretty gnarly and further decoding of it would be a separate SO question.

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205