r - using a trycatch block to read a csv and an excel file -
i trying give both options of uploading csv , excel sheet in shiny in 1 function. using read.csv read csv file , readworksheetfromfile xlconnect package read excel files. using trycatch block still errors when csv file passed on readworksheetfromfile function. tryblock looks like
filedata <- reactive({ infile <- input$templatedfile if (is.null(infile)) { # user has not uploaded file yet return(null) } importedfile = trycatch({ read.csv(infile$datapath,stringsasfactors=false) }, = { readworksheetfromfile(infile$datapath,sheet=1,check.names=false) }) })
i think best served implementing switch
statement. way don't waste computation time trying read file csv when not. want fall option in case uploads file neither csv or excel file. here implementation of filedata
function.
filedata <- reactive({ infile <- input$templatedfile if(is.null(infile)){ return(null) } ext <- file_ext(infile$name) importedfile <- switch(ext, csv = read.csv(infile$datapath, stringsasfactors=false), xlsx = , xls = readworksheetfromfile(infile$datapath, sheet=1, check.names=false), stop("file extension not recognized")) })
here simple gist used verify works properly.
rungist("https://gist.github.com/cdeterman/a41cceadffa7907c505e")
Comments
Post a Comment