R Graphics Cookbook, 2nd edition

repository·master·Indexed 43 days ago

https://github.com/wch/rgcookbook

Source code repository and reference for R graphics implementation examples from the R Graphics Cookbook, 2nd edition. Includes tools and documentation for converting original AsciiDoc files to DocBook, Markdown, and Rmd formats for use with knitr and bookdown.

Tokens
1.2K
Snippets
3
Records
4
Agent score
6%

What's inside R Graphics Cookbook

  1. Access the R Graphics Cookbook source code

    master
    The rgcookbook repository contains the source code for the R Graphics Cookbook, 2nd edition. You can use this repository to explore the code examples used in the book or to host your own version of the cookbook content.
  2. Convert AsciiDoc to DocBook

    master

    To convert original AsciiDoc files to DocBook format, use the asciidoctor/docker-asciidoctor Docker image. This step is required as an intermediate stage before converting to Markdown/Rmd.

    1. Run the Docker container with the current directory mounted to /documents/.
    2. Use asciidoctor -b docbook5 to perform the conversion on all .asciidoc files.
    docker run --rm -it -v $(pwd):/documents/ asciidoctor/docker-asciidoctor
    
    # Convert asciidoc files to docbook
    ls *.asciidoc | xargs -L 1 asciidoctor -b docbook5
  3. Post-conversion cleanup for Rmd

    master

    After converting .md files to .Rmd (using mmv), several cleanup steps are required to ensure compatibility with knitr and bookdown. These steps use mreplace to fix code blocks, quotes, and cross-references.

    Fix Code Blocks and Quotes

    • Convert {.r} to {r}.
    • Replace AsciiDoc smart quote markers (RSQUO, RDQUO, LDQUO) with standard single (') or double (") quotes.

    Fix Figure, Recipe, and Chapter References

    knitr does not support underscores in numbered references. You must rename identifiers from NAME_X_Y to NAME-X-Y and update the reference syntax.

    • Figures: Rename FIG_X_Y to FIG-X-Y and change references to Figure \@ref(fig:...).
    • Recipes: Rename RECIPE_X_Y to RECIPE-X-Y and change references to Recipe \@ref(RECIPE...).
    • Chapters: Rename CHAPTER_X_Y to CHAPTER-X-Y and change references to Chapter \@ref(CHAPTER...).

    Note: For Figure, Recipe, and Chapter renames, run the command repeatedly until no more matches are found.

    Remove Labels and Fix Math Delimiters

    • Remove Problem/Solution/See Also/Discussion labels (e.g., {#_problem_1}).
    • Replace $$_$$ patterns with a single underscore _.
    cd ../rgcookbook2e
    mmv -e \\.md .Rmd
    
    # Fix code blocks
    mreplace -e " \{.r\}" "{r}" *.Rmd
    
    # Fix smart quote markers
    mreplace -e "RSQUO" "'" *.Rmd
    mreplace -e "RDQUO" '"' *.Rmd
    mreplace -e "LDQUO" '"' *.Rmd
    
    # Rename FIG_X_Y to FIG-X-Y (Run repeatedly)
    mreplace -e "FIG([^ \n]*?)_" "FIG\\1-" *.Rmd
    mreplace -e "\\[figure\\\\_title\\]\(#" "Figure \\@ref(fig:" *.Rmd
    
    # Rename RECIPE_X_Y to RECIPE-X-Y (Run repeatedly)
    mreplace -e "RECIPE([^ \n]*?)_" "RECIPE\\1-" *.Rmd
    mreplace -e "\\[.*?\]\(#RECIPE" "Recipe \\@ref(RECIPE" *.Rmd
    
    # Rename CHAPTER_X_Y to CHAPTER-X-Y (Run repeatedly)
    mreplace -e "CHAPTER([^ \n]*?)_" "CHAPTER\\1-" *.Rmd
    mreplace -e "\\[.*?\]\(#CHAPTER" "Chapter \\@ref(CHAPTER" *.Rmd
    
    # Remove Problem/Solution/See Also/Discussion labels
    mreplace " +{#_(problem|solution|see_also|discussion)_\\d+}" "" *.Rmd
    
    # Replace "$$\u005F$$" with "_"
    mreplace "(\w+)\\\$\\\$\_\\\$\\\\\$\\(\w+)" "\1_\2" *.Rmd
  4. Convert DocBook to Markdown

    master

    Once files are in DocBook (.xml) format, use pandoc to convert them to Markdown (.md).

    Use the following command structure to ensure header attributes are preserved and line wrapping is maintained: pandoc --from docbook --to markdown-header_attributes --wrap=preserve <input.xml> -o <output.md>

    for old in *.xml; do pandoc --from docbook --to markdown-header_attributes --wrap=preserve $old -o `basename $old .xml`.md; done
    
    rm *.xml
    mv *.md ../rgcookbook2e