如何让我的 R Shiny 应用程序停止获取变量的无效类型(列表)?
How do I get my R Shiny app to stop getting an invalid type (list) for a variable?
这是我最关心的代码。
server <- function(input, output, session) {
df1 <- reactive({read.csv("input.csv")})
targets <- reactive({select(df1(), Lease.Sales, Retail.Sales, Net.Margin.Lease, Net.Margin.Retail)})
drops <- reactive({c("Lease.Sales", "Retail.Sales", "Net.Margin.Lease", "Net.Margin.Retail")})
df2 <- reactive({df1()[, !(names(df1()) %in% drops())]})
df <- reactive({lapply(df2(), function(x) {
x <- as.numeric(gsub('[$,]', '', x))
})})
#x <- reactive({as.matrix(df())})
#y <- reactive({as.matrix(targets()[1])})
#multivar_reg <- t(cov(y(), x()) %*% solve(cov(x())))
leaseType <- reactive({lm(as.matrix(targets()[1]) ~ as.matrix(df()))$coeff})
retailType <- reactive({lm(as.matrix(targets()[2]) ~ as.matrix(df()))$coeff})
leaseMargin <- reactive({lm(as.matrix(targets()[3]) ~ as.matrix(df()))$coeff})
retailMargin <- reactive({lm(as.matrix(targets()[4]) ~ as.matrix(df()))$coeff})
当它到达任何 lm
部分时,在本例中是任何 lease/retail types/margin,它会说以下内容。 invalid type (list) for variable 'as.matrix(df())'
我做错了什么?
lapply(...)
返回 list
,而不是 data.frame
。虽然有一种形式的 as.matrix
知道如何处理 data.frame
,但没有一种形式知道如何处理 list
。 (A data.frame
实际上是一个 list
,其中所有元素的长度都相同。考虑到这一点,它在某种意义上“显然”是矩形的,因此它可以直接且明确地转换为矩阵。不幸的是,list
不一定是矩形,因此没有理智的方法来假设转换为矩阵......所以他们不会尝试。)
您可以使用 as.data.frame(lapply(...))
来解决这个问题。
如果这不是一个简化的例子(如果是,谢谢),那么你大部分使用 reactive
是不必要的,只需使用 df1 <- read.csv(...)
并处理将其作为“正常”变量。
如果 CSV 文件可能会定期更改,那么您可能需要
df1 <- shiny::reactiveFileReader(3000, session, "input.csv", read.csv)
每 3000 毫秒检查一次文件更新(即修改时间)。如果检测到更改,则在此处使用 read.csv
来读取文件;如果你有特殊的阅读需求,你需要更新这个,也许用一个匿名函数来处理特殊的参数。
由于此版本仍然是反应式的,您仍将在其他地方使用 df1()
。
这是我最关心的代码。
server <- function(input, output, session) {
df1 <- reactive({read.csv("input.csv")})
targets <- reactive({select(df1(), Lease.Sales, Retail.Sales, Net.Margin.Lease, Net.Margin.Retail)})
drops <- reactive({c("Lease.Sales", "Retail.Sales", "Net.Margin.Lease", "Net.Margin.Retail")})
df2 <- reactive({df1()[, !(names(df1()) %in% drops())]})
df <- reactive({lapply(df2(), function(x) {
x <- as.numeric(gsub('[$,]', '', x))
})})
#x <- reactive({as.matrix(df())})
#y <- reactive({as.matrix(targets()[1])})
#multivar_reg <- t(cov(y(), x()) %*% solve(cov(x())))
leaseType <- reactive({lm(as.matrix(targets()[1]) ~ as.matrix(df()))$coeff})
retailType <- reactive({lm(as.matrix(targets()[2]) ~ as.matrix(df()))$coeff})
leaseMargin <- reactive({lm(as.matrix(targets()[3]) ~ as.matrix(df()))$coeff})
retailMargin <- reactive({lm(as.matrix(targets()[4]) ~ as.matrix(df()))$coeff})
当它到达任何 lm
部分时,在本例中是任何 lease/retail types/margin,它会说以下内容。 invalid type (list) for variable 'as.matrix(df())'
我做错了什么?
lapply(...)
返回list
,而不是data.frame
。虽然有一种形式的as.matrix
知道如何处理data.frame
,但没有一种形式知道如何处理list
。 (Adata.frame
实际上是一个list
,其中所有元素的长度都相同。考虑到这一点,它在某种意义上“显然”是矩形的,因此它可以直接且明确地转换为矩阵。不幸的是,list
不一定是矩形,因此没有理智的方法来假设转换为矩阵......所以他们不会尝试。)您可以使用
as.data.frame(lapply(...))
来解决这个问题。如果这不是一个简化的例子(如果是,谢谢),那么你大部分使用
reactive
是不必要的,只需使用df1 <- read.csv(...)
并处理将其作为“正常”变量。如果 CSV 文件可能会定期更改,那么您可能需要
df1 <- shiny::reactiveFileReader(3000, session, "input.csv", read.csv)
每 3000 毫秒检查一次文件更新(即修改时间)。如果检测到更改,则在此处使用
read.csv
来读取文件;如果你有特殊的阅读需求,你需要更新这个,也许用一个匿名函数来处理特殊的参数。由于此版本仍然是反应式的,您仍将在其他地方使用
df1()
。