R闪亮的反应性值,dplyr过滤器错误?

时间:2018-12-03 05:43:07

标签: shiny shinydashboard shiny-server shiny-reactivity

我正在尝试找出在UI中选择输入以立即反映页面结果的方法。 我的搜索结果使我研究了反应表达和反应值。但是,当我尝试过滤数据的值时,我认为这引起了一些麻烦,但我不知道该怎么办。

过滤器功能似乎不起作用。

这是错误消息:

Warning: Error in UseMethod: no applicable method for 'filter_' applied to an object of class "c('reactiveExpr', 'reactive')"
Stack trace (innermost first):
    51: filter_
    50: filter.default
    49: filter
    48: function_list[[i]]
    47: freduce
    46: _fseq
    45: eval
    44: eval
    43: withVisible
    42: %>%
    41: eval
    40: makeFunction
    39: exprToFunction
    38: observe
    37: server 
     1: runApp
Error in UseMethod("filter_") : 
  no applicable method for 'filter_' applied to an object of class "c('reactiveExpr', 'reactive')"

1 个答案:

答案 0 :(得分:1)

我发现了两个问题,

第一个反应式语句是函数-您需要在其后加上括号()。 其次,您需要对变量进行命名,特别是在R中命名变量data从来都不是一件好事,您要为两个对象命名相同,首先是数据集本身,然后是反应性语句返回数据集-看起来好像很困惑。我将反应式语句重命名为dta,这对我来说解决了。这是完整的服务器代码

server <- function(input, output, session) {

  dta <- reactive({
    data
  })

  output$p1 <- renderText({
    paste0("You currently live in ", input$Location, " and are contemplating a job offer in ", input$reLocation, ".")
  })

  values <- reactiveValues()
  observe({ 
    # req(input$Location,input$reLocation)
    # browser()
    values$LocationCost <-  dta() %>% filter(UrbanArea == input$Location) %>% select(CostOfLivingIndex)
    values$reLocationCost <-  dta() %>% filter(UrbanArea == input$reLocation) %>% select(CostOfLivingIndex)
  }) 
  # observeEvent(input$Location, {
  #   values$LocationCost <- data %>%
  #     filter(UrbanArea == input$Location) %>%
  #     select(CostOfLivingIndex)
  # })
  # 
  # observeEvent(input$reLocation, {
  #   values$reLocationCost <- data %>%
  #     filter(UrbanArea == input$reLocation) %>%
  #     select(CostOfLivingIndex)
  # })

  output$p2 <- renderText({
    if (values$LocationCost < values$reLocationCost) {
      calc <- round(100* ((values$reLocationCost-values$LocationCost)/values$LocationCost), 2)
      print(paste0("You need ", calc, "% increase in your after-taxes income in order to maintain your present lifestyle."))
    } else {
      calc <- round(100 * ((values$LocationCost-values$reLocationCost)/values$reLocationCost), 2)
      print(paste0("You can sustain upto ", calc, "% reduction in after taxes income without reducing your present lifestyle."))
    }
  })


} 

希望这会有所帮助!