Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: using previously determined variable as part of png file name

Tags:

variables

r

png

I am still new to R and I have searched around for a solution to my simple question, but I haven't found an answer that I've been able to get to work. I am looking to use a previously identified variable per data set, here variable=SNPname to include in script for automated generation of graph output in png format.

I am using this to generate a kmeans plot and have:

(cl <- kmeans(FilteredData[,6:7], 5, nstart=25))
png("C:/temp/$SNPnamegraph1.png")                 #SNPname to include in filename
plot(FilteredData[,6:7], col=cl$cluster)
points(cl$centers, col=1:5, pch=8)
dev.off()

where I want to include that variable in line 2 at the beginning of the file name. Is there a simple way to do this that I am just missing?

like image 365
Amanda Avatar asked Jan 18 '26 16:01

Amanda


1 Answers

Close, you're just missing the use of paste() and setwd()

setwd("C:/temp/") # use this to set where you want things saved
...
c1 <- kmeans...
png(paste(SNPname, " graph1.png", sep=""))
...

If it's in a loop of some kind, you might need to use SNPname[loop_var]

like image 123
Brandon Bertelsen Avatar answered Jan 21 '26 06:01

Brandon Bertelsen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!