在R中,如何将多行文本文件(包含SQL)的内容导入单个字符串?
sql.txt文件如下所示:
SELECT TOP 100
setpoint,
tph
FROM rates
我需要将该文本文件导入R字符串,使其如下所示:
> sqlString
[1] "SELECT TOP 100 setpoint, tph FROM rates"
这样我可以像这样把它送到RODBC
> library(RODBC)
> myconn<-odbcConnect("RPM")
> results<-sqlQuery(myconn,sqlString)
我已经尝试了如下的readLines命令,但它没有提供RODBC所需的字符串格式。
> filecon<-file("sql.txt","r")
> sqlString<-readLines(filecon, warn=FALSE)
> sqlString
[1] "SELECT TOP 100 " "\t[Reclaim Setpoint Mean (tph)] as setpoint, "
[3] "\t[Reclaim Rate Mean (tph)] as tphmean " "FROM [Dampier_RC1P].[dbo].[Rates]"
>
答案 0 :(得分:17)
多功能paste()
命令可以使用参数collapse=""
执行此操作:
lines <- readLines("/tmp/sql.txt")
lines
[1] "SELECT TOP 100 " " setpoint, " " tph " "FROM rates"
sqlcmd <- paste(lines, collapse="")
sqlcmd
[1] "SELECT TOP 100 setpoint, tph FROM rates"
答案 1 :(得分:8)
下面是一个R函数,它读入多行SQL查询(来自文本文件)并将其转换为单行字符串。该功能删除格式和整行注释。
要使用它,请运行代码来定义函数,单行字符串将是运行的结果 ONELINEQ(&#34; querytextfile.sql&#34;&#34;〜/路径/到/ thefile&#34)。
工作原理:内联评论详细说明了这一点;它读取查询的每一行并删除(无需替换)写出单行版本查询所需的任何内容(如问题中所要求的)。结果是一个行列表,其中一些是空白的并被过滤掉;最后一步是将此(未列出)列表粘贴在一起并返回单行。
## This set of functions allows us to read in formatted, commented SQL queries
# Comments must be entire-line comments, not on same line as SQL code, and begun with "--"
# The parsing function, to be applied to each line:
LINECLEAN <- function(x) {
x = gsub("\t+", "", x, perl=TRUE); # remove all tabs
x = gsub("^\\s+", "", x, perl=TRUE); # remove leading whitespace
x = gsub("\\s+$", "", x, perl=TRUE); # remove trailing whitespace
x = gsub("[ ]+", " ", x, perl=TRUE); # collapse multiple spaces to a single space
x = gsub("^[--]+.*$", "", x, perl=TRUE); # destroy any comments
return(x)
}
# PRETTYQUERY is the filename of your formatted query in quotes, eg "myquery.sql"
# DIRPATH is the path to that file, eg "~/Documents/queries"
ONELINEQ <- function(PRETTYQUERY,DIRPATH) {
A <- readLines(paste0(DIRPATH,"/",PRETTYQUERY)) # read in the query to a list of lines
B <- lapply(A,LINECLEAN) # process each line
C <- Filter(function(x) x != "",B) # remove blank and/or comment lines
D <- paste(unlist(C),collapse=" ") # paste lines together into one-line string, spaces between.
return(D)
}
# TODO: add eof newline automatically to remove warning
#############################################################################################
答案 2 :(得分:4)
这是我正在使用的最终版本。谢谢Dirk。
fileconn<-file("sql.txt","r")
sqlString<-readLines(fileconn)
sqlString<-paste(sqlString,collapse="")
gsub("\t","", sqlString)
library(RODBC)
sqlconn<-odbcConnect("RPM")
results<-sqlQuery(sqlconn,sqlString)
library(qcc)
tph <- qcc(results$tphmean[1:50], type="xbar.one", ylim=c(4000,12000), std.dev=600)
close(fileconn)
close(sqlconn)
答案 3 :(得分:2)
这就是我使用的:
# Set Filename
fileName <- 'Input File.txt'
doSub <- function(src, dest_var_name, src_pattern, dest_pattern) {
assign(
x = dest_var_name
, value = gsub(
pattern = src_pattern
, replacement = dest_pattern
, x = src
)
, envir = .GlobalEnv
)
}
# Read File Contents
original_text <- readChar(fileName, file.info(fileName)$size)
# Convert to UNIX line ending for ease of use
doSub(src = original_text, dest_var_name = 'unix_text', src_pattern = '\r\n', dest_pattern = '\n')
# Remove Block Comments
doSub(src = unix_text, dest_var_name = 'wo_bc_text', src_pattern = '/\\*.*?\\*/', dest_pattern = '')
# Remove Line Comments
doSub(src = wo_bc_text, dest_var_name = 'wo_bc_lc_text', src_pattern = '--.*?\n', dest_pattern = '')
# Remove Line Endings to get Flat Text
doSub(src = wo_bc_lc_text, dest_var_name = 'flat_text', src_pattern = '\n', dest_pattern = ' ')
# Remove Contiguous Spaces
doSub(src = flat_text, dest_var_name = 'clean_flat_text', src_pattern = ' +', dest_pattern = ' ')
答案 4 :(得分:1)
尝试paste(sqlString, collapse=" ")
答案 5 :(得分:1)
可以使用readChar()
代替readLines()
。我一直存在混合评论问题(--
或/* */
),这对我来说一直很好。
sql <- readChar(path.to.file, file.size(path.to.file))
query <- sqlQuery(con, sql, stringsAsFactors = TRUE)
答案 6 :(得分:0)
我一起使用sql <- gsub("\n","",sql)
和sql <- gsub("\t","",sql)
。