Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

write, read lines as code into a csv file

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading