评估传递给R中函数的参数

时间:2016-03-16 20:39:49

标签: r lazy-evaluation

我正在尝试评估传递给R中函数的参数并将它们存储在对象中。无论我尝试什么,如果我使用变量而不是变量的值分配参数,R会不断给我变量名。

ar <- list()


f2 <- function(name, ..., env = parent.frame()) {
  arguments <- match.call()[-1]
  arguments <- arguments[setdiff(names(arguments), c("name", "env"))]
  env$ar[name] <- arguments
}

calibration <- 0.8

f2("sample", calibration = calibration) 

ar

给出

$sample
calibration

而不是

$sample
0.8

我不知道如何解决这个问题。

2 个答案:

答案 0 :(得分:1)

试试这个单行。请注意,ar无需在运行f3之前定义,如果已定义,那么它将被覆盖 - appendmodifyList可用于代替不同的行为(未显示):

f3 <- function(name, ..., env = parent.frame()) {
         env$ar <- setNames(list(list(...)), name)
}

# test
calibration <- 0.8
solution <- "ohoh"
f3("sample", calibration = calibration, solution = solution)

,并提供:

> ar
$sample
$sample$calibration
[1] 0.8

$sample$solution
[1] "ohoh"

答案 1 :(得分:0)

好吧,如果人们对未来感兴趣,我找到了解决方案:

Sub Duplicate()
Dim rngData As Range
Dim cell As Range
Dim cell2 As Range

Set rngData = Range("P3:P19, P56:P58, P39:P42, P21:P25, P27:P37, P39:P42, P39:P42, P44:P54, M25:M76, B69:B77, B66:E67, B51:B64, H44:H47, D44:D47, H42, H33:H40, D33:D42, H31, D28:D31, H28:H29, D5:D8" & Cells(Rows.Count, "B").End(xlUp).Row)

rngData.Font.Color = vbBlack

For Each cell In rngData
    If cell.Font.Color = vbBlack Then
        For Each cell2 In rngData
            If cell = cell2 And cell.Address <> cell2.Address Then
                cell.Font.Color = vbRed
                cell2.Font.Color = vbRed
            End If
        Next
    End If
Next


Set rngData = Nothing

Application.ScreenUpdating = True
End Sub