【问题标题】:Call R Dataframe in SQL在 SQL 中调用 R 数据框
【发布时间】:2019-03-26 22:04:29
【问题描述】:

有没有办法在 SQL 中调用 R 数据框?例如,我想运行类似:

SELECT user_id, other_variables FROM table1 WHERE user_id IN ('R DATAFRAME')

其中 R 数据框是一些用户 ID 的简单列表,可以在 table1 中找到。

我只是想知道这样的事情是否可能,用 R 或 SQL 编写,如果可以,怎么做?我知道我可以将 R 数据框上传到数据库,但我无权在数据库中创建自己的表。任何帮助将不胜感激,谢谢。

【问题讨论】:

    标签: sql r database dataframe


    【解决方案1】:

    除非使用 spark 环境或使用 R 包 sqldf,否则您不能。

    如果只是一个 id 列表,您可以使用 unlist()

    sqlQuery (ch,paste0(" 
      select * from my_table 
      where id in (",
      paste(unlist(my_df_Fktable$id), 
      collapse=','),
      ")"
    ))
    

    【讨论】:

    • 这太棒了,非常感谢。我使用 DBI 包中的 dbGetQuery 应用了这个逻辑,它运行良好。
    • :) 你也可以使用来自glue 包的glue_sql()DBI::dbGetQuery(ch,glue_sql("select * from my_table where id in ({id*}); ",id=my_df_Fktable$id))。我考虑了这一点,因为我更改了查询中的一些内容。
    【解决方案2】:

    这是一个几乎使用数据帧的答案,一行一行。

    library(dplyr)
    library(RSQLite)
    library(DBI)
    library(glue)
    # Only for data example
    con <- dbConnect(RSQLite::SQLite(), ":memory:")
    T_all<- data.frame(a=1:3,b=c("a","b","c"))
    T_where <- data.frame(a=1:3,b=c("a","b","d"))
    DBI::dbWriteTable(con, "T_all", T_all)
    DBI::dbWriteTable(con, "T_where", T_where)
    
    # The function of the answer
    glue_sql.multi.fct <-function (sql_multi_vars, args,connexion_bdd=DBI::ANSI()) {
      args<-unlist(list(sql_multi_vars,args,.con =connexion_bdd), recursive = FALSE ) 
      unname(names(args)[1])# for 1st arg of glue_sql()
      do.call(glue_sql,args )
    }
    
    # All datas where I find 
    dfall<-DBI::dbGetQuery(con,"select * from T_All")
    print("dfall: All datas where I find ")
    print(dfall)
    
    dfw<-DBI::dbGetQuery(con,"select * from T_Where")
    
    sql1="select * from T_all where a = {a} and b={b}"
    
    
    sql_binded<-glue_sql.multi.fct(sql1,list(a=1,b="a"))
    print("sql_binded")
    print(sql_binded)
    dfall_filtered1<-DBI::dbGetQuery(con,sql_binded)
    print("dfall_filtered1: datas dfall filtered by a list (normal use of glue_sql()")
    print(dfall_filtered1)
    
    # loop for the datas dfall filtered by dfw
    dfall_filtered2<-  data.frame()   
    # the loop is mandatory for SQL Server, it doesn't work like example of multi line of DBI::dbGetQuery documentation.
    for (sqlcurrent in glue_sql.multi.fct(sql1,dfw))  {
      dfall_filtered2<-rbind(dfall_filtered2, DBI::dbGetQuery(con,sqlcurrent))
      print(sqlcurrent)
    }
    
    # results of  the datas dfall filtered by dfw
    print("dfall_filtered2: results of  the datas dfall filtered by dfw")
    print(dfall_filtered2)
    
    dbDisconnect(con)
    

    输出

    [1] "dfall: All datas where I find "
      a b
    1 1 a
    2 2 b
    3 3 c
    [1] "sql_binded"
    <SQL> select * from T_all where a = 1 and b='a'
    [1] "dfall_filtered1: datas dfall filtered by a list (normal use of glue_sql()"
      a b
    1 1 a
    [1] "select * from T_all where a = 1 and b='a'"
    [1] "select * from T_all where a = 2 and b='b'"
    [1] "select * from T_all where a = 3 and b='d'"
    [1] "dfall_filtered2: results of  the datas dfall filtered by dfw"
      a b
    1 1 a
    2 2 b
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-27
      • 2014-02-14
      • 2016-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多