【问题标题】:$ operator is invalid for atomic vectors in Shiny$ 运算符对 Shiny 中的原子向量无效
【发布时间】:2017-12-04 02:47:16
【问题描述】:

我有以下代码可以在 R 解释器中按预期工作

recodeRule <- function(df, rules){
    for(i in 1:nrow(rules)){
        x <- which(df$key == rules$key[i] & df$score == rules[i, 'before'])
        df[x, 'score'] <- rules[i, 'after'] 
    }
    df
}
df <- data.frame(key = c(1,2,3,1,2,3), score = c(1,1,2,2,1,1))
rules <- data.frame(key = c(1,2), before = c(1,1), after = c(10,12))
recodeRule(df, rules)

但是,当在我的 Shiny 应用程序中放置相同的代码时,我得到“$ 运算符对原子向量无效”。回溯显示:

Stack trace (innermost first):
    113: which [Rcode/serverFiles/testScoring.R#30]
    112: recodeRule [Rcode/serverFiles/testScoring.R#30]
    111: <reactive:stuItems4Scoring> [Rcode/serverFiles/testScoring.R#35]
    100: stuItems4Scoring
     99: get
     98: <reactive:dimVarNms> [Rcode/serverFiles/testScoring.R#132]
     87: dimVarNms
     86: lapply
     85: listify
     84: choicesWithNames
     83: selectInput
     82: tagAppendChild
     81: selectizeIt
     80: selectizeInput
     79: renderUI [Rcode/serverFiles/testScoring.R#137]
     78: func
     77: origRenderFunc
     76: output$dimVars4Scoring
      1: runApp

函数嵌入在这个反应式表达式中。该函数的预期行为是读入并返回一个文件。如果读入第二个文件,它将修改该文件并返回它。

stuItems4Scoring <- reactive({
    validate(need(input$stuItems4Scoring != "", ""))    
    infile <- input$stuItems4Scoring
    if (!is.null(input$stuItems4Scoring)) {
        file <- load(input$stuItems4Scoring$datapath, e6)
    }
    ### Apply recode if requested
    if(!is.null(input$recodeRules)){
        rules <- recodeFile()
        file <- recodeRule(file, rules)
        return(file)
    } else {
        file
    }   
})

rules文件中读取的部分代码如下:

recodeFile <- reactive({
    validate(need(input$recodeRules != "", "")) 
    infile <- input$recodeRules
    if (is.null(infile)) return(NULL)      
    read.csv(infile$datapath)
})

如果我没有阅读recodeFile,则实际应用程序的相关部分将按预期工作,因为原始数据帧被返回。但是,当我阅读recodeFile 时,应该发生的是它应该修改数据(如上面提供的在 R 解释器中工作的工作示例)并返回修改后的文件。这是我的代码失败的地方,我不知道为什么。

回溯表明有问题的代码行位于recodeRule 函数中的which 语句中,但我根本看不到原因,因为此时我有点陷入困境。

感谢您的支持。

【问题讨论】:

  • 在运行时闪亮显示 df 和 rules 对象。我打赌你会对结果感到惊讶。两者之一将是原子向量。
  • load 不返回数据帧

标签: r shiny


【解决方案1】:

我在使用 rgl 包的 Shiny 中遇到了类似的问题。我在 server.R 中使用了 renderRglwidget() 函数,在 ui.r 文件中使用了 rglwidgetOutput()。 然后通过在 renderRglwidget() 函数的 expr 参数末尾添加一行来解决此问题。该行是:rglwidget()

【讨论】:

    猜你喜欢
    • 2014-05-15
    • 2013-07-14
    • 2021-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    相关资源
    最近更新 更多