shiny - R Shiny Reactive 值,dplyr 过滤器错误?
问题描述
当我在 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')"
解决方案
我发现了两个问题,
第一个反应语句是函数 - 您需要()
在它们之后添加括号。其次,您需要处理变量的命名,尤其data
是在 R 中命名变量从来都不是一件好事,并且您首先将两个对象命名为相同的数据集本身,其次是返回数据集的反应性语句 - 看起来像这样迷糊闪亮不少。我将响应式语句重命名为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."))
}
})
}
希望这可以帮助!!
推荐阅读
- flutter - 在没有用户能够更改的情况下,有什么可靠的方法可以让时间颤抖?
- python - 使用 pybind11 在多线程 C++ 程序中嵌入 Python 解释器
- mysql - 比较两个没有主键的表
- python - 无论出于何种原因,我的代码都没有运行一个段,有人可以帮助我吗?
- reactjs - 无法使用柏树在反应滑块上移动手柄
- javascript - 是否可以根据我当前所在的视图更改 Vue.js 应用程序的元数据,例如标题?
- java - JPA OneToMany - 级联对象和垃圾收集器的迭代
- javascript - 如何使用 Alpine.js 创建动画数字计数器?
- reactjs - 如何在选择输入反应管理员中设置值?
- r - r:为什么在应用 as.Date() 和 origin 时字符串显示日期而不是日期?