1

以下代码在我的 Mac 上运行良好,使用 CRAN R:

delta_scores <- function(df, data_var) {
  # Use Hadley's new non-standard evaluation helpers to compute differences in 
  # the symbol passed through data_var from Session 1 to 2. Assumes an ID column
  # in df that groups units of measurement.

  # For the RHS:
  quo_data_var <- enquo(data_var)
  # For the LHS, we need yet another step (basically a string)
  name_data_var <- quo_name(quo_data_var)

  df %>% select(ID, Session, !!quo_data_var) %>% 
    # NSE spread stopped working on my windows machine!
    spread(Session, !!quo_data_var) %>% 
    # Note use of := instead of plain = to support NSE
    transmute(ID=ID, !!name_data_var := `2`-`1`)
}

test_df <- data_frame(ID=c(1,2,3,1,2,3), 
                      Session=c(1,1,1,2,2,2), 
                      Measure=c(1,2,3,1,1,4))

delta_scores(test_df, Measure)

但是当我在 Windows、Microsoft R Open 3.4.2、dplyr 0.7.3 上运行它时,我得到:

Error: Invalid column specification

spread注意:用.替换很容易修复spread_('Session', name_data_var)。有趣的是,select调用工作正常(我的真实数据框有很多列)。我担心 dplyr 的 NSE 无法在给定环境中工作的更大问题。

看着调试器堆栈跟踪已经足够令人生畏了,我决定先在这里寻求帮助。任何关于正在发生的事情的想法或关于如何调试它的想法都非常感谢!

4

1 回答 1

0

这在 tidyr 的更高版本中得到了解决。确认在 tidyr 0.7.2 中工作。在 0.7 版本中添加了对 Hadley 新 NSE(非标准评估)系统的支持。

于 2017-11-21T20:01:43.157 回答