R:在 Shiny 中,我如何修复应用于“反应式”类对象的“xtable”没有适用的方法

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

提问人:Hack-R 提问时间:12/29/2014 更新时间:12/29/2014 访问量:7049

问:

我收到此错误:

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

用户界面。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')
  )
))

服务器.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
    })
  }
)
r 闪亮

评论


答:

9赞 jdharrison 12/29/2014 #1

reactive返回一个函数。要调用响应式函数,您可以使用:

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