1

Currently I'm trying to read various datasets located on the same location on my PC.

They end up looking something like this (except the URL is much larger):

Data_1 <- read_xlsx('C:/Users/XXXX/XXXXX/XXXXXX/XXXXXX/Data_1.xlsx')
Data_2 <- read_xlsx('C:/Users/XXXX/XXXXX/XXXXXX/XXXXXX/Data_2.xlsx')
Data_3 <- read_xlsx('C:/Users/XXXX/XXXXX/XXXXXX/XXXXXX/Data_3.rds')
Data_4 <- read_xlsx('C:/Users/XXXX/XXXXX/XXXXXX/XXXXXX/Data_4.rds')

I find this is impractical and hard to read. Im trying an alternative like the following:

URL="'C:/Users/XXXX/XXXXX/XXXXXX/XXXXXX/"
Data_1 <- read_xlsx(paste(URL)'Data_1.xlsx')
Data_2 <- read_xlsx(paste(URL)'Data_2.xlsx')
Data_3 <- readRDS(paste(URL)'Data_3.rds')
Data_4 <- readRDS(paste(URL)'Data_4.rds')

However I cant get it to work, it's probably something simple but i can't figure it out. How do you handle this?

JC Cantu
  • 21
  • 2
  • 1
    It should be `paste0(URL, 'Data_1.xlsx')`. First, you are closing the `paste` function before assigin the filename. Second because `paste` will add a space between the strings, and `paste0` will just concatenate them with no separator. – Daniel R Jul 22 '20 at 17:43

1 Answers1

1

There are different ways you could do this.

First: set your working directory and then access them.

URL = "C:/Users/XXXX/XXXXX/XXXXXX/XXXXXX"
# set working directory
setwd(URL)

# read your data
Data_1 <- read_xlsx('Data_1.xlsx')

Second: Use path to create a path

Data_1 <- read_xlsx(file.path(URL, 'Data_1.xlsx'))

Third: more intuitive to use paste0

Data_1 <- read_xlsx(paste0(URL,"/, 'Data_1.xlsx'))
Not_Dave
  • 491
  • 2
  • 8