1

I have been using R for about a year but am only a few weeks into Shiny. I have some code which calls an API and spits out a nice tables of results which I would like to use in a Shiny App - with the API being called when an action button is clicked.

I've been trouble shooting this for a few days but can't figure it out, below are a few of the things I have tried.

  • If I remove the action button, the API calls and displays as soon as the app is opened.
  • Replacing the API with simple text shows the text when the button is clicked.
  • Rendering the table inside the action results in the UI wondering where the table is (as button as not been pressed) and giving an error.

The API contains sensitive info so I have removed some details added a typical return (it returns Json to the browser when URL is visited). Any ideas on what i might be missing?

API return

{"meta":{"request":{"granularity":"Monthly","main_domain_only":false,"format":null,"domain":"cnn.com","start_date":"2017-02-01","end_date":"2017-02-28","limit":null,"country":"world"},"status":"Success","last_updated":"2017-04-30"},"visits":[{"date":"2017-02-01","visits":100000`}]}

UI.r

library(shiny)

    shinyUI(fluidPage(
    headerPanel(actionButton("do", "Click Me")),

        mainPanel(dataTableOutput("mytable"))
      ))

Server.r

library(shiny)

function(input, output) {
 #Call API, flatten Json return 
Visits_Final<- eventReactive(input$do, {
Results<- paste0("https://api.com/",
                  "site.com",
                  "apikey=***") %>%
  map(fromJSON, flatten = FALSE)



#Transform into DF
visits_temp= data.frame(date=NA,visits=NA,ID=NA)

for(i in 1:1){
  DF_L<- as.data.frame(Results[[i]]$visits)
  ID <- (rep(i, nrow(DF_L)))
  DF_L<- cbind(DF_L, ID)
  Visits_Final<-rbind(visits_temp, DF_L)}})

  #Output to UI  
 output$mytable <- renderDataTable({Visits_Final()})

}

Thanks in advance!

Edits

for(i in 1:i){

As per BigDataScientist comment, changed to

for(i in 1:1){

Code comments added

System info added: R 3.3.2 R Studio Version 1.0.143 OS Sierra 10_12_3

Ben
  • 93
  • 2
  • 8
  • `for(i in 1:i){`: this looks odd. Should it maybe be `for(i in 1:length(Results)){` ? – Tonio Liebrand May 19 '17 at 12:16
  • @BigDataScientist Correct, I had changed it for simplicity (the upper limit was variable depending on API call). Edited to 1:1 which works for the sake of this example - good spot, thanks. – Ben May 19 '17 at 12:55

1 Answers1

0

Solved - In the Server file I changed eventReactive to observeEvent

Honestly, not 100% understanding this documentation but it did help Shiny: what is the difference between observeEvent and eventReactive?

Feel free to comment with similar problems.

Community
  • 1
  • 1
Ben
  • 93
  • 2
  • 8
  • In almost all cases if you need to trigger an action with an action button, use `observeEvent`. The concept is clear: do this when this event was triggered (button was clicked). This is the easy part in reactive. Prepared to read the articles about reactive again and again when you need to use reactive values and expressions. Be sure to check Joe Cheng's video tutorial in Shiny dev conference, hosted in RStudio website, conference video section. – dracodoc May 19 '17 at 15:04
  • Thanks, lesson learnt haha! Will give the Joe Cheng video a watch. – Ben May 19 '17 at 15:19