r - 在 R Manipulate 中添加许多复选框作为列表/向量

标签 r user-interface plot statistics

我正在使用 {manipulate}向图中添加多个复选框 - 正在绘制的每条线对应一个复选框。由于所需的数量不是预先定义的,我试图找出一种方法将所有复选框作为向量或列表传递给 manipulate功能。
文档似乎暗示了一些东西:

manipulate(_expr, ...)

_expr : Expression to evalulate.

... : One or more named control arguments (i.e. slider, picker, checkbox, or button), or a list containing named controls.


我认为出路是根据需要传递一个包含尽可能多的复选框的列表。但是具体应该如何进行呢?

最佳答案

下面的例子应该做你想做的:

# Define function for ploting
example <- function(...){
  plot(cars)
  i <- 1
  for (my.control in list(...)) {
    if (my.control) abline(0, i)
    i <- i+1
  }
}

# Define your controls
custom.args <- list()
for (i in 1:5) {
  custom.args <- append(custom.args, list(checkbox(FALSE, paste("Ceckbox", i))))
}
names(custom.args) <- paste("checkbox", 1:5, sep="")

# Pass everything to manipulate
library(manipulate)
manipulate(
  eval(parse(text=sprintf('example(%s)', 
                          paste(names(custom.args), collapse=","))
             )),
  custom.args)

我不知道是否有更简洁的方法将控制值传递给绘图函数,但至少这是有效的。

关于r - 在 R Manipulate 中添加许多复选框作为列表/向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16926674/

相关文章:

r - tidyr::pivot_longer() 函数不适用于将列从逻辑转换为整数的 tibble

r - 循环针织选项

python - 简单的 GUI Windows 拖放

c++ - 此 Windows 应用程序使用什么 API/SDK?

ggplot2 geom_order中的反向堆叠顺序

R从ctree(partykit)中提取内部节点信息和拆分

plot.window(...) 中的 R 错误需要有限的 'xlim' 值

r - 无法让 alpha 与 ggplot2::geom_sf 在 sf linestring 对象上一起使用

python - 查询雪花数据库不起作用,因为没有选择事件仓库

Java图像不会随着鼠标点击而移动?