Stackaccount1
Stackaccount1

Reputation: 149

R Shiny, Object 'input' not found

I am getting the error: Error in ..stacktraceon..({ : object 'input' not found. This code works correctly if the api key/url is formatted without the inputs. I believe the error is at line 25 and 27 where the input$apikey is switched, or that the shiny app doesn't have a value for those to start with.

The goal of this app is to take api key inputted by user and display it nicely with timevis for a schedule. I tried putting the api call and data cleaning below the server function but that does not work. I appreciate any help.

library(shiny)
library(httr)
library(jsonlite)
library(lubridate)
library(tools)
library(timevis)

# Define UI for application 
ui <- fluidPage(

    # Application title
    titlePanel("User Schedule"),
    # Input API INFo
    textInput(inputId = "apikey1", label = h3("API Key"), value = "enterkey"),
    textInput(inputId = "userkey1", label = h3("USER EMAIL"), value = "enteremail"),
    submitButton("Update View", icon("refresh")),
    hr(),
    timevisOutput("timeline"))

#Make API call and prepare Timeline information
options(stringsAsFactors = FALSE)

apikey2 <- input$apikey1

userkey2 <- input$userkey1

master_url <- sprintf("https://%s?api_key= %s",userkey2,apikey2)

my_url <- master_url

my_raw_result <- httr::GET(my_url)

my_raw_result

my_raw_result_text <- content(my_raw_result, "text")

my_raw_result_json <- fromJSON(my_raw_result_text, flatten = TRUE)

my_raw_result_df <- as.data.frame(my_raw_result_json)

dt <-  my_raw_result_df

dt[dt==""] <- NA # Replace all blank to NA
dt$start <- as.Date(as.character(dt$Schedule.item.date, 1,10)) # reformat the date field to my preferred format
dt$end <- NA
dt$content <- as.character(dt$schedule.item.type) # formate the content as text characters


# Define server logic required to draw time line
server <- function(input, output) {
    output$timeline <- renderTimevis({timevis(dt)})
}


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

Upvotes: 1

Views: 1436

Answers (1)

Limey
Limey

Reputation: 12451

As @r2evans says, Your code which creates dt is outside the server function, so it's in the global environment. The variable input doesn't exist there. So, put the code from options(stringsAsFactors = FALSE) to dt$content <- as.character(dt$schedule.item.type) inside the server function. That will give you access to input.

That answers your question, but you will have more to do:

  • input is accessible only within an active context, so you'll have to wrap your initialisation code in observe({}) or similar.
  • You'll need to prevent renderTimevis() from erroring with incorrect input. Something like:
    output$timeline <- renderTimevis({
        req(input$apikey1, input$userkey1)
        if (is.data.frame(dt)) timevis(dt)
      })
  • You might consider using placeholder= rather than value= in your textInputs to provide initial guidance to your users.
  • You'll need to modify your initialisation code to prevent errors on start up due to missing values. Something like:
observe({
    req(input$apikey1, input$userkey2)
    <... your initialisation code  ...>
  })

At least gets your app's GUI to display.

Upvotes: 3

Related Questions