4.2 Import .txt Files
The .txt format is one of the most common formats to store texts in. Not only are these files format-free, but they are also small in size, support a wide array of characters, and are readable on all platforms. To read them into R, we first specify where on our computer these files are. Here they are in a folder called Texts in our Working Directory. To tell R where this is, we first set our working directory (to see where your current Working Directory is, type getwd()
into the Console). Then, we add /Texts
to this and save it as txt_directory
. We can then use this to refer to this folder when we import the files using readtext
:
library(readtext)
setwd("Your Working Directory")
txt_directory <- paste0(getwd(), "/Texts")
data_texts <- readtext(paste0(txt_directory, "*"), encoding = "UTF-8")
Note that now you not only have the texts in your environment but the object txt_directory
as well. Note that this is nothing more than a string that we can use later on to prevent us from having to type the whole address of the folder.