Get file name from read.csv (file.choose ())

I am wondering if it is possible to pull the file name from the file.choose () command built into the read.csv call. Now I am doing this in two stages, but the user must select the same file twice in order to extract both the data (csv) and the file name for use in the function I perform. I want to make the user select the file only once, and then I can use both the data and the file name.

Here is what I work with:

data <- read.csv(file.choose(), skip=1)) name <- basename(file.choose()) 

I am running OS X if this helps, as I think file.choose () has a different behavior depending on the OS. Thanks in advance.

+4
source share
2 answers

Why are you using the built-in file.choose() command?

 filename <- file.choose() data <- read.csv(filename, skip=1) name <- basename(filename) 
+10
source

use this:

 df = read.csv(file.choose(), sep = "<use relevant seperator>", header = T, quote = "") 

delimiters are usually commas , or fullstop .

Example:

 df = read.csv(file.choose(), sep = ",", header = T, quote = "") 
#

Using:

 df = df[,!apply(is.na(df), 2, all)] # works for every data format to remove blank columns to the left 
0
source

Source: https://habr.com/ru/post/1395830/


All Articles