I would like to use a data.frame returning function from dplyr to read in data from Excel files whose location I want to easily configure. Here I'm presenting my problem using a simplified get_table() function and two generated data.frames. In reality the get_table() function fetches data from a server and parses it.
When calling the function from dplyr all data.frame results should be combined. Here is the simplified code:
files <- read.table(header=T, text='
type filename
A A_table
B B_table
')
A_table <- read.table(header=T, text='
area observations
K1 5
K2 9
')
B_table <- read.table(header=T, text='
area observations
K1 23
K2 28
K3 1
')
get_table <- function(name) {
return(get(name))
}
I can read the files with lapply:
list <- as.vector(files[,2])
t <- lapply(list, get_table)
do.call("rbind", t)
And combine the results into:
area observations
1 K1 5
2 K2 9
3 K1 23
4 K2 28
5 K3 1
I would however want to lear to do the same in dplyr-style doing something like this (but working - this doesn't):
files %>%
select(filename) %>%
rowwise() %>%
get_table()