Как в r studio поменять кодировку
Перейти к содержимому

Как в r studio поменять кодировку

  • автор:

How to change the character encoding of .R file in RStudio?

I want to save all .R file in one type of encoding, it is inconvenient to switch encodings everytimes I open a file, so, how to change the character encoding of .R file in RStudio?

3 Answers 3

R 3.4.3; RStudio 1.1.383

Tools > Global Options > Code > Saving > «Default text encoding» [Change]

enter image description here

You can try this. Go in «File» menu and then «Save with encoding. «

Tools — Options — General — Default text encoding:

enter image description here

James's user avatar

This question is in a collective: a subcommunity defined by tags with relevant content and experts.

    The Overflow Blog
Linked
Related
Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.3.11.43304

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Как в r studio поменять кодировку

введите описание изображения здесь

На следующем снимке экрана интересная строка — это номер 70, и вы должны добавить его EN в конце. введите описание изображения здесь

enter image description here

На следующем скриншоте интересная строка — это число 70, и вы должны добавить EN в конце. enter image description here

Write file as UTF-8 encoding in R for Windows

While the R uses UTF-8 encoding as default on Linux and Mac OS, the R for Windows does not use UTF-8 as default. So reading and writing UTF-8 files are something troublesome on Windows. In this article, I will show you a small script to help UTF-8 encoding.

What is the locale

On Mac OX (Japanese),

On Windows (Japanese),

On Mac OX (Russian),

On Windows (Russian),

The “LC_CTYPE” locale controls the encoding when the R writes a text file. As shown above, there is a clear difference of architecture between the Windows and the Mac OS.

Difference between Windows and other OSs

I am trying to say as simple as I can. The Windows chooses one of many language sets, however, the Linux and the Mac OS choose one language subset of a UTF-8 set. By this difference, the Windows forgets characters of unselected languages, while other OSs remember characters of all languages.

Problem on Windows

When a text is written to a file, characters of unselected locale languages can not be handled. Some of them are converted into a similar (but incorrect!) character, and others are written as escaped format such as <U+222D>.

Mind that the R is not responsible for this problem. Because the OS’s architecture of switching languages is generating the problem.

Locales

On Mac and Linux, they are shown by the command “locale -a”. Informations for Windows is valuable. Because Microsoft organize the information by the product version, the lifetime of urls is rather short.

Experiment

Let’s check what is going on.

Because I do not have a high grade Windows PC that covers multiple languages, I want to check this upon a Mac. The Mac OS has a lot of builtin locales that covers not only many languages but also Windows specific encodings. So I can use the Mac to simulate multiple language environments of Windows. Note that locale keywords to specify same encoding are different between Mac and Windows.

The methods are as below.

  1. Set a locale to simulate a Window encoding.
  2. Write a short UTF-8 string into a file.
  3. View the file with Safari.

A web browser (like Safari) is best to view result files, because it is designed to browse a text with worldwide’s encodings.

I tried the following script to perform this test.

Fig. 1. Unicode string

Fig. 1. Unicode string

I changed only “LC_CTYPE”, not entire “LC_ALL”. This is to minimize the impact. The unicode string is regenerated from a raw bytes array in every locale. The first 2 locales are for Mac OS. The 3rd “C” is only for ascii characters, and used commonly in Linux, Mac OS, and Windows. The 4th and the later is for emulating Windows.

Fig. 2. Results

As shown above, when the R writes a UTF-8 text into a file on Windows, characters of unsupported language are modified. In contrast, all characters are written correctly in Mac OS.

Using binary

There is a solution for this problem. Writing a binary file instead of a text file solves this. All applications handling a UTF-8 file in Windows are using the same trick.

The BOM should not be used in UTF-8 files. This is what the Linux and the Mac OS are doing. But the Windows Notepad and some applications use the BOM.
So, handling the BOM is needed, in spite of grammatically wrong.

The BOM is the 3 bytes character put at the beginning of a text file, but because the R does not use the BOM, it should be removed on reading.

