Shiny R值,dplyr过滤器错误?

Shiny R值,dplyr过滤器错误?,shiny,shinydashboard,shiny-server,shiny-reactivity,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')" St

当我在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')"
我发现了两个问题

第一个被动语句是函数-您需要在它们后面添加括号
()
。 其次,您需要对变量进行命名,尤其是在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."))
    }
  })


} 
server%
#选择(生活成本指数)
# })
输出$p2我发现了两个问题

第一个被动语句是函数-您需要在它们后面添加括号
()
。 其次,您需要对变量进行命名,尤其是在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."))
    }
  })


} 
server%
#选择(生活成本指数)
# })

输出$p2哦,非常感谢您的输入。在命名变量时,我必须更加注意。非常感谢。哦,非常感谢你的投入。在命名变量时,我必须更加注意。非常感谢。