Problem
I am developing a simple package to learn R
package development using this tutorial. For this purpose, I wrote a function and then tried it out within the global environment, and then provided it in the package. But I get different results while running the function in these two contexts.
Function
what_time <- function(language = "english") {
if (!language %in% c("urdu", "english")) {
stop("Either choose english or urdu as a language.")
}
time <- format(Sys.time(), "%H:%M")
if (language == "urdu") {
paste(time, "وقت ھوا ہے")
} else if (language == "english"){
sprintf("It is %s now!", time)
}
}
Running the function in the global environment:
I created the above function definition in R
console and then run the function:
what_time("urdu")
[1] "08:47 وقت ھوا ہے"
Running the function after loading the package:
Then I restarted the R session and did the following:
devtools::load_all()
i Loading umair
what_time("urdu")
[1] "08:47 <U 0648><U 0642><U 062A> <U 06BE><U 0648><U 0627> <U 06C1><U 06D2>"
What am I doing wrong?
CodePudding user response:
It seems that 'special characters' are not supported in your self-made package. Check the values of Encoding
in your package's DESCRIPTION file, and of Sys.getlocale()
, Sys.getenv('LANG')
, and Sys.getenv('LC_CTYPE')
in your R session.
Which language you have there shouldn't matter much, but it should be a UTF-8 variant.
I am having no problems to produce the correct output on my own machine with the following settings:
File 'testpackage/DESCRIPTION'
...
Encoding: UTF-8
...
R(Studio) session
> Sys.getlocale()
[1] "en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8"
> Sys.getenv('LANG')
[1] "en_US.UTF-8"
> Sys.getenv('LC_CTYPE')
[1] "en_US.UTF-8"
To modify these settings:
Sys.setlocale("LC_ALL", 'en_EN.UTF-8') Sys.setenv("LANG" = "en_EN.UTF-8") Sys.setenv("LC_CTYPE" = "en_EN.UTF-8")
RStudio Preferences
Preferences > Code > Saving > Default text encoding: UTF-8