Rshiny gives error after loading rpart.object type using .RData file

I have saved the .Rdata file, which contains 3 R objects:

1. Vector
2. Character String
3. rpart.object (I figured out that this object in .RData only creating issue, as if I remove this object from .RData file, Shiny app works fine.)

Whenever I load the .RData file and refresh the application Rshiny gives error below:

Error in .rs.getShinyFunction(params$name, params$where) : attempt to apply non-function

In order to avoid above issue, I tries following options:

  • Load the .RData in Global environment.

        load(infile$datapath,.GlobalEnv)
    
  • Load the .RData in New Environment.

    LoadToEnvironment <- function(RData, env = new.env())       { load(RData, env)  
     return(env) }  
    

    e <- LoadToEnvironment("D:Demo NBAAddOnPropensity.R")
    val_modtyp <- e$val_modtyp
    val_model <- e$val_model
    val_b <- e$val_b

  • Load data Using attach()

  • Code:

    require(shinydashboard)||install.packages("shinydashboard"); library(shinydashboard)  
    require(shiny)||install.packages("shiny"); library(shiny)  
    require(shinyjs)||install.packages("shinyjs"); library(shinyjs) 
    
    
    ui <- fluidPage(
      useShinyjs(),
      extendShinyjs(text = "shinyjs.refresh = function() { location.reload(); }"),
      fluidPage(id="Q1",useShinyjs(),
    
                dashboardPage(dashboardHeader(title="Test",titleWidth=400),
                              dashboardSidebar(),
    
                              dashboardBody (
    
    
                                tabItem("PMData",
                                        fileInput('filepm', 'Choose Data to Upload',accept = c(".R")),
                                        uiOutput('ui.PM2'),
                                        actionButton("savepm","Save"),
                                        uiOutput("ui.PM3")
                                )
                              )
                )
    
      ))
    
    server <- function(session,input,output){
    
      hide("savepm")
    
      dfPM <<- data.frame(Category=character(),
                          PredictiveModel=character(),
                          OfferIdentifier=character(),
                          stringsAsFactors=FALSE)
    
    
    
    
      LoadToEnvironment <<- function(RData, env = new.env())
      {  
        load(RData, env)  
        return(env) }
    
      val_choices <- reactive({
        if (is.null(input$filepm)){
          return()      
        }
        infile <- input$filepm    
        e <- LoadToEnvironment(infile$datapath)
        e$val_b
      }) 
    
    
    
      observeEvent(input$filepm,{
    
        useShinyjs()
        if (is.null(input$filepm)){
          return()      
        }
    
        output$ui.PM2 <- renderUI ({
          selectInput("offered",label= "Offered Test",choices = val_choices(),
                      selected = NULL)
        })
        show("ui.PM2")
        show("savepm")
      })
    
      val_pmfile <- reactive({
        if (is.null(input$filepm)){
          return()      
        }
        infile <- input$filepm
        infile$datapath
      }) 
    
      TempPredmodDF <- reactive({
        if(is.null(input$offered))
        {
          return()
        }else{
          data.frame(Category="Test",
                     PredictiveModel=val_pmfile(),
                     OfferIdentifier=input$offered,
                     stringsAsFactors=FALSE)}
      })
    
      observeEvent(input$savepm,
    {  
      useShinyjs()
    
      tempPMdf <- TempPredmodDF()
    
    
      if(nrow(dfPM[dfPM$Category==tempPMdf$Category,]) == 0)
      {
        dfPM <<- rbind(dfPM,tempPMdf)
      }else
      {
        getidx <- as.numeric(which( dfPM[,1] == tempPMdf$Category ))
    
        dfPM[getidx,2] <<- tempPMdf$PredictiveModel
    
        dfPM[getidx,3] <<- tempPMdf$OfferIdentifier
    
      }
    
      output$ui.PM3 <- renderTable({
        dfPM},include.rownames=FALSE)
    
      hide("ui.PM2")
      hide("savepm")
      show("ui.PM3")
    
    })
    
    }
    
    
    app <- shinyApp(ui,server)
    
    runApp(app,port = 7000,launch.browser = getOption("shiny.launch.browser", interactive()))
    
    链接地址: http://www.djcxy.com/p/38304.html

    上一篇: 在R中的函数内保存单个对象:RData文件大小非常大

    下一篇: 使用.RData文件加载rpart.object类型后,Rshiny发生错误