有没有更好的方法在 R 中编码这个 sqlQuery?

发布于 2024-08-23 02:46:50 字数 1225 浏览 9 评论 0原文

我正在编写一个 R 脚本来获取一些数据库数据,然后使用 RODBC 包对其进行处理。目前我所有的 sqlQuery 命令都是一长串;

stsample<-sqlQuery(odcon, paste"select * from bob.DESIGNSAMPLE T1, bob.DESIGNSUBJECTGROUP T2, bob.DESIGNEVENT T3, bob.CONFIGSAMPLETYPES T4 WHERE T1.SUBJECTGROUPID = T2.SUBJECTGROUPID AND T1.TREATMENTEVENTID = T3.TREATMENTEVENTID AND T1.SAMPLETYPEKEY = T4.SAMPLETYPEKEY AND T1.STUDYID = T2.STUDYID AND T1.STUDYID = T3.STUDYID AND T1.STUDYID = ", chstudid, sep=""))
head(stsample)

它看起来很丑并且很难阅读/更新。我尝试过将它们放在多行中,但是新行字符会妨碍,目前我最好的方法是使用大量粘贴;

stsample<-sqlQuery(odcon,
    paste(
        "select ",
            "* ", 
        "from ", 
            "BOB.DESIGNSAMPLE T1, ",
            "BOB.DESIGNSUBJECTGROUP T2, ",
            "BOB.DESIGNEVENT T3, ",
            "BOB.CONFIGSAMPLETYPES T4 ",
        "WHERE ",
            "T1.SUBJECTGROUPID = T2.SUBJECTGROUPID ",
            "AND T1.TREATMENTEVENTID = T3.TREATMENTEVENTID ",
            "AND T1.SAMPLETYPEKEY = T4.SAMPLETYPEKEY ",
            "AND T1.STUDYID = T2.STUDYID ",
            "AND T1.STUDYID = T3.STUDYID ",
            "AND T1.STUDYID = ",chstudid,
        sep="")
    )
head(stsample)

但我不喜欢必须在每一行周围加上引号,并让我的空格正确。有更好的办法吗?

I'm writing an R script to get some database data and then do stuff with it, using the RODBC package. Currently all my sqlQuery commands are one long string;

stsample<-sqlQuery(odcon, paste"select * from bob.DESIGNSAMPLE T1, bob.DESIGNSUBJECTGROUP T2, bob.DESIGNEVENT T3, bob.CONFIGSAMPLETYPES T4 WHERE T1.SUBJECTGROUPID = T2.SUBJECTGROUPID AND T1.TREATMENTEVENTID = T3.TREATMENTEVENTID AND T1.SAMPLETYPEKEY = T4.SAMPLETYPEKEY AND T1.STUDYID = T2.STUDYID AND T1.STUDYID = T3.STUDYID AND T1.STUDYID = ", chstudid, sep=""))
head(stsample)

which looks ugly and is hard to read/update. I've tried putting them multiline, but then new line characters get in the way, currently my best is this using lots of paste's;

stsample<-sqlQuery(odcon,
    paste(
        "select ",
            "* ", 
        "from ", 
            "BOB.DESIGNSAMPLE T1, ",
            "BOB.DESIGNSUBJECTGROUP T2, ",
            "BOB.DESIGNEVENT T3, ",
            "BOB.CONFIGSAMPLETYPES T4 ",
        "WHERE ",
            "T1.SUBJECTGROUPID = T2.SUBJECTGROUPID ",
            "AND T1.TREATMENTEVENTID = T3.TREATMENTEVENTID ",
            "AND T1.SAMPLETYPEKEY = T4.SAMPLETYPEKEY ",
            "AND T1.STUDYID = T2.STUDYID ",
            "AND T1.STUDYID = T3.STUDYID ",
            "AND T1.STUDYID = ",chstudid,
        sep="")
    )
head(stsample)

But I don't like having to put quotes around everyline, and getting my whitespace correct. Is there a better way ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里泪两行 2024-08-30 02:46:50

我会使用这样的东西:

stsample<-sqlQuery(odcon,
    paste("
####DATASET CONSTRUCTION QUERY #########
    select 
    *  
    from 
    BOB.DESIGNSAMPLE T1, 
    BOB.DESIGNSUBJECTGROUP T2, 
    BOB.DESIGNEVENT T3, 
    BOB.CONFIGSAMPLETYPES T4 
    WHERE 
    T1.SUBJECTGROUPID = T2.SUBJECTGROUPID 
    AND T1.TREATMENTEVENTID = T3.TREATMENTEVENTID 
    AND T1.SAMPLETYPEKEY = T4.SAMPLETYPEKEY 
    AND T1.STUDYID = T2.STUDYID 
    AND T1.STUDYID = T3.STUDYID 
    AND T1.STUDYID = 
###################################   
    ", as.character(chstudid), sep="")
    )

I would use something like this:

stsample<-sqlQuery(odcon,
    paste("
####DATASET CONSTRUCTION QUERY #########
    select 
    *  
    from 
    BOB.DESIGNSAMPLE T1, 
    BOB.DESIGNSUBJECTGROUP T2, 
    BOB.DESIGNEVENT T3, 
    BOB.CONFIGSAMPLETYPES T4 
    WHERE 
    T1.SUBJECTGROUPID = T2.SUBJECTGROUPID 
    AND T1.TREATMENTEVENTID = T3.TREATMENTEVENTID 
    AND T1.SAMPLETYPEKEY = T4.SAMPLETYPEKEY 
    AND T1.STUDYID = T2.STUDYID 
    AND T1.STUDYID = T3.STUDYID 
    AND T1.STUDYID = 
###################################   
    ", as.character(chstudid), sep="")
    )
柠檬心 2024-08-30 02:46:50

使用 gsub("\n", " ", "long multiline select string") 而不是粘贴怎么样?

What about using gsub("\n", " ", "long multiline select string") instead of paste?

提笔落墨 2024-08-30 02:46:50

这是一个非常古老的问题,但认为这可能对某人有用。

我发现有用的一件事是 GetoptLong 包,它提供 qq() 函数。我认为它是受到 Perl 的启发,但本质上它提供了一种通过简单的变量插值来处理多行字符串的方法。例如:

library(GetoptLong)

tableName <- "myTable"
id <- 42

sqlQuery(odcon, qq("
    SELECT * FROM @{tableName}
    WHERE id = @{id}
    LIMIT 1
")

显然,我应该提到通常的警告,如果您直接处理用户输入,这是一个坏主意,在这种情况下最好使用某种准备好的语句。

This is a really old question, but thought this may be useful to someone.

One thing I have found useful is the GetoptLong package, which provides the qq() function. I think it is inspired by Perl, but essentially it provides a way to do a multiline string with easy variable interpolation. For example:

library(GetoptLong)

tableName <- "myTable"
id <- 42

sqlQuery(odcon, qq("
    SELECT * FROM @{tableName}
    WHERE id = @{id}
    LIMIT 1
")

Obviously I should mention the usual caveat that this is a bad idea if you are working directly with user input and it would be better to use some kind of prepared statement in that case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文