代码之家  ›  专栏  ›  技术社区  ›  Knowledge Seeker

警告:结果创建错误:没有这样的列:tmp

  •  0
  • Knowledge Seeker  · 技术社区  · 6 年前

    我正在使用运行数据库查询 sqldf 在光彩夺目的R.

    用户界面:

      observeEvent (input$uploadForTest_1, {
        inFile=input$uploadForTest_1
        inFileName=input$uploadForTest_1$name
        file <-"tss.txt"
        tmp = paste("audio/street", inFileName, sep = "/")
        res <- read.csv.sql(file,header=FALSE,sql = "select * from file where V1=tmp",sep="\t")
        print(res)
      })
    

    我已成功运行以下查询:

    res <- read.csv.sql(file,header=FALSE,sql = "select * from file where V1='audio/street/b098.wav'",sep="\t")
    

    但是,如果我运行中提到的查询 ui.R 这给了我一个错误 tmp 列不存在:

    警告:结果创建错误:没有这样的列:tmp 86:

    我不想在我的查询中使用字符串。我想使用变量名。因为我不想在查询中硬编码字符串。我可以在查询中使用变量名而不是字符串吗?如果是,那我该怎么做呢?我在网上找不到解决问题的办法。谢谢。

    2 回复  |  直到 6 年前
        1
  •  2
  •   G. Grothendieck    6 年前

    序言 read.csv.sql 具有 fn$ 并使用 '$tmp' 在SQL语句中。

    fn$read.csv.sql(file, sql = "select * from file where V1 = '$tmp'", 
      header = FALSE, sep = "\t")
    

    ?fn 以及GSUBFN小插曲以了解更多信息。注意,sqldf会自动加载gsubfn包,这样它就已经可用了。

        2
  •  1
  •   Roman LuÅ¡trik    6 年前

    你可以使用 sprintf 。另一种选择是将一个字符串粘贴在一起,但我发现 把格式数据写成串 这项任务要优雅得多。

    > tmp <- "audio/street/somefile.txt"
    > tmp <- "audio/street/somefile.txt"
    > "select * from file where V1=tmp"
    [1] "select * from file where V1=tmp"
    
    > sprintf("select * from file where V1='%s'", tmp)
    [1] "select * from file where V1='audio/street/somefile.txt'"