R -RMySQL-如何将更多的sql查询保存到文件中?

时间:2018-04-02 15:30:06

标签: r rmysql

我在R中做了一些数据分析。在脚本结束时,我想将结果保存到文件中。我知道有更多的选择如何做,但他们不能正常工作。当我尝试sink()它有效但它给了我:

<MySQLResult:1,5,1>
         host logname user                time                                                                       request_fline status
1 142.4.5.115      -    -  2018-01-03 12:08:58  GET /phpmyadmin?</script><script>alert('<!--VAIBS-->');</script><script> HTTP/1.1     400
                                                                size_varchar referer agent     ip_adress size_int cookie time_microsec filename
1  Mozilla/5.0 (Windows NT 10.0; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0    445      -  142.4.5.115       445     -            159        -
  request_protocol keepalive request_method contents_of_foobar contents_of_notefoobar port child_id
<MySQLResult:1,5,1>
 [1] host                   logname                user                   time                   request_fline          status                
 [7] size_varchar           referer                agent                  ip_adress              size_int               cookie                     
<0 rows> (or 0-length row.names)

这完全无法使用,因为我无法导出该类型的数据。如果我尝试write.table,它会给出一行可能会读取的文件,但是在一行之后R skript并给我错误:Error in isOpen(file, "w") : invalid connection当我尝试write.csv时结果是相同的。当我尝试lapply时,它只给我空文件。

有我的代码:

fileConn<-file("outputX.txt")
fileCon2<-file("outputX.csv")
sink("outputQuery.txt")
for (i in 1:length(awq)){
  sql <- paste("SELECT * FROM mtable ORDER BY cookie LIMIT ", awq[i], ",1")
  nb <- dbGetQuery(mydb, sql)
  print (nb)
  write.table(nb, file = fileConn, append = TRUE, quote = FALSE, sep = " ", eol = "\n", na = "NA", row.names = FALSE, col.names = FALSE)
  write.csv(nb, file = fileCon2,row.names=FALSE, sep="  ")
  lapply(nb, write, fileConn, append=TRUE, ncolumns=7)
  writeLines(unlist(lapply(nb, paste, collapse=" ")))
}
sink()
close(fileConn)
close(fileCon2)

我是R的新手,所以我不知道还应该尝试什么。我想要的是1个文件,其中数据将以易于阅读和导出的形式打印。比如说:

142.4.5.115  -   -  2018-01-03 12:08:58  GET /phpmyadmin?/><!--VAIBS--> HTTP/1.1  400  Mozilla/5.0 (Windows NT 10.0; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0  445  -  142.4.5.115  445  -  145 -  HTTP/1.1  0  GET   -   -  80 7216  ?/><!--VAIBS-->   GET /phpmyadmin?/><!--VAIBS--> HTTP/1.1   -  0  /phpmyadmin   -  354 0
142.4.5.115  -   -  2018-01-03 12:10:23  GET /phpmyadmin?/><!--VAIBS--> HTTP/1.1  400  Mozilla/5.0 (Windows NT 10.0; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0  445  -  142.4.5.115  445  -  145 -  HTTP/1.1  0  GET   -   -  80 7216  ?/><!--VAIBS-->   GET /phpmyadmin?/><!--VAIBS--> HTTP/1.1   -  0  /phpmyadmin   -  354 0
142.4.5.115  -   -  2018-01-03 12:12:41  GET /phpmyadmin?/><!--VAIBS--> HTTP/1.1  400  Mozilla/5.0 (Windows NT 10.0; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0  445  -  142.4.5.115  445  -  145 -  HTTP/1.1  0  GET   -   -  80 7216  ?/><!--VAIBS-->   GET /phpmyadmin?/><!--VAIBS--> HTTP/1.1   -  0  /phpmyadmin   -  354 0
142.4.5.115  -   -  2018-01-03 12:15:29  GET /phpmyadmin?/><!--VAIBS--> HTTP/1.1  400  Mozilla/5.0 (Windows NT 10.0; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0  445  -  142.4.5.115  445  -  145 -  HTTP/1.1  0  GET   -   -  80 7216  ?/><!--VAIBS-->   GET /phpmyadmin?/><!--VAIBS--> HTTP/1.1   -  0  /phpmyadmin   -  354 0

或者这个:

host,logname,user,time, request_fline status,size_varchar,referer agent,ip_adress,size_int,cookie,time_microsec,filename,request_protocol,keepalive,request_method,contents_of_foobar,contents_of_notefoobar port child_id
1 142.4.5.115  -   -  2018-01-03 12:08:58  GET /phpmyadmin?/><!--VAIBS--> HTTP/1.1  400  Mozilla/5.0 (Windows NT 10.0; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0  445  -  142.4.5.115  445  -  145 -  HTTP/1.1  0  GET   -   -  80 7216  ?/><!--VAIBS-->   GET /phpmyadmin?/><!--VAIBS--> HTTP/1.1   -  0  /phpmyadmin   -  354 0
2 142.4.5.115  -   -  2018-01-03 12:10:23  GET /phpmyadmin?/><!--VAIBS--> HTTP/1.1  400  Mozilla/5.0 (Windows NT 10.0; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0  445  -  142.4.5.115  445  -  145 -  HTTP/1.1  0  GET   -   -  80 7216  ?/><!--VAIBS-->   GET /phpmyadmin?/><!--VAIBS--> HTTP/1.1   -  0  /phpmyadmin   -  354 0
3 142.4.5.115  -   -  2018-01-03 12:12:41  GET /phpmyadmin?/><!--VAIBS--> HTTP/1.1  400  Mozilla/5.0 (Windows NT 10.0; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0  445  -  142.4.5.115  445  -  145 -  HTTP/1.1  0  GET   -   -  80 7216  ?/><!--VAIBS-->   GET /phpmyadmin?/><!--VAIBS--> HTTP/1.1   -  0  /phpmyadmin   -  354 0
4 142.4.5.115  -   -  2018-01-03 12:15:29  GET /phpmyadmin?/><!--VAIBS--> HTTP/1.1  400  Mozilla/5.0 (Windows NT 10.0; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0  445  -  142.4.5.115  445  -  145 -  HTTP/1.1  0  GET   -   -  80 7216  ?/><!--VAIBS-->   GET /phpmyadmin?/><!--VAIBS--> HTTP/1.1   -  0  /phpmyadmin   -  354 0

或类似的东西。最重要的是,将有一些帮助如何在循环中编写write.table而不会出错。但我会欢迎任何功能性解决方案。我最好的是:

sql <- paste("SELECT * FROM idsaccess ORDER BY cookie LIMIT ", awq[1], ",1")
nb <- dbGetQuery(mydb, sql)
write.table(nb, file = fileConn, append = TRUE, quote = FALSE, sep = " ", eol = "\n", na = "NA", row.names = FALSE, col.names = FALSE)
fileConn<-file("outputX1.txt")
sql <- paste("SELECT * FROM idsaccess ORDER BY cookie LIMIT ", awq[2], ",1")
nb <- dbGetQuery(mydb, sql)
write.table(nb, file = fileConn, append = true, quote = FALSE, sep = " ", eol = "\n", na = "NA", row.names = FALSE, col.names = FALSE)

但是这会给每个查询提供自己的文件。而且我不希望每个查询都在自己的文件中。有帮助吗?

1 个答案:

答案 0 :(得分:0)

简单地将所有查询数据帧连接到一个大型数据帧,因为它们都共享相同的结构,然后在一个调用中输出到文件,这实际上是使用{的典型方式{1}}或其包装器write.table

具体来说,转动write.csv循环:

for

进入for (i in 1:length(awq)){ sql <- paste("SELECT * FROM mtable ORDER BY cookie LIMIT ", awq[i], ",1") nb <- dbGetQuery(mydb, sql) } 获取数据框列表:

lapply

然后,行绑定df_list <- lapply(1:length(awq), function(i) { sql <- paste0("SELECT * FROM mtable ORDER BY cookie LIMIT ", awq[i], ",1") nb <- dbGetQuery(mydb, sql) }) 以将所有dfs堆叠到单个数据帧中并输出到文件:

do.call