我在R中编写一个函数,根据输入返回不同的字符串,但似乎任何瑞典字符(åäö)在从函数返回时都会变成乱码。
return_string<-function(x){
z<-switch(x,
1 = c("<br><strong>åäö:</strong>"),
2 = c("<br><strong>öäå:</strong>")
return(z)
}
但是当我尝试打印时,我得到了......
var<-1
z<-return_string(var)
print(z)
[1] "<br><strong>åäö:</strong>"
这很奇怪,因为我有UTF-8作为默认文本编码,我可以使用åäö就好了,看起来只有当它们从list或c()格式的函数返回时才会发生。如果我检查Sys.getlocale,我会......
> Sys.getlocale()
[1] "LC_COLLATE=Swedish_Sweden.1252;LC_CTYPE=Swedish_Sweden.1252;
LC_MONETARY=Swedish_Sweden.1252;LC_NUMERIC=C;LC_TIME=Swedish_Sweden.1252"
我的会话信息是......
> sessionInfo()
R version 3.2.2 (2015-08-14)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=Swedish_Sweden.1252 LC_CTYPE=Swedish_Sweden.1252
[3] LC_MONETARY=Swedish_Sweden.1252 LC_NUMERIC=C
[5] LC_TIME=Swedish_Sweden.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] tau_0.0-18 RColorBrewer_1.1-2 htmltools_0.2.6 magrittr_1.5
[5] leaflet_1.0.0 shiny_0.12.2 sp_1.2-1
loaded via a namespace (and not attached):
[1] Rcpp_0.12.2 lattice_0.20-33 digest_0.6.8 mime_0.4
[5] plyr_1.8.3 grid_3.2.2 R6_2.1.1 xtable_1.8-0
[9] jsonlite_0.9.17 scales_0.3.0 tools_3.2.2 htmlwidgets_0.5
[13] munsell_0.4.2 httpuv_1.3.3 yaml_2.1.13 colorspace_1.2-6
答案 0 :(得分:0)
尝试运行代码时出错。当{1}}的类型参数应该是switch
时,character
的替代方法的语法会有所不同,因为您的语法看起来如此:
ret_str_chr <- function( type) {
switch(type,
'1' = c("<br><strong>åäö:</strong>"),
'2' = c("<br><strong>öäå:</strong>") )
}
ret_str_chr(var)
#[1] "<br><strong>åäö:</strong>"
当type / choice参数为numeric时,您只按要归类的顺序放置要返回的项目:
ret_str_n <- function( choice) {
switch(choice,
c("<br><strong>åäö:</strong>"),
c("<br><strong>öäå:</strong>") )
}
var=1
ret_str_n(var)
#[1] "<br><strong>åäö:</strong>"