首页 > 解决方案 > R闪亮部署 - 替换rowr cbind.fill

问题描述

我正在 Heroku 上部署一个闪亮的应用程序。

但是 buildpack 不支持 rowr,因为 R 的版本与此包不兼容。

如何用基本 R 或 dplyr 函数替换 rowr::cbind.fill ?

我试图查看函数中的内容,但我不清楚:

function (..., fill = NULL) 
{
    inputs <- list(...)
    inputs <- lapply(inputs, vert)
    maxlength <- max(unlist(lapply(inputs, len)))
    bufferedInputs <- lapply(inputs, buffer, length.out = maxlength, 
        fill, preserveClass = FALSE)
    return(Reduce(cbind.data.frame, bufferedInputs))
}

问这个问题的另一种方法是在 dplyr 中是否有以下问题的解决方案,我需要 fill = NA ?

cbind 一个不同长度的向量到一个数据帧

标签: rherokushiny

解决方案


这是一种检测每列中缺失元素数量的解决方案,将其附加NAs 并将其绑定在一起。我假设这些列存储在一个列表中:

# test data
test_data <- list(c1 = rep(1, 5),
                  c2 = rep(2, 7),
                  c3 = rep(3, 4))

# find the longest column
longest_column <- max(unlist(lapply(test_data, length)))

# calculate how many NAs need to be added
number_append <- lapply(test_data, function(x) longest_column - length(x))

# append the columns
data_appended <- lapply(seq_len(length(test_data)), function(i) {
  c(test_data[[i]], rep(NA, number_append[[i]]))
})

# combine the columns
data_combined <- do.call("cbind", data_appended)


推荐阅读