在捕获输出时,RStudio和dplyr的格式化问题

在捕获输出时,RStudio和dplyr的格式化问题,r,dplyr,rstudio,R,Dplyr,Rstudio,当我试图捕获输出时,最新版本的RStudio面临编码问题 如果我定义一个TIBLE并将其打印,我将获得预期的结果: library(dplyr) x <- tibble(test=c("Hello","World")) print(x) R会话信息为: > sessionInfo() R version 4.0.5 (2021-03-31) Platform: x86_64-w64-mingw32/x64 (64-bit) Running u

当我试图捕获输出时,最新版本的RStudio面临编码问题

如果我定义一个TIBLE并将其打印,我将获得预期的结果:

library(dplyr)
x <- tibble(test=c("Hello","World"))
print(x)
R会话信息为:

> sessionInfo()
R version 4.0.5 (2021-03-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

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

other attached packages:
[1] dplyr_1.0.5

loaded via a namespace (and not attached):
 [1] fansi_0.4.2      assertthat_0.2.1 utf8_1.2.1       crayon_1.4.1     R6_2.5.0         DBI_1.1.1       
 [7] lifecycle_1.0.0  magrittr_2.0.1   pillar_1.6.0     cli_2.4.0        rlang_0.4.10     rstudioapi_0.13 
[13] vctrs_0.3.7      generics_0.1.0   ellipsis_0.3.1   forcats_0.5.1    tools_4.0.5      glue_1.4.2      
[19] purrr_0.3.4      compiler_4.0.5   pkgconfig_2.0.3  tidyselect_1.1.0 tibble_3.1.0    

这是用于颜色和突出显示的。您可以通过以下方式禁用:

options(cli.num_colors=1)

Stéphane的回答显示了一种禁用输出着色的方法。但更好的解决方案是不使用
capture.output
,而是使用更合适的方式在闪亮的应用程序中显示表格,例如通过“DT”包或从表格中呈现静态HTML输出的众多包之一。谢谢康拉德。我使用
DT
在我闪亮的应用程序中显示漂亮的表格,但是在这种情况下,这个
capture.output
非常方便,可以检查引擎盖下发生了什么。(我使用常规
cat()
捕获底层代码的一些行为。在我的代码中,使用
paste0(txt,“\n”,collapse=”“)对捕获进行后期处理。
然后使用
renderText
直接在(开发)应用程序中呈现它)
[1] "\033[38;5;246m# A tibble: 2 x 1\033[39m"      "  tes  "                                     
[3] "  \033[3m\033[38;5;246m<chr>\033[39m\033[23m" "\033[38;5;250m1\033[39m Hello"               
[5] "\033[38;5;250m2\033[39m World" 
$mode
[1] "desktop"

$version
[1] ‘1.4.1106’

$release_name
[1] "Tiger Daylily"  
> sessionInfo()
R version 4.0.5 (2021-03-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

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

other attached packages:
[1] dplyr_1.0.5

loaded via a namespace (and not attached):
 [1] fansi_0.4.2      assertthat_0.2.1 utf8_1.2.1       crayon_1.4.1     R6_2.5.0         DBI_1.1.1       
 [7] lifecycle_1.0.0  magrittr_2.0.1   pillar_1.6.0     cli_2.4.0        rlang_0.4.10     rstudioapi_0.13 
[13] vctrs_0.3.7      generics_0.1.0   ellipsis_0.3.1   forcats_0.5.1    tools_4.0.5      glue_1.4.2      
[19] purrr_0.3.4      compiler_4.0.5   pkgconfig_2.0.3  tidyselect_1.1.0 tibble_3.1.0    
options(cli.num_colors=1)