【发布时间】: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 <- dummy_df() } else {do_your_stuff } -
如果您从
rhandsontable库中检查函数get_col_types,它会检查数据是is.matrix还是is.data.frame。NULL两者都不是,所以你必须明确告诉应用程序在 NULL 的情况下要做什么。https://rdrr.io/cran/rhandsontable/src/R/misc.R -
您的
mainPanel期待rHandsontable类型的输出。当您返回 NULL 时,rHandsontableOutput无法呈现 NULL,因为它不是表。您可以 1) 返回一个空表 2) 如果为 NULL,则使用uiOutput返回消息,否则返回表。