Write UTF-8 file

Specify a UTF-8 string as x=, and a file name to write as file=. If you want to read the file only with the Windows Notepad, adding a BOM by the bom=T option is a good choice. Note that this is a minimum script, and not meant to write a very large file.

Read UTF-8 file

Reading a UTF-8 is easy, because functions like readLines have encoding= options that accepts UTF-8.

If you want to read a UTF-8 file saved by Windows standard applications like Notepad, you may have a trouble. Because the Windows Notepad appends BOM at writing a UTF-8 file, you must remove the BOM on the R. Or the BOM will appear as a corrupted character at the beginning of the string. Now, the R 3.0.0 supports UTF-8-BOM encoding to remove the BOM. However, if you want to use R 2.15.3 for a while, you must remove the BOM manually. The following code reads a UTF-8 file as binary and remove the BOM. Note that this is a minimum script, and not meant to read a very large file.

Encoding character strings in R

This document describes how to encode character strings in R by demonstrating Encoding() , enc2native() , enc2utf8() , iconv() and iconvlist() .

Finally, it is coded and tested the custom function safe.iconvlist() that aims to list successfully tested supported encodings from a source encoding to all supposedly supported encodings for the current platform by avoiding runtime errors and non-convertible strings.

Session information

Platform

Clarification about getOption(“encoding”)

This option is related to encoding connections (Files, URLs, etc) and not character vector encodings. Its factory fresh setting is "native.enc" if it was not previously changed with options(encoding) or in the initialization files Rprofile.site and .Rprofile .

Current Locale

From help(locales) : For category = “LC_ALL” the details of the string are system-specific: it might be a single locale name or a set of locale names separated by “/” (Solaris, macOS) or “;” (Windows, Linux). For portability, it is best to query categories individually: it is not necessarily the case that the result of foo <- Sys.getlocale() can be used in Sys.setlocale(“LC_ALL”, locale = foo).

Saving locale categories configuration individually.

Restoring saved locale categories configuration.

Warning about Sys.setlocale(“LC_CTYPE”)

From help(locales) : Attempts to change the character set by Sys.setlocale("LC_CTYPE") that implies a different character set during a session may not work and are likely to lead to some confusion because it may not affect the native encoding.

Localization information

The function l10n_info() reports on localization returning a list with three logical and one integer components:

MBCS : if a multi-byte character set in use?
UTF-8 : Is this a UTF-8 locale?
Latin-1 : Is this a Latin-1 locale?
codepage : the Windows codepage corresponding to the locale R is using (and not necessarily that Windows is using).

Native encoding indication

The native encoding indication is reported by l10n_info() in one of the logical elements UTF-8 and Latin-1 which is TRUE .

Native encoding indication for the current platform:

Current native encoding name

Character strings in R can be declared to be encoded in "latin1" or "UTF-8" or as "bytes" . A programmatic approach to deal with the current native encoding name in R functions is based on how character strings can be declared and the information reported by l10n_info .

Current foreign encoding name

A programmatic approach to deal with a foreign encoding name in R functions is based on how character strings can be declared and the information reported by l10n_info .

Base R functions to declare or convert encodings

Encoding() returns the encoding mark as "latin1" , "UTF-8" , "bytes" or "unknown" .
Encoding()<- sets the encoding mark without translating the character string.
enc2native() and enc2utf8() convert elements of character vectors to the native encoding or UTF-8 respectively, taking any marked encoding into account.
iconv() uses system facilities to convert a character vector between encoding. The names of encodings and which ones are available are platform-dependent. All R platforms support "" (for the encoding of the current locale), "latin1" and "UTF-8" . Any encoding bits on elements of x are ignored: they will always be translated as if from encoding from even if declared otherwise.

There are other ways for character strings to acquire a declared encodings. Some of them have an encoding argument that is used to declare encodings. Most character manipulation functions will set the encoding on output strings if it was declared on the corresponding input. These have changed as R has evolved and are mentioned in help(Encoding) . There are also external packages but are out of the scope of this document.

