0

How can I convert a character string such as "c(1:10)" into an actual vector of integers? My issue is that I need to read a bunch of files using read.xlsx from the package xlsx, but I need to read different rows for each file. I've got a separate file that I can read into the workspace as a data.frame (call it "MyFile") that lists in one column the names of the files and, in another column, which rows to read, but I don't know how to convert the character string into the numbers I need. I'd use regex to extract it, but that sounds like more work than I want since it's a mishmash of everything from "c(1:10)" to "c(2, 4, 6:8, 21)".

I've tried:

 Files <- list()
 for (i in 1:nrow(MyFiles)){
       Files[[i]] <- read.xlsx(MyFiles$File[i], 
                              sheetName = MyFiles$Tab[i],
                              rowIndex = MyFiles$Row[i])
 }

but R doesn't know what to do with the row specification since it's currently reading it as a character string.

shirewoman2
  • 1,842
  • 4
  • 19
  • 31

1 Answers1

1

It's a shame there isn't a better way (as far as I know) than eval(parse(text= but that's what I use in these situations.

input <- "c(1:10)"
output <- eval(parse(text=input))
output
# [1]  1  2  3  4  5  6  7  8  9 10
Jonathan Carroll
  • 3,897
  • 14
  • 34