17

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]"           
> 
4

7 回答 7

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"
于 2010-01-05T01:55:08.713 回答
9

下面是一个 R 函数,它读取多行 SQL 查询(来自文本文件)并将其转换为单行字符串。该功能删除格式和整行注释。

要使用它,请运行代码来定义函数,您的单行字符串将是运行 ONELINEQ("querytextfile.sql","~/path/to/thefile") 的结果。

它是如何工作的:内联注释详细说明了这一点;它读取查询的每一行并删除(替换为空)写出查询的单行版本(如问题中所要求的)不需要的任何内容。结果是一个行列表,其中一些是空白的并被过滤掉;最后一步是将这个(未列出的)列表粘贴在一起并返回单行。

#
# 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
#############################################################################################
于 2015-05-30T22:21:49.190 回答
4

这是我正在使用的最终版本。谢谢德克。

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)
于 2010-01-05T02:43:32.267 回答
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 = ' ')
于 2015-11-30T15:31:06.680 回答
1

尝试paste(sqlString, collapse=" ")

于 2010-01-05T01:53:16.610 回答
1

可以使用readChar()而不是readLines(). 我一直存在混合评论(--/* */)的问题,这对我来说一直很有效。

sql <- readChar(path.to.file, file.size(path.to.file))
query <- sqlQuery(con, sql, stringsAsFactors = TRUE)
于 2017-05-03T14:06:14.183 回答
0

我用sql <- gsub("\n","",sql)sql <- gsub("\t","",sql)在一起。

于 2010-01-05T03:24:57.970 回答