R Under development (unstable) (2025-12-17 r89193 ucrt) -- "Unsuffered Consequences" Copyright (C) 2025 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library(knitrdata) > library(magrittr) # For pipe operator > > # Use a temporary directory ---------------------------- > owd = getwd() > td = tempdir() > setwd(td) > > # Create simple data files ------------------ > df = data.frame(a=letters[1:3],b=1.1:3.1) > > csv.fn = "test.csv_rds_chunks.csv" > csv2.fn = "test.csv_rds_chunks.csv2" > odd.fn = "test.csv_rds_chunks.odd" > rds.fn = "test.csv_rds_chunks.RDS" > > write.csv(df,csv.fn,row.names=FALSE) > write.csv2(df,csv2.fn,row.names=FALSE) > write.table(df,odd.fn,row.names=FALSE,sep="|",dec=":") > saveRDS(df,rds.fn) > > # Create chunks ------------------ > loadlib = create_chunk("library(knitrdata)",chunk_type="r",chunk_label="loadlib") > > lines = readLines(csv.fn) > lines2 = readLines(csv2.fn) > lines3 = readLines(odd.fn) > > loadcsv = + create_chunk(lines,output.var="df.csv",echo=TRUE,chunk_label="loadcsv",chunk_type="csv") > loadcsv2 = + create_chunk(lines2,output.var="df.csv2",echo=TRUE,chunk_label="loadcsv2",chunk_type="csv2") > loadodd = + create_chunk(lines3,output.var="df.odd",echo=TRUE, + sep="|",dec=":",chunk_label="loadodd",chunk_type="csv") > > loadrds = data_encode(rds.fn,"base64") %>% + create_chunk(output.var="df.rds",echo=TRUE,chunk_label="loadrds",chunk_type="rds") > > showdata = create_chunk(c("df.csv","df.csv","df.odd","df.rds"), + chunk_type="r", + chunk_label="showdata") > > # Create Rmarkdown doc -------------- > rmd.fn = "test.csv_rds_chunks.Rmd" > if (file.exists(rmd.fn)) + file.remove(rmd.fn) > rmarkdown::draft(rmd.fn,"github_document","rmarkdown",edit=FALSE) > > # Push chunks into document ------------------ > > # Insert in reverse order to not have to figure out line number > rmd.text = readLines(rmd.fn) > > rmd.text = insert_chunk(showdata,11,rmd.text=rmd.text) > rmd.text = insert_chunk(loadrds,11,rmd.text=rmd.text) > rmd.text = insert_chunk(loadodd,11,rmd.text=rmd.text) > rmd.text = insert_chunk(loadcsv2,11,rmd.text=rmd.text) > rmd.text = insert_chunk(loadcsv,11,rmd.text=rmd.text) > rmd.text = insert_chunk(loadlib,11,rmd.text=rmd.text) > > writeLines(rmd.text,rmd.fn) > > # List all chunks in document > chunklst = list_rmd_chunks(file=rmd.fn) > chunklst type label start end 1 r setup 6 8 2 r loadlib 11 13 3 csv loadcsv 14 19 4 csv2 loadcsv2 20 25 5 csv loadodd 26 31 6 rds loadrds 32 36 7 r showdata 37 42 8 r cars 50 52 9 r pressure 58 60 > > # View file in Rstudio ---------- > # rstudioapi::navigateToFile(rmd.fn) > > # Clean environment ------ > l = ls() > l = l[!(l %in% c("rmd.fn","owd"))] > rm(list=l) > > # Render document to test ------- > if (rmarkdown::pandoc_available(version="1.12.3")) + system.time( + rmarkdown::render(rmd.fn) + ) processing file: test.csv_rds_chunks.Rmd output file: test.csv_rds_chunks.knit.md "C:/PROGRA~1/Pandoc/pandoc" +RTS -K512m -RTS test.csv_rds_chunks.knit.md --to gfm+tex_math_dollars-yaml_metadata_block --from markdown+autolink_bare_uris+tex_math_single_backslash --output test.csv_rds_chunks.md --template "D:\RCompile\CRANpkg\lib\4.6\rmarkdown\rmarkdown\templates\github_document\resources\default.md" "C:/PROGRA~1/Pandoc/pandoc" +RTS -K512m -RTS test.csv_rds_chunks.md --to html4 --from gfm+tex_math_dollars --output test.csv_rds_chunks.html --embed-resources --standalone --highlight-style pygments --template "D:\RCompile\CRANpkg\lib\4.6\rmarkdown\rmarkdown\templates\github_document\resources\preview.html" --variable "github-markdown-css:D:\RCompile\CRANpkg\lib\4.6\rmarkdown\rmarkdown\templates\github_document\resources\github.css" --metadata pagetitle=PREVIEW --mathjax Preview created: test.csv_rds_chunks.html Output created: test.csv_rds_chunks.md user system elapsed 0.25 0.03 0.80 > > # Clean up -------------- > setwd(owd) > > proc.time() user system elapsed 0.78 0.21 1.67