1

我使用 expss 包在 R 中定义了一些表函数来自动制表。我的一张表想要显示类别的案例或百分比,然后是平均值。平均值可以基于相同的类别变量,也可以定义为另一个变量。总的来说,代码完美无缺。对于某些变量,尽管我一直在努力解决错误代码“do.call(data.frame, c(x, alis)) 中的错误:变量名称限制为 10000 字节”

此表的代码

  Table2 = function (Q, banner=banner, caption , Q.mean, ddata=d, questlab=dquest, mis.val=999) {
  x_totaln<-eval(substitute(x),ddata)
  x_totaln[is.na(eval(substitute(Q),ddata))]<-NA
  if(missing(Q.mean))
  {Q_mean<-eval(substitute(Q),ddata)}
  else 
  {Q_mean<-eval(substitute(Q.mean),ddata)}
  Q_mean[Q_mean==mis.val]<-NA
  if(missing(caption))
  {caption<-eval(substitute(var_lab(Q_mean)),questlab)}
  eval.parent(substitute(
    { 
      banner %>%
        tab_cells (x_totaln) %>%
        tab_stat_cases(total_row_position = c("none"),label = 'N') %>%
        tab_cells (Q) %>%
        tab_stat_cases(total_row_position = c("none"),label = 'N') %>%
        tab_stat_cpct(total_row_position = c("none"), label = '%') %>%
        tab_cells (Q_mean) %>%
        tab_stat_mean(label = 'Mean') %>%
        tab_pivot (stat_position = "inside_rows") %>%  
        drop_c ()  %>%
        custom_format2()  %>%
        set_caption(caption)
    }
  ))
}

这段代码总体上运行完美。

Table2(Q8_cat)

在此处输入图像描述

对于某些变量,尽管它会生成错误代码

Table2(age_cat,Q.mean=age,caption="Your age at the start of the programme?")
 Error in do.call(data.frame, c(x, alis)) : 
  variable names are limited to 10000 bytes 
19.
do.call(data.frame, c(x, alis)) 

同时在代码中包含变量再次完美

Table2test = function () {
  x_totaln<-eval(substitute(x),d)
  x_totaln[is.na(eval(substitute(age_cat),d))]<-NA
  Q_mean<-eval(substitute(age),d)
  Q_mean[Q_mean==999]<-NA
      banner %>%
        tab_cells (x_totaln) %>%
        tab_stat_cases(total_row_position = c("none"),label = 'N') %>%
        tab_cells (age_cat) %>%
        tab_stat_cases(total_row_position = c("none"),label = 'N') %>%
        tab_stat_cpct(total_row_position = c("none"), label = '%') %>%
        tab_cells (Q_mean) %>%
        tab_stat_mean(label = 'Mean') %>%
        tab_pivot (stat_position = "inside_rows") %>%  
        drop_c ()  %>%
        custom_format2()  %>%
        set_caption("Your age at the start of the programme?")
  
}

在此处输入图像描述

有什么建议吗?或者任何人知道为什么会发生错误?

谢谢

4

1 回答 1

1

在某些情况下,当您使用substitute变量时,它们表示为结构。在这种情况下,表达式中没有变量名,只有值:tab_cells(structure(c(22, 23, 22, 23, ... many numbers)))。我们尝试在结果表中使用这个长表示作为名称。但是 R 对名称的长度有限制。而这里的功能失败了。解决方案非常简单 - 我们将始终设置变量标签,我们将使用它们作为名称。所以下面的代码运行没有任何错误:

Table2 = function (Q, banner=banner, caption , Q.mean, ddata=d, questlab=dquest, mis.val=999) {
    x_totaln<-eval(substitute(x),ddata)
    x_totaln[is.na(eval(substitute(Q),ddata))]<-NA
    var_lab(x_totaln) = "Total" # add label for total
    if(missing(Q.mean))
    {Q_mean<-eval(substitute(Q),ddata)}
    else 
    {Q_mean<-eval(substitute(Q.mean),ddata)}
    Q_mean[Q_mean==mis.val]<-NA
    if(missing(caption))
    {caption<-eval(substitute(var_lab(Q_mean)),questlab)}
    eval.parent(substitute(
        { 
            banner %>%
                tab_cells (x_totaln) %>%
                tab_stat_cases(total_row_position = c("none"),label = 'N') %>%
                tab_cells (Q) %>%
                tab_stat_cases(total_row_position = c("none"),label = 'N') %>%
                tab_stat_cpct(total_row_position = c("none"), label = '%') %>%
                tab_cells ("|" = Q_mean) %>% # "|" suppress label for mean
                tab_stat_mean(label = 'Mean') %>%
                tab_pivot (stat_position = "inside_rows") %>%  
                drop_c ()  %>%
                custom_format2()  %>%
                set_caption(caption)
        }
    ))
}
于 2021-03-28T21:44:21.710 回答