【问题标题】:R Filtering results using text input from a select box or selectInputR使用来自选择框或selectInput的文本输入过滤结果
【发布时间】:2014-07-22 20:45:21
【问题描述】:

我正在闪亮的画廊学习/播放/修改温斯顿伟大的电影探索者。

这是他的 server.ui 代码。 https://github.com/wch/movies/blob/master/server.R

在他的示例中,他对数字电影数据使用了简单的数字滑块范围过滤。

接着是文本过滤,这就是我遇到错误的地方。

他用的是文本输入,(textInput http://shiny.rstudio.com/gallery/widgets-gallery.html)

我尝试从带有选择框的列表中过滤(可能是多个)文本值时遇到错误,(selectInput http://shiny.rstudio.com/gallery/widgets-gallery.html)。

# Optional: filter by director
if (!is.null(input$director) && input$director != "") {
director <- paste0("%", input$director, "%")
m <- m %>% filter(Director %like% director)
}

尝试以相同的方式实现他的导演过滤器代码,除了通过选择框(而不是 textInput)提供“导演”之外,RStudio 响应...

Error in filter_impl(.data, dots(...), environment()) : 
could not find function "%like%"

经过大量挖掘,我相信虽然没有提到,但我可能还需要安装 library(data.table)。

安装并加载data.table后,错误现在变为

Warning in min(x) : no non-missing arguments to min; returning Inf
Warning in max(x) : no non-missing arguments to max; returning -Inf
Warning in min(x) : no non-missing arguments to min; returning Inf
Warning in max(x) : no non-missing arguments to max; returning -Inf
Error in fdata[1, 1] : incorrect number of dimensions

我相信警告(不是错误)指的是 ggvis,

他的server.r代码第61行出现fdata错误

m

这就是我发现自己完全不知道如何进行的地方。

解决方案要么包括一种可以说是优越的过滤方法,要么帮助我把轮子放回这个例子的马车上。

感谢您的宝贵时间。

天哪! 我被快速的反应惊呆了,他们在完整的问题被清楚地输入之前就做出了反应。 我正在导入 CSV,而不是使用 SQL 绑定。 所有 4 个库都已加载。

【问题讨论】:

  • 他一开始提到的四个库你都加载了吗?
  • 您是否尝试更改代码中的任何内容?在dplyr 中有对%like% 的引用,但它似乎只在translate_sql 的上下文中工作(即translate_sql(first %like% "Had*"))。您不再使用 SQL 后端了吗?据我所知,没有名为%like% 的独立运算符。
  • @MrFlick, %like% 看起来非常像 SQL 语法我认为它必须在最后两个包中,但懒得检查
  • 我所知道的唯一其他具有%like% 功能的软件包是data.table - 但我很想知道server.R 文件的作者是如何逃脱的使用那个包。我确实检查了该文件中加载的所有包,但没有找到%like%
  • 那么到底为什么这个问题被否决了,哈哈

标签: r filter rstudio shiny


【解决方案1】:

正如我所想,这似乎是特定于使用 RSQLite 的。这是一个例子

library(dplyr)
library(RSQLite)

#sample data    
dd<-data.frame(name=letters[1:5], age=21:25)

dd %>% filter(age==25)
#   name age
# 1    e  25

dd %>% filter(name %like% a)
# Error in filter_impl(.data, dots(...), environment()) : 
#   could not find function "%like%"

但是现在让我们创建一个测试 sqlite 数据库。

#sample sqlite database
sqlite    <- dbDriver("SQLite")
exampledb <- dbConnect(sqlite,"hello.db")
dbWriteTable(exampledb, "people", dd)
sqliteCloseConnection(exampledb)
sqliteCloseDriver(sqlite)

现在我们将使用dplyr 再次过滤

db <- src_sqlite("hello.db", create=F)
nm <- tbl(db, "people")

nm %>% filter(age==25)
#   row_names name age
# 1         5    e  25

nm %>% filter(name %like% a)
#   row_names name age
# 1         1    a  21

而且它有效。 Do %like% 不是正确的 R 中缀运算符。它只是一个在使用 SQL 连接到适当的 SQL 命令时被转义和翻译的命令。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-19
    • 2021-01-22
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    • 2017-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多