How do you import a plain text file as single character string in R? I think that this will probably have a very simple answer but when I tried this today I found that I couldn't find a function to do this.
For example, suppose I have a file foo.txt
with something I want to textmine.
I tried it with:
scan("foo.txt", what="character", sep=NULL)
but this still returned a vector. I got it working somewhat with:
paste(scan("foo.txt", what="character", sep=" "),collapse=" ")
but that is quite an ugly solution which is probably unstable too.
This question is related to
r
How about:
string <- readChar("foo.txt",nchars=1e6)
The readr package has a function to do everything for you.
install.packages("readr") # you only need to do this one time on your system
library(readr)
mystring <- read_file("path/to/myfile.txt")
This replaces the version in the package stringr.
readChar doesn't have much flexibility so I combined your solutions (readLines and paste).
I have also added a space between each line:
con <- file("/Users/YourtextFile.txt", "r", blocking = FALSE)
singleString <- readLines(con) # empty
singleString <- paste(singleString, sep = " ", collapse = " ")
close(con)
I would use the following. It should work just fine, and doesn't seem ugly, at least to me:
singleString <- paste(readLines("foo.txt"), collapse=" ")
Too bad that Sharon's solution cannot be used anymore. I've added Josh O'Brien's solution with asieira's modification to my .Rprofile file:
read.text = function(pathname)
{
return (paste(readLines(pathname), collapse="\n"))
}
and use it like this: txt = read.text('path/to/my/file.txt')
. I couldn't replicate bumpkin's (28 oct. 14) finding, and writeLines(txt)
showed the contents of file.txt
. Also, after write(txt, '/tmp/out')
the command diff /tmp/out path/to/my/file.txt
reported no differences.
It seems your solution is not much ugly. You can use functions and make it proffesional like these ways
new.function <- function(filename){
readChar(filename, file.info(filename)$size)
}
new.function('foo.txt')
new.function <- function(){
filename <- 'foo.txt'
return (readChar(filename, file.info(filename)$size))
}
new.function()
In case anyone is still looking at this question 3 years later, Hadley Wickham's readr package has a handy read_file()
function that will do this for you.
# you only need to do this one time on your system
install.packages("readr")
library(readr)
mystring <- read_file("path/to/myfile.txt")
Source: Stackoverflow.com