Difference output works rscript vs source

I have the following script:

city <- c("", NA, "", "",
          "", "", NA, "",
          "", "/-",
          "/-", "",
          "", "", "", "",
          " ", "-")
city[grep("^(||msk)", city, ignore.case = TRUE)] <- ""
city[grep("||spb|", city, ignore.case = TRUE)] <- "-"
city[grep("|-", city, invert = TRUE)] <- " "
print(city)

When I run Rscript test.R, I get some results:

% Rscript test.R
[1] " "   " "   " "   " "
[5] " "   " "   " "   ""
[9] ""          "-" "-" " "
[13] " "   "-" ""          ""
[17] ""          "-"

When I run source("test.R"), I get different results:

% Rscript -e 'source("test.R")'
[1] " "            " "           
[3] " "            " "           
[5] " "            " "           
[7] " "            ""                  
[9] ""                   "/-"
[11] "/-" " "           
[13] " "            " "
[15] ""                   ""
[17] " "                  "-"

I got the correct results when:

  • run the script with Rscript:Rscript test.R
  • enter the commands on line R in a row line by line

C source()I got the wrong results (using Rscript -eor inside an R session).

Information about the system may be useful:

sessionInfo()
R version 3.2.1 (2015-06-18)
Platform: x86_64-unknown-linux-gnu (64-bit)
Running under: Arch Linux

locale:
    [1] LC_CTYPE=ru_RU.UTF-8       LC_NUMERIC=C               LC_TIME=ru_RU.UTF-8        LC_COLLATE=C              
[5] LC_MONETARY=ru_RU.UTF-8    LC_MESSAGES=ru_RU.UTF-8    LC_PAPER=ru_RU.UTF-8       LC_NAME=C                 
[9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=ru_RU.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
    [1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
    [1] tools_3.2.1
+4
source share
1 answer

This is due to file encoding. Add to the sourcefollowing options:encoding="UTF-8", verbose=T

( verbose = T), , encoding = "native.enc", .

+2

Source: https://habr.com/ru/post/1606146/


All Articles