R 闪亮的应用程序。尝试处理导入的csv文件后发生故障

R 闪亮的应用程序。尝试处理导入的csv文件后发生故障,r,shiny,R,Shiny,我有一个功能闪亮的应用程序,当我尝试将我的数据框作为csv导入而不是在应用程序中创建它时,它会崩溃。我有不工作的代码被注释掉了。 数据: DF2 = data.frame(agency_postcode = factor(rep(c(12345,45678,24124,32525,32325),2)), car_group=factor(rep(c("Microcar","City car","Supermini","Compact","SUV"),2)),

我有一个功能闪亮的应用程序,当我尝试将我的数据框作为csv导入而不是在应用程序中创建它时,它会崩溃。我有不工作的代码被注释掉了。 数据:

DF2 = data.frame(agency_postcode = factor(rep(c(12345,45678,24124,32525,32325),2)),
                 car_group=factor(rep(c("Microcar","City car","Supermini","Compact","SUV"),2)),
                 transmission=factor(rep(c("automatic","manual"),5)))
csv:

write.csv(DF2,"C:/Users/User/Documents/Test//cars2.csv", row.names = FALSE)
错误:

Warning: Error in get_col_types: Unsupported object type: NULL Can't extract column types.
应用程序:

#ui.r
library(shiny)
library(rhandsontable)

ui <- fluidPage(

  titlePanel("RHandsontable"),
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose CSV File",
                accept = c(
                  "text/csv",
                  "text/comma-separated-values,text/plain",
                  ".csv")
      ),
      actionButton("sr","Search")
    ),
    mainPanel(
      rHandsontableOutput("test")
    )
  )
)
#server.r
library(shiny)
library(rhandsontable)

server <- function(input, output) {

   # Assigning blank values to reactive variable as all the values need to be listed first
   values <- reactiveValues(postcode = "",cargroup = "",date="",days="",transmission="",driver_age="",tabledata = data.frame())
   d<-reactive({
      inFile <- input$file1

      if (is.null(inFile))
         return(NULL)

      DF<- read.csv(inFile$datapath,stringsAsFactors = T)
      for(i in 1:ncol(DF)){
         DF[,i]<-as.factor(DF[,i])

      }
      DF
   })
   observeEvent(values$postcode,{
      DF2 = data.frame(agency_postcode = factor(rep(c(12345,45678,24124,32525,32325),2)),
                       car_group=factor(rep(c("Microcar","City car","Supermini","Compact","SUV"),2)),
                       transmission=factor(rep(c("automatic","manual"),5)))
      # When the user selects any value from the dropdown, filter the table and update the value of reactive df
      if(values$postcode!=""){
         values$tabledata <- d()[ which(d()$agency_postcode ==values$postcode), ]
      }else{
         # When the postcode value is blank, meaning the user hasn't selected any, the table 
         # will render without the third column
         values$tabledata <- d()[,-3]
      }

   })

   observeEvent(values$cargroup,{
      DF2 = data.frame(agency_postcode = factor(rep(c(12345,45678,24124,32525,32325),2)),
                       car_group=factor(rep(c("Microcar","City car","Supermini","Compact","SUV"),2)),
                       transmission=factor(rep(c("automatic","manual"),5)))
      values$tabledata <- d()
      # When the user selects any value from the dropdown, filter the table and update the value of reactive df
      if(values$cargroup!=""){
         values$tabledata <- d()[ which(d()$car_group ==values$cargroup), ]
      }else{
         # When the cargroup value is blank, meaning the user hasn't selected any, the table 
         # will render without the third column
         values$tabledata <- d()[,-3]
      }

   })

   # Observer for changes made to the hot
   observeEvent(input$sr,{
      col <- input$test$changes$changes[[1]][[2]]
      # Changes made in first column
      if(col==0){
         values$postcode <- input$test$changes$changes[[1]][[4]]
      }
      # Changes made in second column
      if(col==1){
         values$cargroup <- input$test$changes$changes[[1]][[4]]
      }
   })

   # Render the hot object
   output$test <- renderRHandsontable({
      rhandsontable(values$tabledata[1,], rowHeaders = NULL, width = 550, height = 300)%>%
         hot_col(colnames(values$tabledata)) 
   })


}
***基于NULL 2进行编辑

    output$test <- renderUI({
      if (is.null(input$file1)){
         return("Add file")
      }
      else{
      rhandsontable(values$tabledata[1,], rowHeaders = NULL, width = 550, height = 300)%>%
         hot_col(colnames(values$tabledata)) 
      }
   })

