Reputation: 12410
Lately I'm having a strange problem with RStudio (v1.3.1056 on Ubuntu 18.04): When I try to open some text files, RStudio refuses with the message file is binary rather than text
:
Yet, the file I'm trying to open is definitely a text file. As an example, take this bibtex file:
@misc{test,
author = {test},
year = {2018},
title = {test},
}
When I try to open it with the file name "test.bib", I get the error above. When renaming it to "test.txt" RStudio opens it without complaining. Here the steps as R
code:
rstudioapi::navigateToFile("test.bib") # won't work
file.copy("test.bib", "test.txt", overwrite = TRUE)
rstudioapi::navigateToFile("test.txt") # works perfectly
I'm pretty sure that RStudio wouldn't mind the file ending and just try to open files as text no matter what in the past. But now I either have to rename the file or get a different text editor to edit my bib and other files.
As far as I can tell, this is not a problem of Ubuntu marking the file as binary. Otherwise one of these commands would complain, I think:
readLines("test.bib")
system("grep 'misc' test.bib")
Upvotes: 2
Views: 6080
Reputation: 12410
The solution to this problem is rather stupid. Apparently it is possible in Linux to include newline characters in your folder name, yet either RStudio or file
doesn't like that. I had no idea that my test.bib
file was sitting in a folder with a newline character. So the problem can be reproduced with:
bib <- "@misc{test,
author = {test},
year = {2018},
title = {test},
}"
dir.create("test\nfolder")
writeLines(bib, "./test\nfolder/test.bib")
rstudioapi::navigateToFile("./test\nfolder/test.bib") # throws error
While it works when the folder is just called "test folder"! Sorry for wasting people's time, I should have just tested it elsewhere...
Upvotes: 3
Reputation: 44788
You can see the current test of whether a file is text or not here: https://github.com/rstudio/rstudio/blob/d1289249b11e0d12d2be12b3ceb701c41f110cec/src/cpp/session/SessionModuleContext.cpp#L1216. It looks for a recorded MIME type according to the filename extension (and .bib
isn't in the list currently, so that will fail).
It then tries to run
file --dereference --mime --brief test.bib
and looks at the result. Maybe your system doesn't recognize test.bib
as text/plain
? A few other types would also be recognized as text; see the end of that function on github.
Upvotes: 2