如何在 R 中的字符串中打印符号“±”(将此字符放在 table 中,如 `kableextra`)

How to print the sign "±" in a string in R (to place this character in a table like `kableextra`)

我想将 ± 形式的加减号打印到 R 中的字符串中,这样我就可以将该字符串放在 table 和 kableextra

字符串的结构如下:

x = paste0("first_string", 2, "±", 3, "second_string", collapse = "")

但是我得到以下输出:

"first_string2213second_string"

我应该怎么做才能得到输出:

"first_string2±3second_string"

我根据this website or this website

发现21是八进制转义序列中±的编码

EDIT 这是 sessionInfo() 的输出:

R version 3.5.0 (2018-04-23)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS  10.14

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] C

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

other attached packages:
 [1] bindrcpp_0.2.2   rms_5.1-2        SparseM_1.77     Hmisc_4.1-1      Formula_1.2-3   
 [6] lattice_0.20-35  blme_1.0-4       emmeans_1.2.4    lme4_1.1-18-1    Matrix_1.2-14   
[11] multcomp_1.4-8   TH.data_1.0-8    MASS_7.3-49      mvtnorm_1.0-8    survminer_0.4.3 
[16] survival_2.41-3  latex2exp_0.4.0  magick_1.9       cowplot_0.9.3    ggpubr_0.1.7    
[21] magrittr_1.5     gridExtra_2.3    rcompanion_2.0.0 ggsignif_0.4.0   ggplot2_3.1.0   
[26] chron_2.3-52     lubridate_1.7.4  dplyr_0.7.5      kableExtra_0.9.0 knitr_1.20      
[31] broom_0.4.5     

loaded via a namespace (and not attached):
 [1] minqa_1.2.4         colorspace_1.3-2    class_7.3-14        modeltools_0.2-22  
 [5] rprojroot_1.3-2     estimability_1.3    htmlTable_1.12      base64enc_0.1-3    
 [9] rstudioapi_0.7      MatrixModels_0.4-1  manipulate_1.0.1    coin_1.2-2         
[13] xml2_1.2.0          codetools_0.2-15    mnormt_1.5-5        nloptr_1.0.4       
[17] km.ci_0.5-2         cluster_2.0.7-1     readr_1.1.1         compiler_3.5.0     
[21] httr_1.3.1          backports_1.1.2     assertthat_0.2.0    lazyeval_0.2.1     
[25] quantreg_5.36       acepack_1.4.1       htmltools_0.3.6     tools_3.5.0        
[29] coda_0.19-1         gtable_0.2.0        glue_1.3.0          reshape2_1.4.3     
[33] Rcpp_1.0.0          nlme_3.1-137        psych_1.8.4         lmtest_0.9-36      
[37] stringr_1.3.1       rvest_0.3.2         polspline_1.1.13    zoo_1.8-2          
[41] scales_1.0.0        hms_0.4.2           parallel_3.5.0      sandwich_2.4-0     
[45] expm_0.999-2        RColorBrewer_1.1-2  yaml_2.1.19         BSDA_1.2.0         
[49] KMsurv_0.1-5        EMT_1.1             rpart_4.1-13        latticeExtra_0.6-28
[53] stringi_1.2.4       nortest_1.0-4       e1071_1.6-8         checkmate_1.8.5    
[57] boot_1.3-20         rlang_0.3.0.1       pkgconfig_2.0.1     evaluate_0.10.1    
[61] purrr_0.2.5         bindr_0.1.1         labeling_0.3        htmlwidgets_1.2    
[65] cmprsk_2.2-7        tidyselect_0.2.4    plyr_1.8.4          R6_2.3.0           
[69] DescTools_0.99.25   multcompView_0.1-7  pillar_1.2.3        foreign_0.8-70     
[73] withr_2.1.2         nnet_7.3-12         tibble_1.4.2        survMisc_0.5.5     
[77] rmarkdown_1.10      grid_3.5.0          data.table_1.11.4   digest_0.6.18      
[81] xtable_1.8-2        tidyr_0.8.1         stats4_3.5.0        munsell_0.5.0      
[85] viridisLite_0.3.0  

区域设置控制输入和输出的字符集。你有

locale:
[1] C

但是如果你想打印非 ASCII 字符,比如 ±,你需要像 UTF-8 这样的东西。如何实现这一点的一个例子(根据评论似乎已经解决了你的问题)是

Sys.setlocale("LC_ALL","en_US.UTF-8")
paste0("first_string", 2, "±", 3, "second_string", collapse = "")

应该输出

[1] "first_string2±3second_string"

要了解更多信息,我会查看以下来源作为起点: