I need to import a random number of mp4 files into k *.docx patient profiles. I set up the mp4 files as a list, subsetted the list for the desired mp4 files, extracted file name information, and created a for loop to iterate over each file.
###########################################################################
###########################################################################
##### generate list of available case images (mp4)
fileLst2 <- list.files(path = rawData,
pattern = "*.mp4",
all.files = FALSE,
full.names = TRUE,
recursive = TRUE,
ignore.case = FALSE,
include.dirs = TRUE,
no.. = FALSE)
fileLst2 <- data.table(subset( fileLst2 , regexpr( "therapy logs", fileLst2 ) %in% -1))
fileLst2 <- setDT(tstrsplit(as.character(fileLst2$V1), "/", fixed=TRUE))[]
fileLst2[,USUBJID := ifelse( substr(V10,1,2) %in% c("00","10") , substr(V10,1,7) , "" )]
fileLst2 <- merge(fileLst2 , temp , by = "USUBJID" , all.x = TRUE)
fileLst2 <- subset( fileLst2 , USUBJID != "" )
fileLst2[,fileName := paste0( V1,"/",V2,"/",V3,"/",V4,"/",V5,"/",V6,"/",V7,"/",V8 ,"/",V9,"/",V10)]
fileLst2 <- subset( fileLst2 , select = -c(V1 , V2 , V3 , V4 , V5 , V6 , V7))
#### most of the code to export one mp4 per page -- this code works fine for importing jpg images
lst <- subset( fileLst2 , ordId %in% jj)
lst[,jj := seq(.N)]
doc <- body_add_break(my_doc)
for(m in 1:max(lst$jj)){
uu <- subset( lst , jj %in% m)
# generate cosmetic details for describing each mp4
qqu <- uu
qqu <- setDT(tstrsplit(as.character(qqu$fileName), "/", fixed=TRUE))[]
line1 <- paste( max(qqu$V8), "MP4 for" , max(qqu$V9) )
line1 <- gsub("CXR Repos","CXR Repositioning" , line1)
line1 <- gsub("CXR Arrival","CXR on Arrival" , line1)
line1 <- gsub("case Image ","Case Image " , line1)
line2 <- paste( "File Name:" , max(qqu$V10) )
fpar1 <- fpar(ftext( paste(line1) , prop = textFmt1 ))
fpar2 <- fpar(ftext( paste(line2) , prop = textFmt1 ))
line3 <- paste( "")
fpar3 <- fpar(ftext( paste(line3) , prop = textFmt1 ))
my_doc <- body_add_fpar( my_doc , fpar1)
my_doc <- body_add_fpar( my_doc , fpar2)
my_doc <- body_add_fpar( my_doc , fpar3)
my_doc <- body_add_video( my_doc , src = uu$fileName , width = 6 , height = 6)
doc <- body_add_break(my_doc)
}
print(my_doc, target=paste0(qcOutput,"/","urine","/","urine_profile_",max(vv$USUBJID),".docx" ))