I noticed this when switching from readr 1.3.1 to 1.4.0, but as shown below there is some unexpected behavior in 1.3.1 too. Basically you get weird behavior (usually an empty file) when you have a sequence of functions connected by a %>% that:
- reads something in with
read_file()
- modifies the resulting string
- writes something out with
write_file()
Reprex 1
This is a version of what my original code was doing: using stringr::str_replace() to modify the contents of a file.
# This works as expected: foo is replaced by bar in the file
> library(readr)
> library(stringr)
> packageVersion("readr")
[1] ‘1.3.1’
> packageVersion("stringr")
[1] ‘1.4.0’
> write_file("foo", "foo.txt")
> read_file("foo.txt")
[1] "foo"
> read_file("foo.txt") %>% str_replace("foo", "bar")
[1] "bar"
> read_file("foo.txt") %>% str_replace("foo", "bar") %>% write_file("foo.txt")
> read_file("foo.txt")
[1] "bar"
But when I do this with 1.4.0 I get a blank file:
> library(readr)
> library(stringr)
> packageVersion("readr")
[1] ‘1.4.0’
> packageVersion("stringr")
[1] ‘1.4.0’
> write_file("foo", "foo.txt")
> read_file("foo.txt")
[1] "foo"
> read_file("foo.txt") %>% str_replace("foo", "bar")
[1] "bar"
> read_file("foo.txt") %>% str_replace("foo", "bar") %>% write_file("foo.txt")
> read_file("foo.txt")
[1] ""
Reprex 2
I tried to simplify this example by using paste() instead, and now I get unexpected behavior in both 1.3.1 and 1.4.0. Hopefully this helps instead of confusing the issue further. Either way, I would expect the result of read_file("foo.txt") %>% paste("second") to the same thing that ends up in the file when I pipe it to write_file() but that's not the case in either version.
> library(readr)
> packageVersion("readr")
[1] ‘1.3.1’
> write_file("first", "foo.txt")
> read_file("foo.txt")
[1] "first"
> read_file("foo.txt") %>% paste("second")
[1] "first second"
> read_file("foo.txt") %>% paste("second") %>% write_file("foo.txt")
> read_file("foo.txt")
[1] " second"
And also here
> library(readr)
> packageVersion("readr")
[1] ‘1.4.0’
> write_file("first", "foo.txt")
> read_file("foo.txt")
[1] "first"
> read_file("foo.txt") %>% paste("second")
[1] "first second"
> read_file("foo.txt") %>% paste("second") %>% write_file("foo.txt")
> read_file("foo.txt")
[1] " second"
I noticed this when switching from readr 1.3.1 to 1.4.0, but as shown below there is some unexpected behavior in 1.3.1 too. Basically you get weird behavior (usually an empty file) when you have a sequence of functions connected by a
%>%that:read_file()write_file()Reprex 1
This is a version of what my original code was doing: using
stringr::str_replace()to modify the contents of a file.But when I do this with 1.4.0 I get a blank file:
Reprex 2
I tried to simplify this example by using
paste()instead, and now I get unexpected behavior in both 1.3.1 and 1.4.0. Hopefully this helps instead of confusing the issue further. Either way, I would expect the result ofread_file("foo.txt") %>% paste("second")to the same thing that ends up in the file when I pipe it towrite_file()but that's not the case in either version.And also here