Custom function to display details about a string

Character vector encoding

Character strings in R can be declared to be encoded in "latin1" or "UTF-8" or as "bytes" .

An encoded character string contains characters beyond the basic ASCII characters. For instance, the string "Maurício" contains an i-acute.

When assigning an string to a name, it is marked with the native encoding indicated in l10n_info() , except for ASCII strings which are always marked as "unknown" .

ASCII strings

ASCII strings will never be marked with a declared encoding, since their representation is the same in all supported encodings.

Escaped strings

The string "Maurício" is escaped "Maur\xEDcio" if intended to be in "latin1" encoding and "Maur\xC3\xADcio" if intended to be in "UTF-8" encoding, therefore, they should be marked accordingly with Encoding()<- to let them portable between different encoding locales.

If your native encoding is "latin1"

If your native encoding is "UTF-8" .

Converting encoded strings

Base R provides enc2native() and enc2utf8() but doesn’t provide an "enc2latin1()" . Therefore, one can use iconv() but must use the from argument with careful because it must match the correct encoding mark of the input string, doesn’t accept "unknown" and be aware that its result is platform dependent. More details in help(iconv) .

Internationalization Convertion Test

From help(iconvlist) : On most platforms iconvlist provides an alphabetical list of the supported encodings. On others, the information is on the man page for iconv(5) or elsewhere in the man pages (but beware that the system command iconv may not support the same set of encodings as the C functions R calls). Unfortunately, the names are rarely supported across all platforms. Value for iconvlist(), a character vector (typically of a few hundred elements) of known encoding names.

Number of supposed supported encodings.

Number of unique supported encodings may be differ.

Test string “Maurício” as “latin1”

Trying to convert the string in 374 supposedly supported target encodings.

Target locales that produce R runtime errors

Runtime error messages

Target locales that cannot convert all bytes

It is due to character strings that cannot be converted because of any of their bytes that cannot be represented in the target encoding, producing NA results.

Here it is used the sub = "byte" argument to replace any non-convertible byte in the input with its hex code indicated with "<xx>" in order to inspect which bytes in the input are non-convertible.

Contingency table of non-convertible strings.

Target locales grouped by non-convertible strings

Target locales that encoded “unknown”

Contingency table of results encoded “unknown”

Target locales grouped by results encoded “unknown”

Target locales that encoded as native “latin1”

Contingency table of results encoded as native “latin1”

Target locales grouped by results encoded as native “latin1”

Target locales that produced new encodings different from “unknown”

Contingency table of results with new encoding

Target locales grouped by new encodings different from “unknown”

safe.iconvlist()

As one can see through the tests above, iconvlist() returns an unsafe list of encodings that may even stop() your R code. All these tests made possible to develop the custom function safe.iconvlist that aims to list successfully tested supported encodings from a source encoding (defaults to from=Encoding(x) ) to all supposedly supported encodings for the current platform by avoiding runtime errors and non-convertible strings.

safe.iconvlist() test for latin1 characters strings

The test strings are defined by the ISO-8859-1 codepoints: https://en.wikipedia.org/wiki/ISO/IEC_8859-1

Number of ISO-8859-1 codepoints

Character strings created from raw vectors are marked “unknown”

Therefore they should be marked as “latin1” wherever possible for the test

Number of real supported encodings for the test strings.

A merged test string shows real supported encodings for the full ISO-88591 character set.

safe.iconvlist() test for UTF-8 character strings

The test strings are based on Basic Multilingual Plane (BMP) which contains characters for almost all modern languages, and a large number of symbols. Most of the assigned code points in the BMP are used to encode Chinese, Japanese, and Korean (CJK) characters. https://en.wikipedia.org/wiki/UTF-8.

There is large number of assigned UTF-8 codepoints:

The only test string encoded “unknown” is the ASCII string as expected.

Number of real supported encodings for the test strings.

A merged test string shows real supported encodings for the full UTF-8 character set.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *