闪亮的应用程序。尝试处理导入的csv文件后崩溃

时间:2019-09-15 17:57:39

标签: 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)),
                 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)) 
      }
   })

1 个答案:

答案 0 :(得分:1)

我已使用here提供的答案中的代码,并将其更新为包括.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"))

对于闪亮的应用程序,用户界面部分可以相同。下面是更新的服务器代码。

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)) 
      })


  }

希望这会有所帮助。