I have a large number of patient data that I'd like to use to produce health reports for. I'd like to produce one file for each patient with the file name being the subject ID.
The data looks like this:
library(tidyverse)
library(reshape2)
set.seed(3344)
subject_id <- as.factor(1:10)
var1 <- rnorm(n = length(subject_id), mean = 100, sd = 12)
var2 <- rnorm(n = length(subject_id), mean = 50, sd = 10)
var3 <- rnorm(n = length(subject_id), mean = 200, sd = 100)
df <- data.frame(subject_id, var1, var2, var3)
df <- df %>%
melt(., id.vars = "subject_id", measure.vars = c(2:4))
I've done this once before, producing a pdf file that had one subject per page, however with our new system we need the files in .jpg format, one file per subject, and each file named the subject ID. When doing this to the large PDF file I did it like this:
plot <- ggplot(df, aes(x = variable, y = value, fill = variable)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.8)) +
facet_wrap(~subject_id)
library(plyr)
pdf("Plot Example.pdf")
dlply(df, .(subject_id), function(x) plot %+% x)
dev.off()
However, I'm stuck as to how to produce each subject as a single file, with their ID as the file name, and the file type to be a .jpg. Any help would be greatly appreciated.
pdf/dev.offtry usingggsave(). Include it to the function you are passing todlply. Or you could also move thepdf/dev.offinside that function as well.ggsave()function to thedlplycall I'm making, however no matter where I add it I get an error:dlply(df, .(subject_id), function(ggsave(x)) plot %+% x). Withinggsave()I know I can specify that I want a.jepghowever this wouldn't solve the issue of printing one file per subject with each file name being the subject ID. Or would it? Thanks!