Create List of Files for Use in Function

I have a Shiny app where I can read multiple files. Now, I'd like to collect these file objects in a list to use them in a function called evalm. The function works fine for one selected file but fails when I select multiple files and attempt to use them in the evalm function.

The error I get is Warning: Error in gzfile: invalid 'description' argument

files <- readRDS(file$datapath) # this reads in multiple files

results <- MLeval::evalm(list(files)) # works ok with one file but fails on more than one file selected above

I followed the evalm vignette using the exact syntax https://cran.r-project.org/web/packages/MLeval/vignettes/introduction.pdf

I believe I need to pass in a list, in this case they are caret models saved as .RDS files. Any suggestions on getting this to work is appreciated!

The function readRDS(file) expects the argument file to contain the path to a file. Here, you seem to be giving a vector with multiple paths (file$datapath seems to be the column of a data frame). So you need to loop on the elements of file$datapath and call readRDS() on each one. In R, the functions lapply() and purrr::map() are made for this purpose:

file$contents <- lapply(file$datapath, readRDS)

Alexis, thanks, that works.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.