# Sample R script to query GraphQL API

**URL:** <https://community.opentargets.org/t/sample-r-script-to-query-graphql-api/510>\
**Category:** GraphQL API\
**Created:** [17 March 2022 08:08 UTC](https://community.opentargets.org/t/sample-r-script-to-query-graphql-api/510 "2022-03-17T08:08:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![enricoferrero](https://avatars.discourse-cdn.com/v4/letter/e/a587f6/32.png) [@enricoferrero](https://community.opentargets.org/u/enricoferrero)\
**Post date:** [17 March 2022 08:08 UTC](https://community.opentargets.org/t/sample-r-script-to-query-graphql-api/510/1 "2022-03-17T08:08:38Z")

</div>

Hello,

When I look at the [documentation](https://platform-docs.opentargets.org/data-access/graphql-api#sample-scripts), I can only see the sample Python script to query the GraphQL API. If I click on the R tab, nothing happens.

Would it be possible to add the R sample script (and/or can someone share it here)?

Thanks!

---

<div class="post-metadata">

**Author:** ![hcornu](https://dub1.discourse-cdn.com/flex017/user_avatar/community.opentargets.org/hcornu/32/716_2.png) [@hcornu](https://community.opentargets.org/u/hcornu)\
**Post date:** [18 March 2022 09:43 UTC](https://community.opentargets.org/t/sample-r-script-to-query-graphql-api/510/2 "2022-03-18T09:43:52Z")

</div>

Hi @enricoferrero, and welcome to the Open Targets Community! 🥳

I’m not sure what’s happening because I was able to access the R script in our documentation, but I have copied it below:

```auto
# Install relevant library for HTTP requests
library(httr)

# Set gene_id variable for AR (androgen receptor)
gene_id <- "ENSG00000169083"

# Build query string to get general information about AR and genetic constraint and tractability assessments 
query_string = "
  query target($ensemblId: String!){
    target(ensemblId: $ensemblId){
      id
      approvedSymbol
      biotype
      geneticConstraint {
        constraintType
        exp
        obs
        score
        oe
        oeLower
        oeUpper
      }
      tractability {
        id
        modality
        value
      }
    }
  }
"

# Set base URL of GraphQL API endpoint
base_url <- "https://api.platform.opentargets.org/api/v4/graphql"

# Set variables object of arguments to be passed to endpoint
variables <- list("ensemblId" = gene_id)

# Construct POST request body object with query string and variables
post_body <- list(query = query_string, variables = variables)

# Perform POST request
r <- POST(url=base_url, body=post_body, encode='json')

# Print data to RStudio console
print(content(r)$data)

```

---

<div class="post-metadata">

**Author:** ![enricoferrero](https://avatars.discourse-cdn.com/v4/letter/e/a587f6/32.png) [@enricoferrero](https://community.opentargets.org/u/enricoferrero)\
**Post date:** [18 March 2022 10:01 UTC](https://community.opentargets.org/t/sample-r-script-to-query-graphql-api/510/3 "2022-03-18T10:01:48Z")

</div>

Thanks a lot @hcornu! That’s weird, I just checked again and I still can’t get to the R script in the docs - I guess it’s an issue with my browser (Microsoft Edge 97.0.1072.62) then.

---

<div class="post-metadata">

**Author:** ![Shicheng\_Guo](https://dub1.discourse-cdn.com/flex017/user_avatar/community.opentargets.org/shicheng_guo/32/217_2.png) [@Shicheng\_Guo](https://community.opentargets.org/u/Shicheng_Guo)\
**Post date:** [11 August 2023 06:16 UTC](https://community.opentargets.org/t/sample-r-script-to-query-graphql-api/510/4 "2023-08-11T06:16:03Z")

</div>

I am wondering whether this R code works well or not? For me, It returns nothing. Here is the return:

![1691734551820](https://europe1.discourse-cdn.com/flex017/uploads/opentargets/original/1X/ede88e71d33f73245bc249e30ca910517fec06c1.png)

---

<div class="post-metadata">

**Author:** ![irene](https://dub1.discourse-cdn.com/flex017/user_avatar/community.opentargets.org/irene/32/50_2.png) [@irene](https://community.opentargets.org/u/irene)\
**Post date:** [11 August 2023 08:43 UTC](https://community.opentargets.org/t/sample-r-script-to-query-graphql-api/510/5 "2023-08-11T08:43:28Z")

</div>

Hi @Shicheng_Guo,

I am guessing you took this snippet from our documentation. The response is indeed empty because the tractability was not up-to-date with the latest changes - `tractability` no longer has an id.  
This is the correct request:

```auto
  query target($ensemblId: String!){
    target(ensemblId: $ensemblId){
      id
      approvedSymbol
      biotype
      geneticConstraint {
        constraintType
        exp
        obs
        score
        oe
        oeLower
        oeUpper
      }
      tractability {
        label
        modality
        value
      }
    }
  }

```

We have corrected the documentation, and the changes will be shortly available. Thanks for reporting!

---

<div class="post-metadata">

**Author:** ![enricoferrero](https://avatars.discourse-cdn.com/v4/letter/e/a587f6/32.png) [@enricoferrero](https://community.opentargets.org/u/enricoferrero)\
**Post date:** [11 August 2023 09:02 UTC](https://community.opentargets.org/t/sample-r-script-to-query-graphql-api/510/6 "2023-08-11T09:02:22Z")

</div>

Wrong recipient?

Enrico

**Enrico Ferrero**

+41 79 666 3164

[enrico.ferrero@novartis.com](mailto:enrico.ferrero@novartis.com)

![~WRD0000.jpg](https://europe1.discourse-cdn.com/flex017/uploads/opentargets/original/1X/52d5f1c08649212fe6768d2cacdcf8df711810d9.jpeg)
