I'm using knitr withR to build charts showing metrics from various tests I've performed on a piece of software. The data is stored in a SQL server table. These tests produce the same categories of data but in different files so I'm using a loop to build the charts.
Here's my set up:
(SQLConn_remote
is a function included in an internally used R package. Just trust me that the db connection is not the issue.)
<<setup, echo=FALSE>>=
db <- SQLConn_remote(DBName = "DATABASE", ServerName = "SERVER")
df <- sqlQuery(db, "select * from TABLE", stringsAsFactors = FALSE)
versions<-unique(df[order(df$Number), ][,2])
df$Version <- factor(df$Version, levels = versions)
files <- unique(df$FileName)
@
Here's the loop:
for (i in 1:6){
g <- ggplot(subset(df, FileName == files[i]), aes(x=Version, y=Value, group=FileName))
g <- g + geom_line(size=.25) + geom_point(aes(shape = Build), size = 1.2, colour = 'red') +
scale_shape_manual(values=c(1,15)) + ggtitle(files[i]) +
facet_grid(Category ~ ., scales = "free", space = "fixed", labeller = label_value) +
ylab("Value") + xlab("Version") + expand_limits(y=0)
g
}
However, when I compile the PDF, no charts are generated. If I take the g
at the end and put it on the other side of the final "}
" then it does print a single chart for the very last file (as expected).
Any idea or thoughts on why the charts not print inside the for loop?
EXTRAS: I didn't think it was very relevant to include an example of the SQL data but if you think differently, leave a comment and I'll add a basic example.
EDIT: Here's a script to create a table you can use with the chart script.
FileName <- c('File1', 'File1', 'File1', 'File2', 'File2', 'File2', 'File1', 'File1', 'File1', 'File2', 'File2', 'File2', 'File1', 'File1', 'File1', 'File2', 'File2', 'File2', 'File1', 'File1', 'File1', 'File2', 'File2', 'File2')
Version <- c('1.0.1', '1.0.12', '1.0.12', '1.0.1', '1.0.1', '1.0.1', '1.0.15', '1.0.15', '1.0.15', '1.0.15', '1.0.15', '1.0.15', '1.0.17', '1.0.17', '1.0.17', '1.0.17', '1.0.17', '1.0.17', '1.0.15', '1.0.15', '1.0.15', '1.0.15', '1.0.21', '1.0.21')
Category <- c('Category1', 'Category2', 'Category3', 'Category1', 'Category2', 'Category3', 'Category1', 'Category2', 'Category3', 'Category1', 'Category2', 'Category3', 'Category1', 'Category2', 'Category3', 'Category1', 'Category2', 'Category3', 'Category1', 'Category2', 'Category3', 'Category1', 'Category2', 'Category3')
Value <- c(10, 11, 12, 18, 19, 20, 12, 11, 10, 20, 18, 19, 10, 11, 12, 18, 19, 20, 12, 11, 10, 20, 18, 19)
Date <- c('2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00')
Number <- c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4)
Build <- c('Release', 'Release', 'Release', 'Release', 'Release', 'Release', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration')
df <- data.frame(FileName, Version, Category, Value, Date, Number, Build)
Here's the chunk
opts_chuck$set(fig.path='figure/minimal-',fig.align='center', fig.show='hold',fig.width=10,fig.height=5.5)
options(replace.assign=TRUE,width=90)