0

is there a way to load a csv file in R and define the variable automatically from the filename?  So, if you have a csv file called 'hello',  can I load it in R and create the df/var. without defining it? 

So, rather than define hello in the load procedure: hello=read("filepath/hello"); instead we have read("filepath/hello") but include a command to create and name a variable that is the same name of the file name (hello in this example?)

David Specht
  • 7,784
  • 1
  • 22
  • 30
  • 1
    One could write a function that reads the file, names the variable and uses `<<-` to add that name in the higher namespace but I strongly advice against trying. Keep you data entering the function via arguments and leave it via return values and do not automatically name variables. – Bernhard Aug 12 '20 at 12:09
  • Hi, thanks. Could you sjow me how? I think I understand the risk but in this case it is mitigated. – Montana_innit96 Aug 12 '20 at 12:10

2 Answers2

0

I have advised you not to do this in any real world scenario, but if it helps understanding the concepts, this is not a complete solution but the important ingredients.

<<- the superassignement operator in the enclosing environment, which in the following case is the global namespace:

rm(hello) # just in case, ignore warning if there is any

dont <- function(){
    hello <<- 42
}

print(hello)
dont()
print(hello)

So you can define values in the enclosing environment within a function without a return value.

The name of that variable does not have to be fixed (as hello in the example above) but can depend on an argument to that function as in

dontdothis <-  function(name){
   eval(parse(text = paste0(name, " <<- 42")))
}

dontdothis("frederik")
print(frederik * 2)

You will need to add the file operations and some small detail but that is how one could do it. You may want to google for namespaces and environments and assignment operators in R to get a better understanding of the details in there.

Worthwhile short read to distinguish between global environment and enclosing environment: Why is using `<<-` frowned upon and how can I avoid it?

Bernhard
  • 4,272
  • 1
  • 13
  • 23
0

Depending on why you would like to do this I would offer you another solution:

I suppose your problem is that you have a big folder with a lot of csv files and you would like to load them all and give the variables the name of the csv file without typing everything manually.

Then you can run

> setwd("C:/Users/Testuser/testfiles")
> file_names <- list.files()
> file_names
[1] "rest"      "test1.txt" "test2.csv" "test3.csv"

where as path you use the path where all your csv files are stored.

Then if there are stored any other files and you only would like to have the csv files we have to grep them with regex

> file_names_csv <- file_names[grepl(".csv",file_names)]
> file_names_csv
[1] "test2.csv" "test3.csv"

Now we load them with a for loop and assign them to a variable that is named as the corresponding csv file

for( name in file_names_csv){
  assign(paste(name, sep=""), read.csv(file = paste(name, sep="")))
}

And we have

> test2.csv
  test
1 1234
> test3.csv
  test
1 2323

You can also gsub the .csv away before you load the data by

> file_names_csv <- gsub(".csv","",file_names_csv )
> file_names_csv
[1] "test2" "test3"

So basically you have exactly what you have asked for without using global variables.

Methamortix
  • 70
  • 10