Import text file as single character string

R

R Problem Overview


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.

R Solutions


Solution 1 - R

Here's a variant of the solution from @JoshuaUlrich that uses the correct size instead of a hard-coded size:

fileName <- 'foo.txt'
readChar(fileName, file.info(fileName)$size)

Note that readChar allocates space for the number of bytes you specify, so readChar(fileName, .Machine$integer.max) does not work well...

Solution 2 - R

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")

Solution 3 - R

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=" ")

Solution 4 - R

How about:

string <- readChar("foo.txt",nchars=1e6)

Solution 5 - R

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.

Solution 6 - R

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.

Solution 7 - R

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)

Solution 8 - R

It seems your solution is not much ugly. You can use functions and make it proffesional like these ways

  • first way
new.function <- function(filename){
  readChar(filename, file.info(filename)$size)
}

new.function('foo.txt')
  • second way
new.function <- function(){
  filename <- 'foo.txt'
  return (readChar(filename, file.info(filename)$size))
}

new.function()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSacha EpskampView Question on Stackoverflow
Solution 1 - RTommyView Answer on Stackoverflow
Solution 2 - RSharonView Answer on Stackoverflow
Solution 3 - RJosh O'BrienView Answer on Stackoverflow
Solution 4 - RJoshua UlrichView Answer on Stackoverflow
Solution 5 - RMike StanleyView Answer on Stackoverflow
Solution 6 - RFrank B. BrokkenView Answer on Stackoverflow
Solution 7 - Rharris11View Answer on Stackoverflow
Solution 8 - RKalanaView Answer on Stackoverflow