RObservations #32: Creating an Instant Answer Oracle with httr and Shiny

[This article was first published on r – bensstats, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.


Knowing how to write API requests and handle their responses is a valuable skill that a developer, data
engineer or data analyst/scientist needs to know. In this short blog I share how its possible leverage DuckDuckGo’s instant answer API to create a oracle which can answer (some) of your questions using the httr package and Shiny.

Asking the instant answer API

The basic functionality is pretty straight forward. The code takes a individual query and returns an answer.


getAnswer<-function(query) {
 raw_data<- paste0("",
             "&format=json") %>%
      GET() %>%
      content(type = "application/json")

   return("Sorry, I don't have an answer")
 } else{

For some questions, the received answer is quite sophisticated (and quoted verbatim from Wikipedia).

getAnswer("What is R Programming?")

## [1] "R is a programming language for statistical computing and graphics supported by the R Core Team and the R Foundation for Statistical Computing. Created by statisticians Ross Ihaka and Robert Gentleman, R is used among data miners, bioinformaticians and statisticians for data analysis and developing statistical software. Users have created packages to augment the functions of the R language. According to user surveys and studies of scholarly literature databases, R is one of the most commonly used programming languages used in data mining. As of March 2022, R ranks 11th in the TIOBE index, a measure of programming language popularity, in which the language peaked in 8th place in August 2020. The official R software environment is an open-source free software environment within the GNU package, available under the GNU General Public License. It is written primarily in C, Fortran, and R itself. Precompiled executables are provided for various operating systems."

And for others, not so much.

getAnswer("What is 2+2?")

## [1] "Sorry, I don't have an answer"

Building a Shiny Application.

Whats nice about the API is that the request takes care of all the back-end we need for the front end to work. The code I used to create a simple user interface is:

# UI
ui <- fluidPage(
  # Application title
  titlePanel("Instant Answer Oracle"),
    '<h3>Made with the <img src="" width="20%"</img>API</h3>'
  # Sidebar layout
                label = "",
                placeholder = "What is your question?"),
                   "Find Answer")

    # Output

# Define server logic required 
server <- function(input, output) {
  observeEvent(input$search, {
    raw <-
             "&format=json") %>%
      GET() %>%
      content(type = "application/json")

    if (raw[["Abstract"]] == "") {
      output$heading <- renderText("")
      output$logo <- renderText("")
      output$answer <- renderText({
        "Sorry, I don't have an Answer"
      output$source <- renderText("")

    } else{
      output$heading <- renderText(h2(raw[["Heading"]]) %>% as.character())
      output$logo <- renderText(img(
          src = paste0("", raw[["Image"]]),
          width = "50%"
        ) %>% as.character)
      output$answer <-renderText(p(raw[["Abstract"]]) %>% as.character())
      output$source <- renderText(paste0('<p>Source: <a href="',raw[["AbstractURL"]],'"\\>',raw[["AbstractURL"]],"</a></p>") %>% as.character())

# Run the application
shinyApp(ui = ui, server = server)

If you don’t want to run this code on your own machine to check it out, you can check it out on Unfortunately the current version of WordPress doesn’t permit iframe embedding for it to be run here, but I can share a screenshot instead.


This endeavor was my personal exploration with the package. When working, I often find myself switching to python to use the requests module as I find that httr converts json data into nested list form which is not the easiest to work with syntax-wise. With this little project, I got more used to using the synatx and also got to share how its possible to leverage the power of APIs for building applications.

Thank you for reading!

Want to see more of my content?

Be sure to subscribe and never miss an update!

To leave a comment for the author, please follow the link and comment on their blog: r – bensstats. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)