This function takes a character string, a path to a text file or a URL as input and converts it to tidy-text format. The resulting tibble contains one row for each line of the input text, along with the file name, and line number.
Value
A tibble containing the converted text in tidy-text format,
with columns text, file (the path or URL, or "character string" for
literal text), line and batch_index (always 1).
Details
text is read as follows:
text containing a newline is literal text;
the path of an existing regular file (not a directory) is read from the file;
an
http://orhttps://URL is downloaded (with the timeout of theTheOpenAIR.timeoutoption, default 120 seconds; an HTTP error status is an error);anything else is literal text. A string that looks like a file path (no spaces and a common file extension such as
.R,.pyor.txt) but does not exist, or that names a directory, gives a warning, so that a mistyped path is not silently used as the text.
Examples
read_text("Hello, how are you?")
#> # A tibble: 1 × 4
#> text file line batch_index
#> <chr> <chr> <int> <dbl>
#> 1 Hello, how are you? character string 1 1
read_text("Hello!\nHow are you?")
#> # A tibble: 2 × 4
#> text file line batch_index
#> <chr> <chr> <int> <dbl>
#> 1 Hello! character string 1 1
#> 2 How are you? character string 2 1
# A file is read line by line
path <- tempfile(fileext = ".txt")
writeLines(c("First line.", "Second line."), path)
read_text(path)
#> # A tibble: 2 × 4
#> text file line batch_index
#> <chr> <chr> <int> <dbl>
#> 1 First line. /tmp/RtmpeNKywA/file1c2513afb980.txt 1 1
#> 2 Second line. /tmp/RtmpeNKywA/file1c2513afb980.txt 2 1
unlink(path)