我使用了我提供的答案中的代码,将其更新为包含.csv上传。希望这有帮助

用于创建df和保存.csv的代码段

test <- data.frame(agency_postcode = factor(rep(c(12345,45678,24124,32525,32325),2)),
                   car_group=factor(rep(c("Microcar","City car","Supermini","Compact","SUV"),2)),
                   transmission=factor(rep(c("automatic","manual"),5)))
write.csv(test,paste0("C:/Users/",Sys.getenv("USERNAME"),"/Desktop/Sample.csv"))
对于闪亮的应用程序,ui部分可以是相同的。下面是更新的服务器代码

server <- function(input, output) {

  # Assigning blank values to reactive variable as all the values need to be listed first
  values <- reactiveValues(postcode = "",cargroup = "",tabledata = data.frame(), sourcedata = data.frame())

# Let's add another reactive df called sourcedata. This will have our parent data
# The dataframe table data will be the parsed data passed to create handsontable object

  values$sourcedata <- data.frame(agency_postcode = factor(rep(c(12345,45678,24124,32525,32325),2)),
                                 car_group=factor(rep(c("Microcar","City car","Supermini","Compact","SUV"),2)),
                                 transmission=factor(rep(c("automatic","manual"),5)))

  observe({
    inFile <- input$file1

    if (is.null(inFile))
      return(NULL)

    sourceData <- read.csv(inFile$datapath,stringsAsFactors = TRUE)

    sourceData$agency_postcode <- as.factor(sourceData$agency_postcode)
    sourceData$car_group <- as.factor(sourceData$car_group)
    sourceData$transmission <- as.factor(sourceData$transmission)

# if any .csv files are uploaded, update the value of sourceData from the hardcoded dataframe
    values$sourcedata <- sourceData
    values$tabledata <- sourceData[,-3]
  }
  )

    observeEvent(values$postcode,{
      DF2 = values$sourcedata
      # When the user selects any value from the dropdown, filter the table and update the value of reactive df
      if(values$postcode!=""){
        values$tabledata <- DF2[ which(DF2$agency_postcode ==values$postcode), ]
      }else{
        # When the postcode value is blank, meaning the user hasn't selected any, the table 
        # will render without the third column
        values$tabledata <- DF2[,-3]
      }

    })

    observeEvent(values$cargroup,{
      DF2 = values$sourcedata
      # When the user selects any value from the dropdown, filter the table and update the value of reactive df
      if(values$cargroup!=""){
        values$tabledata <- DF2[ which(DF2$car_group ==values$cargroup), ]
      }else{
        # When the cargroup value is blank, meaning the user hasn't selected any, the table 
        # will render without the third column
        values$tabledata <- DF2[,-3]
      }

    })

    # Observer for changes made to the hot
    observeEvent(input$test$changes$changes,{
      col <- input$test$changes$changes[[1]][[2]]
      # Changes made in first column
      if(col==0){
        values$postcode <- input$test$changes$changes[[1]][[4]]
      }
      # Changes made in second column
      if(col==1){
        values$cargroup <- input$test$changes$changes[[1]][[4]]
      }
    })

    # Render the hot object
      output$test <- renderRHandsontable({
        rhandsontable(values$tabledata[1,], rowHeaders = NULL, width = 550, height = 300)%>%
          hot_col(colnames(values$tabledata)) 
      })


  }

希望这有帮助。

如果您可以发布应用程序崩溃时出现的错误消息,这将非常有用。你能把这个添加到你的问题中吗?当然,我也在代码中添加了第二个观察者。这个错误似乎表明你的表是空的,所以不可能返回任何列。当文件未上载时,是否尝试返回值d?如果是.nullinfle{d如果您从rhandsontable库中检查函数get_col_types,它会检查数据是.matrix还是.data.frame.NULL。NULL两者都不是,因此您必须明确告诉应用程序在NULL情况下该怎么做。https://rdrr.io/cran/rhandsontable/src/R/misc.Ryour mainPanel需要rHandsontable类型的输出。返回NULL时,rHandsontableOutput无法呈现NULL,因为它不是表。您可以1返回空表2如果为NULL,则使用uiOutput返回消息,否则返回表。