如何在R中执行SQL脚本时使用动态值

时间:2017-04-13 06:33:43

标签: r rpostgresql r-dbi

我的R工作流程现在涉及处理大量查询(RPostgreSQL库)。我真的希望将来能够轻松维护和管理代码。

我开始从单独的.SQL文件中加载大型查询(this帮助)并且效果很好。

然后我开始使用插值(that帮助),这意味着我可以写

SELECT * FROM table WHERE value = ?my_value;

和(在将其加载到R之后)使用sqlInterpolate(ANSI(), query, value = "stackoverflow")插入它。

现在发生的事情是我想使用这样的东西

SELECT count(*) FROM ?my_table;

但我怎样才能让它发挥作用? sqlInterpolate()默认情况下仅安全插值。有解决方法吗?

由于

2 个答案:

答案 0 :(得分:0)

sqlInterpolate()仅用于替换值,而不是替换表名等其他组件。您可以使用其他模板框架,例如brewwhisker

答案 1 :(得分:0)

?DBI::SQL中,您可以阅读:

  

默认情况下,任何用户提供的查询输入都应使用转义进行转义   dbQuoteIdentifier()dbQuoteString()取决于是否为dbQuoteIdentifier()sqlInterpolate(ANSI(), "SELECT count(*) FROM ?my_table", my_table = dbQuoteIdentifier(ANSI(), "table_name")) # <SQL> SELECT count(*) FROM "table_name"   表示一个表或变量名,或者是一个文字字符串。

此外,在this page上:

  

如果您正在创建表格,您可能还需要get_ad_links = function(page){ require(rvest) # construct url to page (!when running the code put the url in 1 line!) url_base = "https://www.leboncoin.fr/ventes_immobilieres/offres/ languedoc_roussillon/pyrenees_orientales" url = paste(url_base, "?o=", page,"&ret=1&ret=2&f=p", sep = "") page = read_html(url) # extract links to ads on page a="//*/section/section/ul/li[" b="]/a/@href" t =lapply(1:30, function(i) paste(a,i,b, sep = "")) ad_links = sapply(1:30,function(i) { page %>% html_node(xpath=as.character(t[i])) %>% html_text()}) return(ad_links) } # Function to Get Ad Details by Ad URL get_ad_details = function(ad_url){ require(rvest) # parse ad url to html tree doc = read_html(paste("https:",ad_url,sep="")) # extract labels and values using xpath expression pattern<- "</?\\w+((\\s+\\w+(\\s*m\\s*(?:\".*? \"|'.*?'[^'\">\\s]+))?)+\\s*|\\s*)/?>" prix = doc %>% html_node(xpath="//section/section/section[2]/div[4]/h2/span[2]") %>% html_text() PRIX = stringr::str_replace_all(prix,pattern,"") PRIX =stringr::str_wrap(PRIX) ville = doc %>% html_node(xpath="//section/section/section[2]/div[5]/h2/span[2]") %>% html_text() VILLE = stringr::str_replace_all(ville,pattern,"") VILLE = stringr::str_wrap(VILLE) surface = doc %>% html_node(xpath="//section/section/section[2]/div[8]/h2/span[2]") %>% html_text() SURFACE = stringr::str_replace_all(surface,pattern,"") SURFACE = stringr::str_wrap(SURFACE) pieces = doc %>% html_node(xpath="//section/section/section[2]/div[7]/h2/span[2]") %>% html_text() PIECES = stringr::str_replace_all(pieces,pattern,"") PIECES = stringr::str_wrap(PIECES) type = doc %>% html_node(xpath="//section/section/section[2]/div[6]/h2/span[2]") %>% html_text() TYPE_BIEN = stringr::str_replace_all(type,pattern,"") TYPE_BIEN = stringr::str_wrap(TYPE_BIEN) ges = doc %>% html_node(xpath="//section/section/section[2]/div[9]/h2/span[2]") %>% html_text() GES = stringr::str_replace_all(ges,pattern,"") GES = stringr::str_wrap(GES) values = c(PRIX, VILLE,SURFACE,PIECES,TYPE_BIEN,GES) # convert to data frame and add labels mydf = as.data.frame(t(values)) names(mydf)= c("PRIX", "VILLE","SURFACE","PIECES" ,"TYPE_BIEN","GES") return(mydf) } ad_links = get_ad_links(page = 1) # grab ad details for first 30 links from page 1 require(plyr) ad_details = ldply(ad_links[1:30], get_ad_details, .progress = 'text')   依靠用户输入来选择要过滤的列。

所以你可以使用:

/Api