如果您的输出是PDF,则可能需要一些替换功能。 这是我正在使用的:
rtidyPrint <- function(data) { content <- paste0(data,collapse = "\n\n") content <- str_replace_all(content,"\\t"," ") content <- str_replace_all(content,"\\ ","\\\\ ") content <- str_replace_all(content,"\\$","\\\\$") content <- str_replace_all(content,"\\*","\\\\*") content <- str_replace_all(content,":",": ") return(content) }
例
代码也需要有所不同:
{r my_pre_example,echo=FALSE,include=TRUE,results='asis'}knitr::opts_chunk$set(comment = NA)resultTTest <- capture.output(t.test(mtcars$mpg,mtcars$wt))cat(tidyPrint(resultTTest))