Ethan V
Ethan V

Reputation: 1

R Shiny API Calls based on reactive input

I am new to R shiny, and Stack Overflow in general. I am trying to create my first dashboard. I have an API call that goes out and collects data from a website called The Blue Alliance. The Blue Alliance is a website that hosts data for high school robotics competitions.

However, in order for the API call to know what event to pull its data from, it needs an event code, which is one of the inputs from a dropdown menu in my UI. How can I make the whole API call reactive so that it subs in the input from the dropdown as part of the URL for the API call?

I have tried placing the reactive() before the #Pull Event Scoring comment by doing something like

reactive(Scores_S4 <- input$Event_Name)

I then tried using it to wrap my entire function, both with and without {} on either side of the parentheses. I have found on various websites they both suggest to and to not use {} when using the reactive function.

server <- function(input, output){
      reactive(
        #Pull Event live scoring
         Scores_S1 <- "https://www.thebluealliance.com/api/v3/event/2022",
         Scores_S2 <- input$Event_Name,
         Scores_S3 <- "?X-TBA-AUTH-KEY *API Key Goes Here*",
         FScores <- paste0(Scores_S1, Scores_S2, Scores_S3),
         EScores <- fromJSON(FScores),
         EScores <- as.data.frame(EScores)
         RPP1 <- ggplot(EScores, aes(reorder(`TeamName`, -`Avg. Hang`), `Avg. Hang`)) + geom_point()
         output$RPPP1 <- renderPlot(RPP1))
}

I know that the API call is correct if the value of input$Event_Name is put in, I just need help understanding the reactive portion so that it uses current widget outputs while the app is running. In my understanding, if done correctly, this will allow my ggplot function to create a new graph depending on what is chosen for the input$event. I have a call to "plot output("RPPP1")" in my UI, so this should work once I understand the reactive piece.

Thank you for any and all time put into this, it is all helping a great deal to learn from a community this great.

Upvotes: 0

Views: 368

Answers (1)

DaveArmstrong
DaveArmstrong

Reputation: 21757

Something like this might work:

EScores <- reactive({
  #Pull Event live scoring
  Scores_S1 <- "https://www.thebluealliance.com/api/v3/event/2022"
  Scores_S2 <- input$Event_Name
  Scores_S3 <- "?X-TBA-AUTH-KEY *API Key Goes Here*"
  FScores <- paste0(Scores_S1, Scores_S2, Scores_S3)
  EScores <- fromJSON(FScores)
  as.data.frame(EScores)
})
output$RPP1 <- renderPlot({
  ggplot(EScores(), aes(reorder(`TeamName`, -`Avg. Hang`), `Avg. Hang`)) + geom_point()
    output$RPPP1 <- renderPlot(RPP1))
})

But, presumably you'll need something between

  FScores <- paste0(Scores_S1, Scores_S2, Scores_S3),

and

  EScores <- fromJSON(FScores),

that actually makes a call to the API, like curl() or GET().

Upvotes: 0

Related Questions