How to change the character encoding of .R file in RStudio?
You can try this. Go in "File" menu and then "Save with encoding..."
How to change the encoding of R files
I think you are looking for either "File > Reopen with Encoding" or "File > Save with Encoding". If the source file already is in the other encoding, then re-open it, but if you want to change it into the other encoding, then choose "Save with encoding".
Which is the correct encoding for a degree character?
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 not picking the encoding I'm telling it to use when reading a file
This problem is caused by the wrong locale being set, whether inside RStudio or command-line R:
If the problem only happens in RStudio not command-line R, go to RStudio->Preferences:General, tell us what 'Default text encoding:'is set to, click 'Change' and try Windows-1252, UTF-8 or ISO8859-1('latin1') (or else 'Ask' if you always want to be prompted). Screenshot attached at bottom. Let us know which one worked!
If the problem also happens in command-line R, do the following:
Do locale -m
on your Mac and tell us whether it supports CP1252 or else ISO8859-1 ('latin1')? Dump the list of supported locales if you need to. (You might as well tell us your version of MacOS while you're at it.)
For both of those locales, try to change to that locale:
# first try Windows CP1252, although that's almost surely not supported on Mac:
Sys.setlocale("LC_ALL", "pt_PT.1252") # Make sure not to omit the `"LC_ALL",` first argument, it will fail.
Sys.setlocale("LC_ALL", "pt_PT.CP1252") # the name might need to be 'CP1252'
# next try IS08859-1(/'latin1'), this works for me:
Sys.setlocale("LC_ALL", "pt_PT.ISO8859-1")
# Try "pt_PT.UTF-8" too...
# in your program, make sure the Sys.setlocale worked, sprinkle this assertion in your code before attempting to read.csv:
stopifnot(Sys.getlocale('LC_CTYPE') == "pt_PT.ISO8859-1")
That should work.
Strictly the Sys.setlocale()
command should go in your ~/.Rprofile
for startup, not inside your R session or source-code.
However Sys.setlocale()
can fail, so just be aware of that. Also, assert Sys.getlocale()
inside your setup code early and often, as I do. (really, read.csv
should figure out if the encoding it uses is compatible with the locale, and warn or error if not).
Let us know which fix worked! I'm trying to document this more generally so we can figure out the correct enhance.
- Screenshot of RStudio Preferences Change default text encoding menu:
How to source() .R file saved using UTF-8 encoding?
We talked about this a lot in the comments to my previous post but I don't want this to get lost on page 3 of comments: You have to set the locale, it works with both input from the R-console (see screenshot in comments) as well as with input from file see this screenshot:
The file "myfile.r" contains:
russian <- function() print ("Американские с...");
The console contains:
source("myfile.r", encoding="utf-8")
> Error in source(".....
Sys.setlocale("LC_CTYPE","ru")
> [1] "Russian_Russia.1251"
russian()
[1] "Американские с..."
Note that the file-in fails and it points to the same character as the original poster's error (the one after "R). I can not do this with Chinese because i would have to install "Microsoft Pinyin IME 3.0", but the process is the same, you just replace the locale with "chinese" (the naming is a bit inconsistent, consult the documentation).
R encoding unable to save symbol
Use "File -> Save with encoding -> UTF-8". Unicode is a "superset" of all the encodings, thus it handles all the code points you can imagine. Moreover, R also is Unicode-aware.
Track the exact place of a not encoded character in an R script file
Use tools::showNonASCIIfile()
to spot the non-ascii.
Related Topics
Overlapped Density Plots in Ggplot2
Error in Dev.Off(): Cannot Shut Down Device 1 (The Null Device)
Aggregating Rows for Multiple Columns in R
R Finding Duplicates in One Column and Collapsing in a Second Column
How to Make UI Respond to Reactive Values in for Loop
Could Not Find Function Tagpos
How to Make Install.Packages Return an Error If an R Package Cannot Be Installed
Read List of File Names from Web into R
How to Create a Prop.Table() for a Three Dimension Table
Plot The Intensity of a Continuous with Geom_Tile in Ggplot
Ggplot: Recommended Colour Palettes Also Distinguishable for B&W Printing
How to Give Numbers to Each Group of a Dataframe with Dplyr::Group_By
Barplot with Multiple Columns in R
How to Use Custom Cross Validation Folds with Xgboost
Find Match of Two Data Frames and Rewrite The Answer as Data Frame