Reputation: 958
I'm wondering if it would be possible to draw out out the filename from a file.choose() command embedded within a read.csv call. Right now I'm doing this in two steps, but the user has to select the same file twice in order to extract both the data (csv) and the filename for use in a function I run. I want to make it so the user only has to select the file once, and then I can use both the data and the name of the file.
Here's what I'm working with:
data <- read.csv(file.choose(), skip=1))
name <- basename(file.choose())
I'm running OS X if that helps, since I think file.choose() has different behavior depending on the OS. Thanks in advance.
Upvotes: 6
Views: 28069
Reputation: 1
df <- read.csv(file.choose(), header = TRUE, stringsAsFactors = TRUE) You can also use this for choose csv file in your current system
Upvotes: 0
Reputation: 384
use this:
df = read.csv(file.choose(), sep = "<use relevant seperator>", header = T, quote = "")
seperators are generally comma ,
or fullstop .
Example:
df = read.csv(file.choose(), sep = ",", header = T, quote = "")
#
Use :
df = df[,!apply(is.na(df), 2, all)] # works for every data format
to remove blank columns to the left
Upvotes: 0
Reputation: 25736
Why you use an embedded file.choose()
command?
filename <- file.choose()
data <- read.csv(filename, skip=1)
name <- basename(filename)
Upvotes: 12