R/rewrite_modified_md.R

#' rewrite the modified test contents to markdown file
#'
#' @description rewrite the modified test contents to markdown file in the directory - modified.
#' @param idvec an id vector
#' @export
#' @examples
#'#rewrite_modified_md(c(1,3,4))
#'#rewrite_modified_md(201:203)
#'
#'

rewrite_modified_md <- function(idvec){

	mddir = "/Users/gabe/liguomin.teaching/Testbanks/medMicro-md-bank/"
	mddir0 = paste0(mddir,"cn/") # original md dir
	mddir1 <- paste0(mddir,"modified/") # the modified md directory
	setwd(mddir0)

	library(dplyr)
	my_db <- dplyr::src_sqlite(path="/Users/gabe/liguomin.teaching/Testbanks/testbankmd.sqlite")
	testbank_tb = dplyr::tbl(my_db,'testbank')

	# list all md files
	mdfiles = list.files(pattern = "md$")

	# get the md file name from id vectors
	allmd <- mdfiles[which(stringr::str_extract(mdfiles,"^[0-9]+") %in% idvec)]

	# get the content from the sqlite database
	write_md <- function(filename){
		# get the mdid in the sqlite
		mid <- stringr::str_extract(filename,"^[0-9]+")

		testbank_tb %>%
			filter(mdid==mid) %>%
			select(content) %>%
			# To pull down all the results use collect()
			collect %>%
			as.list %>%
			unlist %>%
			paste(.,collapse = "\n") %>%
			cat(., file= paste0(mddir1,filename))

	}

  sapply(allmd, write_md)

}
Gabegit/gmExam documentation built on May 6, 2019, 5:32 p.m.