1

I'm having trouble getting a function to save separate data frames. It keeps overwriting the name of a variable in the function. The code I have is

LFStable = function(LFS_number, vector_number, titles_number){
LFS_number <- get_cansim_vector(vector_number,
start_time = as.Date(startdate),
end_time = today(),
refresh = FALSE)

Temp <- data.frame(get_cansim_vector_info(vector_number))
Temp <- Temp %>% select(title, VECTOR, table) %>% separate(title,
        titles_number,";")

LFS_number <<- left_join(LFS_number,Temp, by = "VECTOR")
rm(Temp)
}
LFStable(LFS0063, V0063, title0063)

LFS_number is the number of the table I'm pulling from. vector_number is the list of vectors from Stats Canada I need from that table. titles_number is the name of the columns I need from the table.

It is making the table in the correct format but instead of naming the table "LFS0063" it names it "LFS_number" and then overwrites it when I run the function again for another table.

How can I get the table to be saved to the global environment with the name I gave it?

Thanks for reading and trying to help!

Edit: based on the comments from @MrFlick and @r2evans , I changed the code to

LFStable = function(LFS_number, vector_number, titles_number){
Temp_table <- get_cansim_vector(vector_number,
start_time = as.Date(startdate),
end_time = today(),
refresh = FALSE)

Temp <- data.frame(get_cansim_vector_info(vector_number))
Temp <- Temp %>% select(title, VECTOR, table) %>% separate(title,
            titles_number,";")

LFS_number <<- left_join(Temp_table,Temp, by = "VECTOR")
rm(Temp, vector_number)
}
LFStable(LFS0063, V0063, title0063)

Which produces the same problem as before. OR

Temp_table <- left_join(Temp_table,Temp, by = "VECTOR")
assign(LFS_number,Temp_table,envir=.GlobalEnv)
rm(Temp, vector_number)
}
LFStable(LFS0063, V0063, title0063)

which gives an error saying "invalid first argument". I created empty data frames with the LFS_number names to assign them to before running the function.

  • 5
    If you want to dynamically create variables with certain names, you'll need to use `assign()`. You cant use a string value to the left of a `<-` or `<<-`. But normally in R we avoid creating functions with side effects. It's better if the function returns the new data.frame rather than creating it in the gloabl environment as a side effect. This makes it much easier to write code that can use and automate calls to the function. – MrFlick Aug 31 '22 at 18:38
  • 5
    FYI, `<<-` will find the first instance of the LHS in the search tree and overwrite its contents with the RHS. In this case, you define `LFS_number` in the first line, overwriting whatever the user provided as the first argument. After this, using `LFS_number <<- ...` within the function will assign to the immediate version of that variable, it will not search outside of the local scope to find an object of the same name. – r2evans Aug 31 '22 at 18:41
  • 3
    It's really hard to help without a reproducible question and very specific coding. It looks like you're fairly new to SO; welcome to the community! If you want great answers quickly, it's best to make your question reproducible. This includes sample data like the output from `dput()` or `reprex::reprex()` and any libraries you are using. Check it out: [making R reproducible questions](https://stackoverflow.com/q/5963269). – Kat Sep 01 '22 at 01:28

0 Answers0