2

可能是一个简单的问题,我已经查看了许多选项,scan但还没有得到我想要的。

一个简单的例子是

require(httr)
example <- content(GET("http://www.r-project.org"), as = 'text')
write(example, 'text.txt')
input <- readLines('text.txt')

> example
[1] "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n<html>\n<head>\n<title>The R Project for Statistical Computing</title>\n<link rel=\"icon\" href=\"favicon.ico\" type=\"image/x-icon\">\n<link rel=\"shortcut icon\" href=\"favicon.ico\" type=\"image/x-icon\">\n<link rel=\"stylesheet\" type=\"text/css\" href=\"R.css\">\n</head>\n\n<FRAMESET cols=\"1*, 4*\" border=0>\n<FRAMESET rows=\"120, 1*\">\n<FRAME src=\"logo.html\" name=\"logo\" frameborder=0>\n<FRAME src=\"navbar.html\" name=\"contents\" frameborder=0>\n</FRAMESET>\n<FRAME src=\"main.shtml\" name=\"banner\" frameborder=0>\n<noframes>\n<h1>The R Project for Statistical Computing</h1>\n\nYour browser seems not to support frames,\nhere is the <A href=\"navbar.html\">contents page</A> of the R Project's\nwebsite.\n</noframes>\n</FRAMESET>\n\n\n\n"

input
 [1] "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"       
 [2] "<html>"                                                                  
 [3] "<head>"                                                                  
 [4] "<title>The R Project for Statistical Computing</title>"                  
 [5] "<link rel=\"icon\" href=\"favicon.ico\" type=\"image/x-icon\">"          
 [6] "<link rel=\"shortcut icon\" href=\"favicon.ico\" type=\"image/x-icon\">" 
 [7] "<link rel=\"stylesheet\" type=\"text/css\" href=\"R.css\">"              
 [8] "</head>"                                                                 
 [9] ""                                                                        
[10] "<FRAMESET cols=\"1*, 4*\" border=0>"                                     
[11] "<FRAMESET rows=\"120, 1*\">"                                             
[12] "<FRAME src=\"logo.html\" name=\"logo\" frameborder=0>"                   
[13] "<FRAME src=\"navbar.html\" name=\"contents\" frameborder=0>"             
[14] "</FRAMESET>"                                                             
[15] "<FRAME src=\"main.shtml\" name=\"banner\" frameborder=0>"                
[16] "<noframes>"                                                              
[17] "<h1>The R Project for Statistical Computing</h1>"                        
[18] ""                                                                        
[19] "Your browser seems not to support frames,"                               
[20] "here is the <A href=\"navbar.html\">contents page</A> of the R Project's"
[21] "website."                                                                
[22] "</noframes>"                                                             
[23] "</FRAMESET>"                                                             
[24] ""                                                                        
[25] ""                                                                        
[26] ""                                                                        
[27] ""     

这样做的动机是我想在 Postgresql 中存储各种文件,并且我exampleinput. 抱歉,如果我没有很好地解释。

@Hong Ooi 使用 readChar 给出了一个很好的答案。我有编码问题,所以不得不换行

iconv(readChar(file, nchars=file.info(file)["size"], TRUE), from = "latin1", to = "UTF-8")

停止数据库抱怨。

4

1 回答 1

4

如果您希望将所有这些字符串连接成一个字符串:

paste(input, collapse="\n")

或者,如果您正在从文件中读取并希望避免将输入拆分为位并将它们重新组合在一起:

f <- readChar(file, nchars=file.info(file)["size"], TRUE)
于 2013-08-01T04:22:55.317 回答