r - Error in UseMethod("xtable") -
i trying load multiple files , merge using reduce function... tried several options got same error got 'xtable' applied object of class "character"
server.r
library(shiny) shinyserver(function(input,output) { output$data <- renderui({ res <- lapply( 1:input$fnos, function(i) { fileinput(paste("file", i), "load file", accept=c( 'text/csv', 'text/comma-separated-values', 'text/tab-separated-values', 'text/plain','.csv','.tsv' ))} ) do.call(sidebarpanel,res) }) output$multi <- rendertable({ infile <- list( lapply(1:input$fnos, function(i) {input[[paste("file",i)]]}) )[[1]] # data frame names df <- (letters[1:input$fnos]) # trying use assign function create # different dataframes using read.csv (i in 1:input$fnos) {assign(df[i], read.csv(infile[[c(i,4)]]))} #merging using reduce function merged <- reduce(function(x,y) merge(x,y), list(df)) # getting error here }) })
ui.r
library(shiny) shinyui(fluidpage( titlepanel(title="multiple file load"), sidebarlayout( sidebarpanel( numericinput("fnos","files input",1)), mainpanel(uioutput("data"), tableoutput("multi")) ) ))
you reduce wrong thing. assuming have 2 files 'file1.csv', 'file2.csv' should work larger number of files well:
> write.csv(structure(list(id = 1:3, x = 4:6), .names = c("x", "y"), class = "data.frame", row.names = c(na, -3l)), 'file1.csv', row.names=false) > write.csv(structure(list(id = 1:2, y = 9:10), .names = c("x", "z"), class = "data.frame", row.names = c(na, -2l)), 'file2.csv', row.names=false) > dfs <- lapply(list.files(pattern="file[1-2]\\.csv$"), read.csv) > dfs [[1]] x y 1 1 4 2 2 5 3 3 6 [[2]] x z 1 1 9 2 2 10
you can use either reduce
:
> reduce(merge, dfs) x y z 1 1 4 9 2 2 5 10
or better simple do.call
:
> do.call(merge, dfs) x y z 1 1 4 9 2 2 5 10
if want translate app can use this:
reduce( merge, lapply( paste('file', 1:input$fnos), function(x) read.csv(input[[x]]$datapath) ))
just remember checking if input set.
Comments
Post a Comment