![](/img/trans.png)
[英]The downloadablePlot Shiny Module breaks down my shiny app upon launching
[英]Shiny app. breaks down after trying to process an imported csv file
我有一個功能強大的閃亮應用程序,當我嘗試將數據框導入為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))
})
}
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.