Reputation: 339
I would like to open an Excel file in directory "dld" that starts with "RPT". I've tried the below, but I keep getting an error:
Error in file(file, "rt") : invalid 'description' argument
I'm guessing it has something to do with the code coming from a read.csv and I'm trying to adapt it to read.table.
dld <- "C:/Users/Me/Downloads/"
filename <- paste(dld, "RPT_", sep = "")
file <- read.table(dir(dirname(filename), full.names=T, pattern=paste("^", basename(filename), sep="")))
Ideas? Any direction/help would be greatly appreciated.
Upvotes: 0
Views: 1030
Reputation: 34753
First, as @joran mentioned, there are several tools for reading .xlsx
or xls
files directly (all of which are covered here).
As to your question of finding a partially matched file name, I would use grepl
as follows:
#get all file names in the directory
flz <- list.files("C:/Users/Me/Downloads/")
#find those that start with RPT (or otherwise match your pattern)
my_excel <- flz[grepl("^RPT", flz)]
#(make sure here that you've identified a unique file)
Finally, read the file:
library(readxl)
read_excel(my_excel) #(specifying whichever options as needed)
Upvotes: 1