I have a date frame with a column containing lines of code.
f1 <- 'paste0("f","q","D",collapse = "")'
f2 <- "q <- rep(NA,10)
for(i in 1:10){
q[i] <- 5+i
}
q"
df <- data.frame(code=c(f1,f2), n=1:2 , lg=c(TRUE,FALSE))
the code can be run
eval(str2expression(df$code[1]))
eval(str2expression(df$code[2]))
how to correctly save it in a csv and then read this frame so that this code can be run again
pth <- "C:\\Users\\....\\Desktop\\f.csv"
# how to correctly save
write.csv(m, pth, row.names = F, quote = F)
# how to correctly read
df2 <- read.csv(pth)
# so the code can be run again
eval(str2expression(df2$code[1]))
eval(str2expression(df2$code[2]))
>Solution :
You need to use quotes when you save the file.
write.csv(df, pth, row.names = F)
Then it should be a valid CSV file that you can read in later. Otherwise you will be creating an invalid CSV file (you have commas in your code so that is not an unambiguous separator).