Error: object of type 'closure' is not subsettable.

Hi Everyone, I'm new to Shiny.

I'm doing a restaurant recommender project where when user clicked an action button "Recommend me!", it will show one random restaurant nearby with all the details. My app was running well but if I clicked the "Recommend me!" button for the second time, it gave me an error message

Error: object of type 'closure' is not subsettable.

I've tried to look for solutions with similar error message but can't find any that works for me.

Does anyone know how can I overcome this issue? Appreciate some help. Thanks!

library(rjson)
library(shinyWidgets)
library(shiny)
library(shinycssloaders)
library(data.table)
#data source
setwd("C:\\Users\\22003354\\Downloads") #Directory
json_data <-jsonlite::fromJSON("nearby.json")

#data preprocessing
json_data2 <- data.frame(cbind(name=json_data$result$name,
                               address=json_data$result$vicinity,
                               status=json_data$result$business_status,
                               rating=json_data$result$rating,
                               open = json_data$result$opening_hours))

json_data2$open <- gsub("TRUE", "Open", json_data2$open)
json_data2$rating <- gsub(0, "No Rating", json_data2$rating)
json_data2$open <- gsub("FALSE", "Closed", json_data2$open)
json_data2$open[is.na(json_data2$open)] <- "unavailable"

json_data2

#ui section

ui <- fluidPage(
        br(),
        headerPanel(h1("Restaurant Recommender", align = "center")),
        #div(style = "position:absolute;right:1em;top:1em",
        br(),br(),br(),br(),br(),br(),
        
        fluidRow(                        
        column(12, align = "center",
            actionBttn(
              inputId = "rules",
              label = "Recommend Me!",
              style = "jelly",
              color = "warning",
              size = "lg"
            )
        )
        ),
        br(),br(),br(),br(),br(),br(),
        fluidRow(
          column(1), 
          column(10, align = "center",
                 withSpinner(htmlOutput("location_output"), image = "https://u01.appmifile.com/images/2019/09/10/b3788a8e-24d2-41b3-91c4-131968dab219.gif",  
                             image.height = "400px", image.width = "400px")
)))



server <- function(input, output, session) {
  show_result <- eventReactive(input$rules, {
    df <- copy(json_data2)
    Sys.sleep(1)
    if (input$rules == TRUE) {
      show_result <- df[sample(nrow(df),1),]
    }
    return(show_result)
    }
    )
    output$location_output <- renderText({
    
    outcome_text <- HTML(paste(
        
        "<div class='card'>",
        "<img src='https://cdn.iconscout.com/icon/free/png-256/restaurant-1495593-1267764.png' alt='John' style='width:20%'>",
        "<br><br>",
        "<p class='title'><font size='15'><b>", show_result()[,"name"],"</b></font></p>",
        "<p><font size='5'>",show_result()[,"address"],"</font></p>",
        "<p><font size='5'> Store is ",show_result()[,"open"], "! </font></p>",
        "<p><font size='5'>",show_result()[,"rating"],"&nbsp;<img src='https://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/256/Actions-rating-icon.png' alt='John' width='40' height='40'></font></p>",
        "</div>"
        
      )
      )
      
      return(outcome_text)
    
    }
    )
}

shinyApp(server = server, ui = ui)

I think your application might work on the first button press by accident.
actionBttns dont return TRUE/FALSE (which you test for), they return the number of times they have been pressed. by accident, when they have been pressed once, this 'equals' TRUE in R.
see

0:2 == TRUE
[1] FALSE  TRUE FALSE

if show_result is not defined within the eventReactive, then the return statement can not be performed. Hence the error.

Omg, I removed the if statement and it is showing output as intended now. Thanks a lot! Need to learn more about it.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.