0

I'm importing a folder of files according to the package eemR using the following:

library(data.table)
library(tidyverse)
library(eemR)

importIlliter <- function(file) {
  fluoroFrame <- fread(file) %>% as.data.frame()
  return(
    list(
      file = file,
      x = fluoroFrame[ -1, -1] %>% t() %>% as.matrix() %>% unname(),
      ex = fluoroFrame[ -1, 1] %>% as.numeric(),
      em = fluoroFrame[ 1, -1] %>% as.numeric()
    )
  )
}



folder <- file.path("C:/Users/ejs7c/OneDrive/Desktop/eemR/")

eems <- eem_read(folder, recursive = TRUE, import_function = importIlliter)

However, when I import the files, they are simply just called [[1]], [[2]].. and so on - Is there a way to make it so the files retain their names when I import them? i.e., one is called Raman520, and I would like it to be called that when it's imported as an eem (in the final line of the above code).

My eventual goal is to automate some functions within the eemR package, but I'm assuming I can still do that even if I let the files keep their names.

Thanks

dogman
  • 13
  • 2

1 Answers1

0

As you already use tidyverse a way to assign the file to name of list is

names(eems) <- map_chr(eems, pluck, "file")

A demo

library(purrr)

eems <- list(
  list(file = "file_1"),
  list(file = "file_2"),
  list(file = "file_3"),
  list(file = "file_4")
)

eems
#> [[1]]
#> [[1]]$file
#> [1] "file_1"
#> 
#> 
#> [[2]]
#> [[2]]$file
#> [1] "file_2"
#> 
#> 
#> [[3]]
#> [[3]]$file
#> [1] "file_3"
#> 
#> 
#> [[4]]
#> [[4]]$file
#> [1] "file_4"

names(eems) <- map_chr(eems, pluck, "file")

eems
#> $file_1
#> $file_1$file
#> [1] "file_1"
#> 
#> 
#> $file_2
#> $file_2$file
#> [1] "file_2"
#> 
#> 
#> $file_3
#> $file_3$file
#> [1] "file_3"
#> 
#> 
#> $file_4
#> $file_4$file
#> [1] "file_4"

eems[["file_1"]]
#> $file
#> [1] "file_1"

Created on 2021-05-24 by the reprex package (v2.0.0)

Sinh Nguyen
  • 4,277
  • 3
  • 18
  • 26
  • Thank you Sinh, do Iadd that at the very end of my code? – dogman May 24 '21 at 15:51
  • Yes add it to the very end of your current code. – Sinh Nguyen May 24 '21 at 15:52
  • Thank you; I did that, but it made the entire path the names of the variables – dogman May 24 '21 at 15:53
  • I do not understand the eems <- list portion of your example; does this stop it from just naming them the whole path when I eventually do the name function? I have a lot of samples, so I'd like to be able to do this without typing out all of the name values in the list function if possible – dogman May 24 '21 at 16:08
  • It has to be full path as what would happen if there are file with same name in different sub-directory? – Sinh Nguyen May 25 '21 at 03:40
  • My example is just a trying to replicate your `eems` list as you didn't share what's in your data by `dput`- for more information reference here https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Sinh Nguyen May 25 '21 at 03:46