Let's say I have a list of filenames and I want to write a function to, for each filename, import the file and clean it. This is a natural use of lapply, along the lines of
function cleaning_function = function(filename){
df = read_csv(filename)
df = df %>% rename(id = old_id)
}
list_of_dfs = lapply(filenames, cleaning_function)
The problem is debugging. Let's say the column old_id did not exist in the csv file. My code will error.
But it's hard to debug the code now that it's in a function.
What are my options for working with this?
in Julia, you can wrap all your code inside the function in an @eval block
function cleaning_function(filename)
@eval begin
df = read_csv(filename)
df = df %>% rename(id = old_id)
end
end
now df appears in global scope and i can see what's going on when I get an error.
Is there any way to do the equivalent in R, an easy way to run commands in global scope for the purposes of inspecting intermediate variables?