TL;DR
UTF-8 encoded files with non-ASCII strings are not correctly represented when running devtools::test()
on Windows. There should be an option to get the encoding from the Description file or from e.g. devtools::test(encoding = "UTF-8")
Description of issue
I am writing a package where strings containing non-ASCII characters need to be sent to a SQL server through RODBC.
By saving a query string with non-ASCII characters and running this in RStudio, R-terminal or using source from RStudio I am able to get the correct result.
However if I use devtools:test() my code fails since it seems that it does not source the functions using the correct encoding.
I have made a minimal example test functions (not using the SQL server). Here is the relevant code:
library(testthat)
context("Test encoding")
test_that("encoding works", {
query <- "Test encoding: ó æ ø å END TEST"
print(query)
})
Running this code in RStudio (using Ctrl-R) returns:
[1] "Test encoding: ó æ ø å END TEST"
Which is the correct and expected result.
Pressing the source button in RStudio runs:
source('C:/Users/hahad/workspaces/bugreports/tests/testthat/test_encoding.R', encoding = 'UTF-8', echo=TRUE)
and returns the correct, expected result:
> source('C:/Users/hahad/workspaces/bugreports/tests/testthat/test_encoding.R', echo=TRUE)
> library(testthat)
> context("Test encoding")
> test_that("encoding works", {
+ query <- "Test encoding: ó æ ø å END TEST"
+ print(query)
+ })
[1] "Test encoding: ó æ ø å END TEST"
From this I conclude that the files are saved as UTF-8 and should therefore be sourced as UTF-8 (also confirmed using external program and by inspecting the RStudio settings).
If I save this script in a testthat folder in an otherwise empty package and run
devtools::test()
I get the following result:
devtools::test()
Loading bugreports
Testing bugreports
Test encoding: [1] "Test encoding: ó æ ø å END TEST"
DONE =========================================================================================
This means that devtools is sourcing the files as latin1 instead of the UTF-8 files that they are. This happens irrespective of the Encoding: UTF-8 statement in the description file.
I have attempted to change to a UTF-8 encoding for my locale using
> Sys.setlocale("LC_ALL", 'UTF-8')
[1] ""
Warning message:
In Sys.setlocale("LC_ALL", "UTF-8") :
OS reports request to set locale to "UTF-8" cannot be honored
Summary
When using devtools::test() the encoding is not properly handled (at least on Windows). It is not possible to change to a UTF-8 encoding for the locale on Windows (as far as I can tell), even if this is the suggested encoding to use.
Proposed solutions
- Make devtools::test() respect the Encoding in the DESCRIPTION file.
- Make optional argument to devtools:test() for encoding (as there is for source-function.
- I should develop package entirely in latin1
- Find a way to use UTF-8 compatible locale in Windows.
I would prefer solution 1
:-)
TL;DR
UTF-8 encoded files with non-ASCII strings are not correctly represented when running devtools::test()
on Windows. There should be an option to get the encoding from the Description file or from e.g. devtools::test(encoding = "UTF-8")
Description of issue
I am writing a package where strings containing non-ASCII characters need to be sent to a SQL server through RODBC.
By saving a query string with non-ASCII characters and running this in RStudio, R-terminal or using source from RStudio I am able to get the correct result.
However if I use devtools:test() my code fails since it seems that it does not source the functions using the correct encoding.
I have made a minimal example test functions (not using the SQL server). Here is the relevant code:
Running this code in RStudio (using Ctrl-R) returns:
Pressing the source button in RStudio runs:
source('C:/Users/hahad/workspaces/bugreports/tests/testthat/test_encoding.R', encoding = 'UTF-8', echo=TRUE)and returns the correct, expected result:
From this I conclude that the files are saved as UTF-8 and should therefore be sourced as UTF-8 (also confirmed using external program and by inspecting the RStudio settings).
If I save this script in a testthat folder in an otherwise empty package and run
I get the following result:
This means that devtools is sourcing the files as latin1 instead of the UTF-8 files that they are. This happens irrespective of the
Encoding: UTF-8statement in the description file.I have attempted to change to a UTF-8 encoding for my locale using
Summary
When using devtools::test() the encoding is not properly handled (at least on Windows). It is not possible to change to a UTF-8 encoding for the locale on Windows (as far as I can tell), even if this is the suggested encoding to use.
Proposed solutions
I would prefer solution 1
:-)