sana
sana

Reputation: 25

How to read in directory name as user input, without double backslashes?

I want to write generic code which takes input from user, but when I run:

directory_name <- readline(prompt=" Enter the directory")

Enter the directory C:\Users\ANKIT\Documents

It shows directory name like this (with double-backslashes)

directory_name "C:\\Users\\ANKIT\\Documents"

And how to use this directory name to load .csv file?

Upvotes: 0

Views: 62

Answers (2)

akrun
akrun

Reputation: 887231

We can use paste0 to paste the file with the object directory_name

directory_name <- readline(prompt=" Enter the directory")
dat <- read.csv(paste0(directory_name, "\\mpg_data.csv"))

Or with paste and specify the sep

dat <- read.csv(paste(directory_name, "mpg_data.csv", sep="\\")) 
dim(dat)
#[1] 79 16

Upvotes: 0

rg255
rg255

Reputation: 4169

Use read.csv and paste0:

directory_name <- readline(prompt=" Enter the directory")

Enter C:\Users\griffinevo\temporaryRfiles

read.csv(paste0(directory_name, "\\filename.csv"))

Upvotes: 0

Related Questions