Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将向量打印为\Sexpr{}中的“and”_R_Knitr_Sweave - Fatal编程技术网

将向量打印为\Sexpr{}中的“and”

将向量打印为\Sexpr{}中的“and”,r,knitr,sweave,R,Knitr,Sweave,考虑这个简单的向量: x <- c(1,2,3,4,5) 这似乎在控制台\Sexpr{nicevectorx}中工作,但在\Sexpr{x}工作时在.Rnw文件中失败。一些想法?你可以使用knitr::combine\u wordsx 使用cat只是为了它的副作用:在控制台中打印。cat不会返回字符串,因此在输出中看不到任何内容。相比之下,knitr::combine_words返回一个字符串。在glue包中也有一个用于此的函数 看我不记得knitr有这么好的功能,我开始。。。非常感谢

考虑这个简单的向量:

x <- c(1,2,3,4,5) 
这似乎在控制台\Sexpr{nicevectorx}中工作,但在\Sexpr{x}工作时在.Rnw文件中失败。一些想法?

你可以使用knitr::combine\u wordsx


使用cat只是为了它的副作用:在控制台中打印。cat不会返回字符串,因此在输出中看不到任何内容。相比之下,knitr::combine_words返回一个字符串。

在glue包中也有一个用于此的函数


我不记得knitr有这么好的功能,我开始。。。非常感谢你的帮助。
x <- c(1,2,3,4,5)
nicevector <- function(x){
    a <- head(x,length(x)-1)
    b <- tail(x,1)
    cat(a,sep=", ");cat(" and ");cat(b)}
nicevector(x)
glue::glue_collapse(1:4, ",", last = " and ")
#> 1, 2, 3 and 4