as.data.frame.default 中的错误:无法强制类“c(“reactiveExpr",“reactive")";到 Shiny 中的 data.frame [英] Error in as.data.frame.default: cannot coerce class "c("reactiveExpr", "reactive")" to a data.frame in Shiny

查看:123
本文介绍了as.data.frame.default 中的错误:无法强制类“c(“reactiveExpr",“reactive")";到 Shiny 中的 data.frame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having an issue with creating a data frame from a reactive in Shiny. The app allows a user to upload some data hence the dataset is reactive. It will then allow the user to select the input and output variables which will be passed onto a regression and a plot is generated. I keep getting an error stating:

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

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

I believe that its coming from my d.f variable which is supposed to be a dataframe of the user uploaded data. I have not been able to secure a workaround after scouring the internet. Any help would be appreciated. The code so far is below.

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)

推荐答案

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

As Chi Pak has mentioned, reactive expressions must contain () at the end. Changing d.f to d.f() fixes the issue.

这篇关于as.data.frame.default 中的错误:无法强制类“c(“reactiveExpr",“reactive")";到 Shiny 中的 data.frame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