How do you use special letters (danish) in R-studio? - r

I am from Denmark and need to be able to use danish letters in my R-code.
If I add some code like:
print("Århus er bare øv...")
and save it as an R-script file in R-studio, the next time I open the file in R-studio I get:
print("?rhus er bare ?v...")

When saving your file, use File -> Save with encoding and select e.g. "UTF-8". If the file is read incorrectly next time, use File -> Reopen with encoding.
You may also change the RStudio preferences (Tools -> Global options / General->Default text encoding) and set "UTF-8".

Related

Which is the correct encoding for a degree character?

I have a line of code that alters text
temperature<-as.numeric(gsub("°.*","",temp))
R does not like the "°" character. When I save the file it says I need to use a different encoding.
I have tried all sorts of different encodings from the list, but they all save the code in some variation of
temperature<-as.numeric(gsub("??.*","",temp))
My current solution is to open the script in notepad and copy paste the code into rstudio. Which encoding do I need to save a ° in rstudio?
The full solution to this in rstudio was to go to file -> save with encoding -> select ISO-8859-1 -> check the box Set as default encoding for source files. Now the file opens properly with the degree character every time.

RStudio won't open some text files

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

.Rmd files open as completely empty

When opening .rmd files in RStudio 3.3.2, they show up as completely empty. There is text if I open using Notepad or if I open on another machine. What is going on?
RMD file in question
I had a similar issue with older R files that opened as empty. It turned out that RStudio didn't use the correct encoding as default and therefore wasn't able to read the file (presented the file as empty).
You can make sure that you are using the correct encoding by:
Opening the file in RStudio as you normally would (the file will be empty)
Navigate to File -> Reopen with Encoding...
Select UTF-8 and click OK
UTF-8 will most likely be the encoding you need. You can also choose to set this as the default for all source files.
This issue was also addressed on RStudio Support
In RStudio go to:
Tools
Global Setting
Left hand side "Code"
a. under "Saving" - Default text econding: change to UTF-8
save #Richard N mentioned, save the files with "UTF-8" encoding. Will solve the issue.
Incase you saved the files without encoding, use the open with encoding option under "Files" tab.

R text encoding

The R Data Import/Export Manual says that there is a good way to guess the encoding of a text file is to use the "file" command line tool (available in R tools). How would one use this? I already have the newest version of Rtools installed. Is this something I can do from my R session? Or do I need to open up the command prompt?
In the context of R Data Import/Export Manual, I interpret it as using a file on a command prompt.
However you can invoke a system command with system() function from R. For example if I have a file called mpi.R in the current directory, I can do:
> foo <- system('file mpi.R', intern=TRUE, ignore.stdout=FALSE, ignore.stderr=TRUE, wait=TRUE)
> print(foo)
[1] "mpi.R: ASCII text"
The "command prompt" here refers to a "Terminal" window (OS X or Linux) or "Command Prompt" (Windows). From these, you have access to the command-line file utility, which as the manual states, provides a good description of the type and format of (text) files.
You can also run this straight from R, using the system() function to pass the call to file. For example, on my system, in the current working directory I have three text files:
> list.files(pattern = "*.txt")
[1] "00005802.txt" "googlebooks-eng-all-totalcounts-20120701.txt"
[3] "sentences.txt"
> system("file *.txt")
00005802.txt: Par archive data
googlebooks-eng-all-totalcounts-20120701.txt: ASCII text, with very long lines, with no line terminators
sentences.txt: ASCII English text, with very long lines
It could be that file will call something "plain ASCII" when it only contains the lower 128 ASCII characters, but this will be the same as UTF-8 since those two encodings share the same 8-bit mappings of the first 128 ASCII characters.
Also, file is not always right -- for instance the 00005802.txt is in fact UTF-8 encoded text that I converted from a pdf using pdftotext.
Also beware that on most Windows platforms, you cannot set your system locale to UTF-8 in R. Try Sys.getlocale(). (To set it, use Sys.setlocale()).

Programmatically open a text file in R?

Is there something in R to open a text file? In Python, one could open a text file using notepad:
import subprocess as sp
programName = "notepad.exe"
fileName = "file.txt"
sp.Popen([programName, fileName])
Other methods are described in the post as well. Is there something similar in R?
Keeping in mind portability issues, i.e. this is specific to Windows, you can use shell.exec, which will open the file in whatever program is associated with that type of file by default - e.g.
## full path
shell.exec(paste0(c(getwd(), "/tmpfile.txt"),collapse = ""))
## relative to current working directory
shell.exec("tmpfile.txt")
both open tmpfile.txt in notepad on my machine.

Resources