相关疑难解决方法(0)

Cast function argument as a character string?

超级快速的问题......

如何获取某个特定函数(用户定义的)参数并将其转换为字符串?

如果是一个简单的例子,

foo <- function(x) { ... }
Run Code Online (Sandbox Code Playgroud)

我想简单地返回x的对象名称.所以,

foo(testing123)
Run Code Online (Sandbox Code Playgroud)

返回"testing123"(并且testing123可能只是一些随机数字向量)

如果之前已经问过这个问题,我会道歉 - 搜索,但找不到它!谢谢!!

r

18
推荐指数
2
解决办法
2万
查看次数

提取在R中用点传递的数据帧的名称

可以使用deparse(substitute())组合来提取函数内部的参数名称,就像这个函数一样

names_from_dots <- function(...) {
    deparse(substitute(...))
 }

data(iris)
data(swiss)

names_from_dots(iris)
#[1] "iris"
names_from_dots(swiss)
#[1] "swiss"
Run Code Online (Sandbox Code Playgroud)

提取传入...(点)参数的data.frame的名称.

但是如何提取传递的多个data.frames的每个名称

names_from_dots(swiss, iris)
[1] "swiss"
names_from_dots(iris, swiss)
[1] "iris"
Run Code Online (Sandbox Code Playgroud)

这只返回第一个对象的名称.

r ellipsis

7
推荐指数
2
解决办法
304
查看次数

点 - 点参数的范围

我对dot-dot-dot参数的范围有疑问.考虑以下函数`foo =

foo <- function(x, ...){
   require(classInt);
   intvl = classIntervals(x, ...);
   return(intvl);
 }
Run Code Online (Sandbox Code Playgroud)

该功能适用​​于以下调用

x = runif(100, 0, 100);
y1 = foo(x, n = 5, style = 'quantile');
y2 = foo(x, style = 'equal');
Run Code Online (Sandbox Code Playgroud)

但是当我尝试使用style ='fixed'参数时,我需要一个fixedBreaks参数

y3 = foo(x, style = 'fixed', fixedBreaks = seq(0, 100, 20))
Run Code Online (Sandbox Code Playgroud)

eval中的错误(expr,envir,enclos):...列表不包含2个元素

请注意,以下工作完美

y5 = classIntervals(x, style = 'fixed', fixedBreaks = seq(0, 100, 20))
Run Code Online (Sandbox Code Playgroud)

我怀疑这与范围规则有关,但一直无法指责它.任何有关这方面的帮助将非常感谢.

编辑.我拼凑了一个更简单的黑客,使它工作.我认为这是一个match.call问题,因为style ='pretty'存在同样的问题.快速查看代码显示这些是match.calls的两种样式,所以很可能这是错误的来源.无论如何,这是我提出的黑客攻击

foo2 <- function(x, ...){
  require(classInt);
  y = list(...); y$var = x;
  intvl = do.call('classIntervals', y);
}

y6 …
Run Code Online (Sandbox Code Playgroud)

r scoping

5
推荐指数
1
解决办法
1139
查看次数

如何用点 (...) 参数覆盖硬编码的函数参数?

我正在尝试创建一个具有一组默认值的绘图函数,并且可以通过使用该函数plot在点 ( ...) 参数内接受的任何参数来灵活地更改这些值。一个例子:

PlotIt <- function(x, y, ...) {
  plot(x, y, type = "l", asp = 1, ...)
}

x <- 1:10
y <- 10:1

PlotIt(x = x, y = y)
# Returns a plot
PlotIt(x = x, y = y, asp = NA)
# Error in plot.default(x, y, type = "l", asp = 1, ...) : 
#  formal argument "asp" matched by multiple actual arguments
Run Code Online (Sandbox Code Playgroud)

该错误自然是因为我尝试将 asp 参数两次传递到plot. 到目前为止,我最好的笨拙尝试是做出一个 if-else 语句来考虑到这一点(该方法是从此处修改的): …

r

5
推荐指数
1
解决办法
475
查看次数

使用点点点(...)表示从data.table对象的自定义函数返回的列

我想用来...指示要从对象的自定义函数返回的变量data.table。这是一个最小的可复制示例:

library(data.table)
d = data.table(mtcars)

getvar = function(...){
  return(d[,.(xyz = mean(hp), ...), cyl])
}

getvar(mpg, cyl, disp)
Run Code Online (Sandbox Code Playgroud)

在错误[.data.table(d,(N = .N,...),CYL):对象'CYL'未找到

我希望得到的是:

d[,.(xyz = mean(hp), mpg, cyl, disp), cyl]

 #    cyl       xyz  mpg cyl  disp
 # 1:   6 122.28571 21.0   6 160.0
 # 2:   6 122.28571 21.0   6 160.0
 # 3:   6 122.28571 21.4   6 258.0
 # 4:   6 122.28571 18.1   6 225.0
 # 5:   6 122.28571 19.2   6 167.6
Run Code Online (Sandbox Code Playgroud)

任何人都可以分享他们的解决方案吗?

r function ellipsis data.table

4
推荐指数
1
解决办法
94
查看次数

标签 统计

r ×5

ellipsis ×2

data.table ×1

function ×1

scoping ×1