knitr-knit

Here are the examples of the r api knitr-knit taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

14 Examples 7

 buildRAQSAPIbase  < - function() { 

     if ("RAQSAPI" %in% loaded_packages()$package) { 

         unload("RAQSAPI") 

     } 

     invisible(usethis::use_lifecycle()) 

     devtools::document(quiet = TRUE, roclets = c("collate", "namespace",  

         "rd", "vignette")) 

     devtools::build_readme() 

     knitr::knit(input = "./dev/contributing.Rmd", output = "./dev/contributing.md") 

     knitr::knit(input = "./cran-comments.Rmd", output = "./cran-comments.md") 

     tools:::.installMD5sums(pkgDir = ".") 

 } 

19 File: precompile.R, author: bcgov, license: Apache License 2.0

 precompile  < - function(vignette_to_run = NULL) { 

     orig_files  < - file.path(list.files(path = "vignettes/", pattern = "*\\.Rmd\\.orig",  

         full.names = TRUE)) 

     if (!is.null(vignette_to_run)) { 

         orig_files  < - orig_files[basename(orig_files) %in% vignette_to_run] 

         if (rlang::is_empty(orig_files))  

             stop("Not a vignette!") 

     } 

     purrr::walk(orig_files, ~knitr::knit(.x, tools::file_path_sans_ext(.x))) 

     images  < - file.path(list.files(".", pattern = "vignette-fig.*\\.png$")) 

     success  < - file.copy(from = images, to = file.path("vignettes",  

         images), overwrite = TRUE) 

     if (!all(success)) { 

         stop("Image files were not successfully transferred to vignettes directory") 

     } 

     else { 

         unlink(images) 

     } 

 } 

 knitr::knit(input = "model-uptake.Rmd") 

19 File: test-knitr.r, author: klmr, license: MIT License

 test_that("modules are found inside a knitr document", { 

     check_knitr() 

     on.exit(unloadNamespace("knitr")) 

     input = "support/knitr/doc.rmd" 

     knitr::opts_knit$set(root.dir = getwd()) 

     output = knitr::knit(input, quiet = TRUE) 

     on.exit(unlink(output), add = TRUE) 

     expected = "```\n## knitr/a\n```" 

     expect_match(paste(readLines(output), collapse = "\n"), expected) 

 }) 

19 File: starter.R, author: MattiaCinelli, license: MIT License

 my.knit  < - knitr::knit("document.Rnw") 

 for (file in vignette_files) { 

     cat(glue::glue("Building {file}\n")) 

     knitr::knit(glue::glue("../vignettes_src/{file}"), glue::glue("{file}")) 

 } 

 knitr::knit(devtools::package_file("README.Rmd")) 

 test_that("module templates work", { 

     testInitOut  < - testInit(c("knitr", "rmarkdown"), smcc = FALSE) 

     on.exit({ 

         testOnExit(testInitOut) 

     }, add = TRUE) 

     expect_true(dir.exists(tmpdir)) 

     moduleName  < - "myModule" 

     newModule(moduleName, tmpdir, open = FALSE, unitTests = TRUE,  

         useGitHub = TRUE) 

     mpath  < - file.path(tmpdir, moduleName) 

     expect_true(file.exists(mpath)) 

     expect_true(file.exists(file.path(mpath, "citation.bib"))) 

     expect_true(file.exists(file.path(mpath, "LICENSE"))) 

     expect_true(file.exists(file.path(mpath, paste0(moduleName,  

         ".R")))) 

     expect_true(file.exists(file.path(mpath, paste0(moduleName,  

         ".Rmd")))) 

     expect_true(file.exists(file.path(mpath, "README.txt"))) 

     expect_true(dir.exists(file.path(mpath, ".github"))) 

     expect_true(dir.exists(file.path(mpath, ".github", "workflows"))) 

     expect_true(file.exists(file.path(mpath, ".github", "workflows",  

         "render-module-rmd.yaml"))) 

     expect_true(dir.exists(file.path(mpath, "data"))) 

     expect_true(file.exists(file.path(mpath, "data", "CHECKSUMS.txt"))) 

     expect_true(file.exists(file.path(mpath, "data", ".gitignore"))) 

     expect_true(dir.exists(file.path(mpath, "tests"))) 

     expect_true(dir.exists(file.path(mpath, "tests", "testthat"))) 

     expect_true(file.exists(file.path(mpath, "tests", "unitTests.R"))) 

     expect_true(file.exists(file.path(mpath, "tests", "testthat",  

         "test-template.R"))) 

     utils::capture.output(zipModule(name = moduleName, path = tmpdir,  

         version = "0.0.2", flags = "-q -r9X")) 

     skip_if_not(nzchar(Sys.which("zip"))) 

     expect_true(file.exists(file.path(mpath, paste0(moduleName,  

         "_0.0.2.zip")))) 

     expect_equal(knitr::knit(input = file.path(mpath, paste0(moduleName,  

         ".Rmd")), output = file.path(mpath, paste0(moduleName,  

         ".md")), quiet = TRUE), file.path(mpath, paste0(moduleName,  

         ".md"))) 

 }) 

 do_knit  < - function(option, quiet = TRUE) { 

     ff  < - list.files("_R", pattern = ".Rmd$", ignore.case = TRUE,  

         full.names = TRUE, recursive = TRUE) 

     kf  < - list.files(".", pattern = "\\.rst$", recursive = TRUE) 

     kf  < - kf[-grep("index.rst", kf, ignore.case = TRUE)] 

     dir.create("figures/", showWarnings = FALSE) 

     dir.create("txt/", showWarnings = FALSE) 

     u  < - unique(gsub("_R", "", dirname(ff))) 

     u  < - u[u != ""] 

     u  < - gsub("^/", "", u) 

     for (d in u) { 

         dir.create(d, showWarnings = FALSE, TRUE) 

         dir.create(file.path(d, "figures"), showWarnings = FALSE) 

         dir.create(file.path(d, "txt"), showWarnings = FALSE) 

     } 

     if (option == "clean") { 

         file.remove(kf) 

         file.remove(list.files("txt", full = TRUE)) 

         file.remove(list.files("figures", full = TRUE)) 

     } 

     else { 

         if (length(kf) > 0) { 

             stime  < - file.info(ff) 

             fn  < - gsub("_R/", "./", raster::extension((rownames(stime)),  

                 "")) 

             stime  < - data.frame(f = fn, stime = stime$mtime,  

                 stringsAsFactors = FALSE) 

             btime  < - file.info(kf) 

             fn  < - paste0("./", raster::extension((rownames(btime)),  

                 "")) 

             btime  < - data.frame(f = fn, btime = btime$mtime,  

                 stringsAsFactors = FALSE) 

             m  < - merge(stime, btime, by = 1, all.x = TRUE) 

             m[is.na(m$btime), "btime"]  < - as.POSIXct(as.Date("2000-01-01")) 

             i  < - which(m$btime  <  m$stime) 

             ff  < - ff[i] 

         } 

     } 

     if (length(ff) > 0) { 

         loadNamespace("knitr") 

         outf  < - gsub("_R/", "", ff) 

         md  < - gsub(".rmd$", ".md", outf) 

         rst  < - gsub(".rmd$", ".rst", outf) 

         txtp  < - file.path(dirname(outf), "txt", basename(outf)) 

         rcd  < - gsub(".rmd$", ".txt", txtp) 

         knitr::opts_chunk$set(dev = "png", fig.width = 6, fig.height = 6,  

             fig.path = "figures/", fig.cap = "", collapse = TRUE) 

         for (i in 1:length(ff)) { 

             dn  < - dirname(rst[i]) 

             if (dn != ".") { 

                 knitr::opts_chunk$set(fig.path = paste0(dn, "/figures/")) 

                 fdirclean  < - TRUE 

             } 

             else { 

                 fdirclean  < - FALSE 

             } 

             cat(paste("   ", tools::file_path_sans_ext(outf[i]),  

                 "\n")) 

             knitr::knit(ff[i], md[i], envir = new.env(), encoding = "UTF-8",  

                 quiet = quiet) 

             knitr::purl(ff[i], rcd[i], quiet = TRUE) 

             if (fdirclean) { 

                 x  < - readLines(md[i]) 

                 j  < - grep("png", x) 

                 x[j] = gsub(paste0(dn, "/"), "", x[j]) 

                 writeLines(x, md[i]) 

             } 

             pc  < - paste("pandoc", md[i], "-f markdown -t rst -o",  

                 rst[i]) 

             sysfun(pc) 

             file.remove(md[i]) 

         } 

     } 

 } 

19 File: utils.R, author: rstudio, license: Apache License 2.0

 eval_metadata  < - function(metadata) { 

     metadata_yaml  < - yaml::as.yaml(metadata) 

     metadata_yaml  < - knitr::knit(text = metadata_yaml) 

     yaml::yaml.load(metadata_yaml) 

 } 

 withr::with_dir(here::here("vignettes/articles"), { 

     knitr::knit("ghclass.Rmd_raw", "ghclass.Rmd") 

 }) 

 knitr::knit("summarize_benchmarking.Rmd", output = "summarize_benchmarking.md") 

 for (i in vignette_files) { 

     knitr::knit(i, output = gsub(pattern = ".orig", replacement = "",  

         x = i)) 

 } 

 list_apps  < - function(categories = NULL, keywords = NULL) { 

     apps  < - dir(paste0(find.package("teachR"), "/apps")) 

     info_table  < - data.frame() 

     for (i in 1:length(apps)) { 

         info  < - utils::read.table(paste0(find.package("teachR"),  

             "/apps/", apps[i], "/info"), sep = ":") 

         info  < - t(as.matrix(info)[, 2]) 

         info_table  < - rbind(info_table, info) 

     } 

     info_table[]  < - lapply(info_table, as.character) 

     names(info_table)  < - c("Title", "Version", "Date", "Author",  

         "Maintainer", "Description", "Category", "Keywords",  

         "License", "Depends", "Command") 

     md  < - .make_list(info_table, categories, keywords) 

     tmp_file  < - tempfile(pattern = "file", fileext = ".html") 

     out  < - knitr::knit(text = md) 

     markdown::markdownToHTML(text = out, output = tmp_file, options = c("toc"),  

         title = "Apps available in teachR", template = paste0(find.package("teachR"),  

             "/html_templates/template.html")) 

     viewer  < - getOption("viewer") 

     if (!is.null(viewer))  

         viewer(tmp_file) 

     else utils::browseURL(tmp_file) 

 } 

Categories r Tags