简体   繁体   中英

passing directory name as a parameter

My current directory is c:/users/akshay/Documents

But all my data is in the directory "specdata" whose path address is c:/users/akshay/Documents/specdata

when i type these commands separately in console it works successfully.

path <- "C:/Users/akshay/Documents"
directory <- "specdata"
setwd(paste(path, directory, sep="/", collapse=NULL))

But when i use it in function like this it wont change my working directory.

pollutantmean <- function(directory){
directory <- character(1)
path <- character(1)
path <- "C:/Users/akshay/Documents"
setwd(paste(path, directory, sep="/", collapse=NULL))    
}

But when i pass

>pollutantmean("specdata")

It wont change my working directory why is it so? what is the problem?

Maybe try returning the paste. Also, you don't need the character() functions.

pollutantmean <- function(directory){
   path <- "C:/Users/akshay/Documents"
   return(paste(path, directory, sep="/", collapse=NULL))
   }

pollutantmean("specdata")

Output:

> pollutantmean("test")
[1] "C:/Users/akshay/Documents/test"

Change directory:

pollutantmean<-function(directory){ + path<-"C:/Users/akshay/Documents" + setwd(paste(path,directory,sep="/",collapse=NULL)) + }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM