简体   繁体   中英

How do I read upload file and dropbox file in the same app

I was upgrading an app which was developed by my ex-colleague. The app was develop to read file from dropbox. I am trying to add on a function which enable the user to upload the latest file from their desktop and use that file afterward.

Here is the code that was written by my ex-colleague which shows how the file was read from dropbox:

  if(drop_exists(path = file.path(outputDir,paste(name,".csv",sep="")))==TRUE){
    # data = data.frame(drop_read_csv(file.path(outputDir,paste(name,".csv",sep=""))))
    data = data.frame(drop_read_csv(file = paste0(outputDir,"/",name,".csv"), header=TRUE, sep=","), stringsAsFactors = FALSE)
    data[,"Timestamp"] = as.Date(data[,"Timestamp"],"%Y-%m-%d")
  } else { data = data.frame(read.csv("Risk Heat Map.csv", header = TRUE, sep=","), stringsAsFactors = FALSE)
  data[,"Timestamp"] = as.Date(data[,"Timestamp"],"%Y-%m-%d")}
  return(data)
}

Then I added these:

fileInput("newriskregister",'Upload Risk Register',accept=c('text/csv','text/comma-separated-values,text/plain','.csv')),

and

observe({
    if (!is.null(input$newriskregister)){
    risk$data =data.frame(upload(), stringsAsFactors = FALSE)}
  
  })

and

upload=function(){

  data <- data.frame(read.csv(input$newriskregister$datapath, header = TRUE, sep=","), stringsAsFactors = FALSE)
  data[,"Timestamp"] = as.Date(data[,"Timestamp"],"%Y-%m-%d")
  
}

on different area on my codes. The problems appear when I upload the file, the app crashes immediately. but if I change the code over the upload() function like below, its working.

upload=function(){

  data = data.frame(read.csv("Risk Heat Map.csv", header = TRUE, sep=","), stringsAsFactors = FALSE)
  data[,"Timestamp"] = as.Date(data[,"Timestamp"],"%Y-%m-%d")
  
  return(data)
}

The Risk Heat Map.csv is another file which is on the dropbox right now, but its not what I wanted. I wanted to create the file uploaded locally, can anybody help to diagnose? Much appreciated.

Try this

upload=function(){
  req(input$newriskregister)
  file <- input$newriskregister
  data <- data.frame(read.csv(file$datapath, header = TRUE, sep=","), stringsAsFactors = FALSE)
  data[,"Timestamp"] = as.Date(data[,"Timestamp"],"%Y-%m-%d")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM