Back to Fan’s Reusable R =Code table of content.
```r
```r
options(knitr.duplicate.label = 'allow')
Rmarkdown in Rstudio is easier for debugging, and allows for easier interaction with current workspace.
Rmarkdown has a conversion program: convert_ipynb.
# Generate Paths
spt_file_root = 'C:/Users/fan/R4Econ/summarize/summ/'
spt_file_name = 'ByGroupSummOne'
spt_file_full_ipynb = paste0(spt_file_root, spt_file_name, '.ipynb')
spt_file_full_rmd = paste0(spt_file_root, spt_file_name, '.rmd')
# Convert from IPYNB to RMD
file_nb_rmd = rmarkdown:::convert_ipynb(spt_file_full_ipynb)
st_nb_rmd = xfun::file_string(file_nb_rmd)
# Save RMD
fileConn <- file(spt_file_full_rmd)
writeLines(st_nb_rmd, fileConn)
close(fileConn)
# Convert to PDF and HTML
# rmarkdown::render(spt_file_full_rmd, "pdf_document")
# rmarkdown::render(spt_file_full_rmd, "html_document")
Search in folder for ipynb files, and then convert collectively to Rmd.
spt_root <- 'C:/Users/fan/Stat4Econ/'
# if the path contains these skip
spn_skip <- c('summarize', 'panel', 'support')
spn_skip <- c('')
# Group A
spt_desc <- paste0(spt_root, 'descriptive')
spt_srvy <- paste0(spt_root, 'survey')
spt_summ <- paste0(spt_root, 'summarystats')
ls_path_group_a <- c(spt_desc, spt_srvy, spt_summ)
# Group B
spt_prob <- paste0(spt_root, 'probability')
spt_pbct <- paste0(spt_root, 'probcts')
ls_path_group_b <- c(spt_prob, spt_pbct)
# All ls_path_group_use
ls_path_group <- c(ls_path_group_a, ls_path_group_b)
# Group To Use
ls_path_group_use <- ls_path_group
# ls_path_group_use <- ls_path_group_temp
# Get Path
ls_sfls <- list.files(path=ls_path_group_use, recursive=T, pattern=".ipynb", full.names=T)
# Excludes elements of path that have exclusion strings
if (spn_skip != '') {
ls_sfls <- ls_sfls[!grepl(paste(spn_skip, collapse = "|"), ls_sfls)]
}
# print
for (spt_file in ls_sfls) {
# 1. Check if the RMD file has been modified or is new, if neither, do not generate pdf html
# 2. store pdf and html files in a subfolder
# 3. main folder keeps only Rmd file
# 4. delete tex and other files
# print(paste0('spt_file:', spt_file))
st_fullpath_noname <- dirname(spt_file)
st_fullpath_nosufx <- sub('\\.ipynb$', '', spt_file)
st_file_wno_suffix <- sub('\\.ipynb$', '', basename(spt_file))
setwd(st_fullpath_noname)
spt_file_full_rmd = paste0(st_fullpath_nosufx, '.Rmd')
print(paste0('st_fullpath_noname:', st_fullpath_noname))
print(paste0('spt_file:', spt_file))
print(paste0('spt_file_full_rmd:', spt_file_full_rmd))
# Convert from IPYNB to RMD
file_nb_rmd = rmarkdown:::convert_ipynb(spt_file)
st_nb_rmd = xfun::file_string(file_nb_rmd)
# Save RMD
fileConn <- file(spt_file_full_rmd)
writeLines(st_nb_rmd, fileConn)
close(fileConn)
}
[1] "st_fullpath_noname:C:/Users/fan/Stat4Econ/descriptive"
[1] "spt_file:C:/Users/fan/Stat4Econ/descriptive/DataBasketball.ipynb"
[1] "spt_file_full_rmd:C:/Users/fan/Stat4Econ/descriptive/DataBasketball.Rmd"
[1] "st_fullpath_noname:C:/Users/fan/Stat4Econ/descriptive"
[1] "spt_file:C:/Users/fan/Stat4Econ/descriptive/MultipleVariables.ipynb"
[1] "spt_file_full_rmd:C:/Users/fan/Stat4Econ/descriptive/MultipleVariables.Rmd"
[1] "st_fullpath_noname:C:/Users/fan/Stat4Econ/descriptive"
[1] "spt_file:C:/Users/fan/Stat4Econ/descriptive/OneVariable.ipynb"
[1] "spt_file_full_rmd:C:/Users/fan/Stat4Econ/descriptive/OneVariable.Rmd"
[1] "st_fullpath_noname:C:/Users/fan/Stat4Econ/probability/binomial"
[1] "spt_file:C:/Users/fan/Stat4Econ/probability/binomial/binomial.ipynb"
[1] "spt_file_full_rmd:C:/Users/fan/Stat4Econ/probability/binomial/binomial.Rmd"
[1] "st_fullpath_noname:C:/Users/fan/Stat4Econ/probability"
[1] "spt_file:C:/Users/fan/Stat4Econ/probability/countrules.ipynb"
[1] "spt_file_full_rmd:C:/Users/fan/Stat4Econ/probability/countrules.Rmd"
[1] "st_fullpath_noname:C:/Users/fan/Stat4Econ/probability"
[1] "spt_file:C:/Users/fan/Stat4Econ/probability/lottery.ipynb"
[1] "spt_file_full_rmd:C:/Users/fan/Stat4Econ/probability/lottery.Rmd"
[1] "st_fullpath_noname:C:/Users/fan/Stat4Econ/probability"
[1] "spt_file:C:/Users/fan/Stat4Econ/probability/poisson.ipynb"
[1] "spt_file_full_rmd:C:/Users/fan/Stat4Econ/probability/poisson.Rmd"
[1] "st_fullpath_noname:C:/Users/fan/Stat4Econ/probability"
[1] "spt_file:C:/Users/fan/Stat4Econ/probability/samplespace.ipynb"
[1] "spt_file_full_rmd:C:/Users/fan/Stat4Econ/probability/samplespace.Rmd"
[1] "st_fullpath_noname:C:/Users/fan/Stat4Econ/probability"
[1] "spt_file:C:/Users/fan/Stat4Econ/probability/samplespacedice.ipynb"
[1] "spt_file_full_rmd:C:/Users/fan/Stat4Econ/probability/samplespacedice.Rmd"
[1] "st_fullpath_noname:C:/Users/fan/Stat4Econ/probability"
[1] "spt_file:C:/Users/fan/Stat4Econ/probability/samplespaceexa.ipynb"
[1] "spt_file_full_rmd:C:/Users/fan/Stat4Econ/probability/samplespaceexa.Rmd"
[1] "st_fullpath_noname:C:/Users/fan/Stat4Econ/summarystats"
[1] "spt_file:C:/Users/fan/Stat4Econ/summarystats/meansdhist.ipynb"
[1] "spt_file_full_rmd:C:/Users/fan/Stat4Econ/summarystats/meansdhist.Rmd"
[1] "st_fullpath_noname:C:/Users/fan/Stat4Econ/summarystats"
[1] "spt_file:C:/Users/fan/Stat4Econ/summarystats/toolboxone.ipynb"
[1] "spt_file_full_rmd:C:/Users/fan/Stat4Econ/summarystats/toolboxone.Rmd"
[1] "st_fullpath_noname:C:/Users/fan/Stat4Econ/survey"
[1] "spt_file:C:/Users/fan/Stat4Econ/survey/classsurvey.ipynb"
[1] "spt_file_full_rmd:C:/Users/fan/Stat4Econ/survey/classsurvey.Rmd"
The working directory was changed to C:/Users/fan/Stat4Econ/survey inside a notebook chunk. The working directory will be reset when the chunk is finished running. Use the knitr root.dir option in the setup chunk to change the working directory for notebook chunks.