Hack-R
Hack-R

Reputation: 23221

R: In Shiny how do I fix no applicable method for 'xtable' applied to an object of class "reactive"

I'm getting this error:

Error in UseMethod("xtable") : 
  no applicable method for 'xtable' applied to an object of class "reactive"

UI.R

library(shiny)
shinyUI(pageWithSidebar(
  headerPanel("Test App"),
  sidebarPanel(
    textInput(inputId="text1", 
              label = "Enter Keywords"),
    actionButton("goButton", label = "Go!", icon = "search")
  ),
  mainPanel(
    p('Your search:'),
    textOutput('text1'),
    p(''),
    textOutput('text3'),
    p('Search Results'),
    tableOutput('searchResult')
  )
))

Server.R

library(shiny)

data <- read.csv("./data/data.csv", quote = "")

shinyServer(
  function(input, output) {
    searchResult<- reactive({
      subset(asos, grepl(input$text1, asos$Title))
    })

    output$text1 <- renderText({input$text1})
    output$text3 <- renderText({      
      if (input$goButton == 0) "Get your search on!"
      else if (input$goButton == 1) "Computing... here's what I found!"
      else "OK, I updated the results!"
    })
    output$searchResult <- renderTable({ 
      searchResult
    })
  }
)

Upvotes: 4

Views: 7303

Answers (1)

jdharrison
jdharrison

Reputation: 30445

reactive returns a function. To call the reactive function you would use:

output$searchResult <- renderTable({ 
  searchResult()
})

Upvotes: 9

Related Questions