【问题标题】:Error in as.data.frame.default: cannot coerce class "c("reactiveExpr", "reactive")" to a data.frame in Shinyas.data.frame.default 中的错误:无法将类“c(“reactiveExpr”,“reactive”)”强制转换为 Shiny 中的 data.frame
【发布时间】:2017-11-20 18:36:18
【问题描述】:

我在从 Shiny 中的响应式创建数据框时遇到问题。该应用程序允许用户上传一些数据,因此数据集是反应性的。然后它将允许用户选择输入和输出变量,这些变量将传递给回归并生成图。 我不断收到一条错误消息:

Error in as.data.frame.default: cannot coerce class "c("reactiveExpr", "reactive")" to a data.frame

我相信它来自我的 d.f 变量,它应该是用户上传数据的数据框。搜索互联网后,我无法找到解决方法。 任何帮助,将不胜感激。到目前为止的代码如下。

library(shiny)
library(triangle)
library(readxl)
library(leaps)
library(coefplot)
library(relaimpo)
library(data.table)
library(XLConnect)
library(xlsx)

ui <- fluidPage(

  titlePanel("Hi"),
  sidebarLayout(position = "left",
                sidebarPanel(
                  conditionalPanel(condition = "input.tabs1==1",
                                   tags$style(type='text/css', ".well { max-width: 20em; }"),
                                   # Tags:
                                   tags$head(
                                     tags$style(type="text/css", "select[multiple] { width: 100%; height:10em}"),
                                     tags$style(type="text/css", "select { width: 100%}"),
                                     tags$style(type="text/css", "input { width: 19em; max-width:100%}")
                                   ),

                                   # Select filetype:
                                   selectInput("readFunction", "Function to read data:", c(
                                     # Base R:
                                     "read.table",
                                     "read.csv",
                                     "read.csv2",
                                     "read.delim",
                                     "read.delim2",
                                     "readWorksheet",
                                     "read_excel",
                                     "read.xlsx"

                                   )),

                                   # Argument selecter:
                                   htmlOutput("ArgSelect"),

                                   # Argument field:
                                   htmlOutput("ArgText"),

                                   # Upload data:
                                   fileInput("file", "Upload data-file:"),

                                   # Variable selection:
                                   htmlOutput("varselect"),

                                   br(),

                                   uiOutput("invar"),
                                   br(),
                                   uiOutput("outvar"),

                                   textInput("name","Dataset name:","Data")),
                  conditionalPanel(condition = "input.tabs1==2",
                                   #fileInput('file', 'Choose file to upload.'),
                                   #selectizeInput('invar',"Select Invar", choices = varnames, multiple = TRUE),
                                   #selectizeInput('outvar',"Select Outvar", choices = predictors, multiple = FALSE),
                                   radioButtons('LM',"Select Regression",choices = list("LM" = 1, "LM2" = 2),selected = 1)




                )),
                mainPanel(
                  tabsetPanel(id="tabs1",
                              tabPanel("Data File",value = 1,tableOutput("table")),
                              tabPanel("Plot",value=2,tableOutput("Data"),plotOutput("Plot"))       
                  )
                )

))



server<-function(input, output) {
  options(shiny.maxRequestSize=30*1024^2)

  ### Argument names:
  ArgNames <- reactive({
    Names <- names(formals(input$readFunction)[-1])
    Names <- Names[Names!="..."]
    return(Names)
  })

  # Argument selector:
  output$ArgSelect <- renderUI({
    if (length(ArgNames())==0) return(NULL)

    selectInput("arg","Argument:",ArgNames())
  })

  ## Arg text field:
  output$ArgText <- renderUI({
    fun__arg <- paste0(input$readFunction,"__",input$arg)

    if (is.null(input$arg)) return(NULL)

    Defaults <- formals(input$readFunction)

    if (is.null(input[[fun__arg]]))
    {
      textInput(fun__arg, label = "Enter value:", value = deparse(Defaults[[input$arg]])) 
    } else {
      textInput(fun__arg, label = "Enter value:", value = input[[fun__arg]]) 
    }
  })


  ### Data import:
  Dataset <- reactive({
    if (is.null(input$file)) {
      # User has not uploaded a file yet
      return(data.frame())
    }

    args <- grep(paste0("^",input$readFunction,"__"), names(input), value = TRUE)

    argList <- list()
    for (i in seq_along(args))
    {
      argList[[i]] <- eval(parse(text=input[[args[i]]]))
    }
    names(argList) <- gsub(paste0("^",input$readFunction,"__"),"",args)

    argList <- argList[names(argList) %in% ArgNames()]

    Dataset <- as.data.frame(do.call(input$readFunction,c(list(input$file$datapath),argList)))
    return(Dataset)
  })

  # Select variables:
  output$varselect <- renderUI({

    if (identical(Dataset(), '') || identical(Dataset(),data.frame())) return(NULL)

    # Variable selection:    
    selectInput("vars", "Variables to use:",
                names(Dataset()), names(Dataset()), multiple =TRUE)            
  })

  # Show table:
  output$table <- renderTable({

    if (is.null(input$vars) || length(input$vars)==0) return(NULL)

    return(Dataset()[,input$vars,drop=FALSE])
  })

  #################################################################################

  varnames<-reactive({
    names(input$readFunction)
  })

  output$invar<-renderUI({
    selectizeInput('invar',"Select Invar", choices = names(Dataset()), multiple = TRUE)
  })

  output$outvar<-renderUI({
    selectizeInput('outvar',"Select Outvar", choices = names(Dataset()), multiple = TRUE)

  })

  d.f<-Dataset

  output$Plot <- renderPlot({    

    SelectedVars <- input$invar
    vartopredict <- input$outvar

      fmla <- reformulate(SelectedVars, response = vartopredict)
      pred.model=lm(fmla,d.f)
      plot(pred.model)
      abline(a=0,b=1,col="red")


  })
}

  shinyApp(ui = ui, server = server)

【问题讨论】:

  • 尝试使用d.f() 而不是d.freactive 值不是 data.frame。要访问 reactive 值中的 data.frame,请使用 ()
  • @ChiPak 兄弟我不知道怎么感谢你!!!我花了很长时间试图弄清楚。非常感谢。

标签: r rstudio shiny


【解决方案1】:

正如 Chi Pak 所提到的,反应式表达式的末尾必须包含 ()。将 d.f 更改为 d.f() 可以解决此问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 2017-07-09
    • 1970-01-01
    相关资源
    最近更新 更多