Sample R script to query GraphQL API

Hello,

When I look at the documentation, 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!

1 Like

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

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:

# 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)
1 Like

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.

2 Likes