【问题标题】:Shiny app. breaks down after trying to process an imported csv file闪亮的应用程序。尝试处理导入的 csv 文件后崩溃
【发布时间】:2019-10-03 15:41:43
【问题描述】:

我有一个功能强大的闪亮应用程序,当我尝试将我的数据框导入为 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)) 
      }
   })

【问题讨论】:

  • 如果您可以发布应用程序崩溃时出现的错误消息会很有帮助。您可以将其添加到您的问题中吗?
  • 当然我还在代码中添加了第二个观察者
  • 该错误似乎表明您的表是NULL,因此无法返回任何列。您是否尝试在文件未上传时返回 d() 的值? if (is.null(inFile)){ d &lt;- dummy_df() } else {do_your_stuff }
  • 如果您从rhandsontable 库中检查函数get_col_types,它会检查数据是is.matrix 还是is.data.frameNULL 两者都不是,所以你必须明确告诉应用程序在 NULL 的情况下要做什么。 https://rdrr.io/cran/rhandsontable/src/R/misc.R
  • 您的mainPanel 期待rHandsontable 类型的输出。当您返回 NULL 时,rHandsontableOutput 无法呈现 NULL,因为它不是表。您可以 1) 返回一个空表 2) 如果为 NULL,则使用 uiOutput 返回消息,否则返回表。

标签: r shiny


【解决方案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)) 
      })


  }

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2018-07-24
    • 1970-01-01
    • 2015-05-28
    • 2023-04-11
    • 2014-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-26
    相关资源
    最近更新 更多