Read file into R keeping end of lines -
probably simple question , have looked @ many options in scan havent got want yet.
a simple example be
require(httr) example <- content(get("http://www.r-project.org"), = '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 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 statistical computing</h1>\n\nyour browser seems not support frames,\nhere <a href=\"navbar.html\">contents page</a> of 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 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 statistical computing</h1>" [18] "" [19] "your browser seems not support frames," [20] "here <a href=\"navbar.html\">contents page</a> of r project's" [21] "website." [22] "</noframes>" [23] "</frameset>" [24] "" [25] "" [26] "" [27] "" the motivation want store various files in postgresql , passing them in in format given example opposed input. apologies if havent explained well.
@hong ooi gave nice answer using readchar. have encoding issues have had wrap
iconv(readchar(file, nchars=file.info(file)["size"], true), = "latin1", = "utf-8") to stop database complaining.
if want strings concatenated single string:
paste(input, collapse="\n") alternatively, if you're reading file , want avoid splitting input bits , putting them together:
f <- readchar(file, nchars=file.info(file)["size"], true)
Comments
Post a Comment