我想知道如何测试生成图形的函数.我有一个简单的绘图功能img:
img <- function() {
plot(1:10)
}
Run Code Online (Sandbox Code Playgroud)
在我的包中,我喜欢使用这个函数创建一个单元测试testthat.因为plot和它的朋友在基础图形只是返回NULL一个简单
expect_identical的不工作:
library("testthat")
## example for a successful test
expect_identical(plot(1:10), img()) ## equal (as expected)
## example for a test failure
expect_identical(plot(1:10, col="red"), img()) ## DOES NOT FAIL!
# (because both return NULL)
Run Code Online (Sandbox Code Playgroud)
首先,我考虑绘制到文件中并比较md5校验和以确保函数的输出相等:
md5plot <- function(expr) {
file <- tempfile(fileext=".pdf")
on.exit(unlink(file))
pdf(file)
expr
dev.off()
unname(tools::md5sum(file))
}
## example for a successful test
expect_identical(md5plot(img()),
md5plot(plot(1:10))) ## equal (as expected)
## example for …Run Code Online (Sandbox Code Playgroud) 让我们假设你想根据条件构建一个简单的测试setdiff(input, 1:9).
我怎样才能构建一个
if isnotempty(setdiff(input, 1:9)) stop ("not valid")
Run Code Online (Sandbox Code Playgroud)
在输入时停止执行c(3, 12)但在输入时继续执行的语句c(2,5,7)?非常感谢,伯蒂
有没有办法比较两个函数对象是否相同?
m <- mean
m == mean ## don't work
## this seems not to be the correct way:
functionBody(mean)==functionBody(m)
Run Code Online (Sandbox Code Playgroud)
编辑:更多细节.我有一个带有两个参数的函数(一个矩阵和一个用户定义的函数,它按列方式应用,例如mean,median,...).如果该功能是mean我想要改为使用colMean(以节省一些运行时间).
foo <- function(m, fun) {
#if (fun==mean) {
# return(colMeans(m));
#} else {
return(apply(m, 2, fun));
#}
}
Run Code Online (Sandbox Code Playgroud) 我testthat用来测试包含类似于以下文件树的包:
.
??? data
? ??? testhaplom.out
??? inst
? ??? test
? ??? test1.r
? ??? tmp_S7byVksGRI6Q
? ? ??? testm.desc
? ??? tmp_vBcIkMN1arbn
? ???testm.bin
? ??? testm.desc
??? R
? ??? haplom.r
? ??? winIdx.r
??? tmp_eUG3Qb0PKuiN
??? testhaplom.hap2.desc
Run Code Online (Sandbox Code Playgroud)
在test1.r文件中,我需要使用该data/testhaplom.out文件作为某个函数的输入数据,但如果我这样做test_file(test1.r),它将更改为inst/test目录并且无法查看数据文件,给出以下错误:
...Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file 'data/testhaplom.out': No such file or directory
Run Code Online (Sandbox Code Playgroud) 我在Rcpp遇到条件问题.解释我的问题的最好方法是通过一个例子.
z <- seq(from=1,to=10,by=0.1)
z[c(5,10,15,20,40,50,80)] <- NA
src <- '
Rcpp::NumericVector vecz(z);
for (int i=0;i<vecz.size();i++) {
if (vecz[i] == NA_REAL) {
std::cout << "Here is a missing value" << std::endl;
}
}
'
func <- cxxfunction(signature(z="numeric"),src,plugin="Rcpp")
func(z)
# NULL
Run Code Online (Sandbox Code Playgroud)
根据我的理解,NA_REAL表示Rcpp中的实数NA_Integer的NA值,并表示整数的NA值.我不确定为什么上面的条件永远不会返回true给出z.
我试图了解set.seedR中的作用.我理解它,可以重现随机样本,但我不知道set.seed(1)和之间的区别是set.seed(123)什么?
括号中的参数是什么意思?
我对R很新,并且遇到了NA的问题.这个问题可能已在其他地方得到解答,但我似乎无法找到答案.我试图做一些相反的rowSums()事情,因为我试图减去x2和x3从中x1产生x4没有NA的产生.我目前使用的代码如下:
> x <- data.frame(x1 = 3, x2 = c(4:1, 2:5), x3=c(1,NA))
> x$x4=x$x1-x$x2-x$x3
> x
x1 x2 x3 x4
1 3 4 1 -2
2 3 3 NA NA
3 3 2 1 0
4 3 1 NA NA
5 3 2 1 0
6 3 3 NA NA
7 3 4 1 -2
8 3 5 NA NA
Run Code Online (Sandbox Code Playgroud)
换句话说,我想让NA的内容类似于如何rowSums允许na.rm=TRUE参数,以便得到这个结果:
x1 x2 x3 x4
1 …Run Code Online (Sandbox Code Playgroud) 我在一些.Rnw文件中的文本中包含一些sweave表达式.以下段落包含两个sweave表达式.我可以使用什么正则表达式来查找每个表达式中的R代码.所以正则表达式应该能够找到mean(mtcars$mpg)和/或summary(lm(mpg ~ hp + drat, mtcars))
Lorem ipsum dolor sit amet,consectetur adipisicing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\ {Sexpr平均(mtcars $ MPG)}.Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.Duis aute irure dolor in repreptderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.Excepteur sint occaecat cupidatat\Sexpr {summary(lm(mpg~hp + drat,mtcars))} non non proident,sunt in culpa qui officia deserunt mollit anim id est laborum.
我已经编写了下面的代码来生成一个矩阵,其中包含对我来说相当复杂的模式.在这种情况下,我通过反复试验确定完成矩阵中有136行.
我可以编写一个函数来预先计算矩阵行的数量,但函数会有点复杂.在该示例中,矩阵中的行数=((4*3 + 1)+(3*3 + 1)+(2*3 + 1)+(1*3 + 1))*4.
有没有一种简单有效的方法在R中创建矩阵而不用硬连接矩阵语句中的行数?换句话说,是否有一种简单的方法让R在使用for循环时根据需要简单地向矩阵添加一行?
我提出了一个解决方案,每次通过循环使用rbind,但这似乎有点复杂,我想知道是否有一个更容易的解决方案.
对不起,如果这个问题与之前的问题有关.我无法使用本网站上的搜索功能或今天使用互联网搜索引擎找到类似的问题,尽管我认为我在过去的某处发现过类似的问题.
下面是2组示例代码,一组使用rbind,另一组使用试验和错误预先设置nrow = 136.
谢谢你的任何建议.
v1 <- 5
v2 <- 2
v3 <- 2
v4 <- (v1-1)
my.matrix <- matrix(0, nrow=136, ncol=(v1+4) )
i = 1
for(a in 1:v2) {
for(b in 1:v3) {
for(c in 1:v4) {
for(d in (c+1):v1) {
if(d == (c+1)) l.s = 4
else l.s = 3
for(e in 1:l.s) {
my.matrix[i,c] = 1
if(d == (c+1)) my.matrix[i,d] = (e-1)
else my.matrix[i,d] = …Run Code Online (Sandbox Code Playgroud) 我很惊讶,missing似乎没有在一个被调用的函数中工作lapply.假设我有以下功能:
.add <- function(x, arg, ...) {
if (missing(arg)) {
arg <- 1
}
print(match.call())
return(x + arg)
}
wrapper <- function(l, arg, ...) {
return(lapply(l, .add, arg=arg, ...))
}
Run Code Online (Sandbox Code Playgroud)
设置arg明确的工作,如例外:
wrapper(list(x=1:10, y=1:10), arg=1)
#FUN(x = X[[1L]], arg = ..1)
#FUN(x = X[[2L]], arg = ..1)
#$x
# [1] 2 3 4 5 6 7 8 9 10 11
#
#$y
# [1] 2 3 4 5 6 7 8 9 10 11
Run Code Online (Sandbox Code Playgroud)
没有 …