RMarkdown Inline Code Format

John Smith

I am reading ISL at the moment which is related to machine learning in R

I really like how the book is laid out specifically where the authors reference code inline or libraries for example library(MASS).

Does anyone know if the same effect can be achieved using R Markdown i.e. making the MASS keyword above brown when i reference it in a paper? I want to color code columns in data frames when i talk about them in the R Markdown document. When you knit it as a HTML document it provides pretty good formatting but when i Knit it to MS Word it seems to just change the font type

Thanks

brittenb

I've come up with a solution that I think might address your issue. Essentially, because inline source code gets the same style label as code chunks, any change you make to SourceCode will be applied to both chunks, which I don't think is what you want. Instead, there needs to be a way to target just the inline code, which doesn't seem to be possible from within rmarkdown. Instead, what I've opted to do is take the .docx file that is produced, convert it to a .zip file, and then modify the .xml file inside that has all the data. It applies a new style to the inline source code text, which can then be modified in your MS Word template. Here is the code:

format_inline_code = function(fpath) {
  if (!tools::file_ext(fpath) == "docx") stop("File must be a .docx file...")
  cur_dir = getwd()
  .dir = dirname(fpath)
  setwd(.dir)
  out = gsub("docx$", "zip", fpath)

  # Convert to zip file
  file.rename(fpath, out)

  # Extract files
  unzip(out, exdir=".")

  # Read in document.xml
  xml = readr::read_lines("word/document.xml")

  # Replace styling
  # VerbatimChar didn't appear to the style that was applied in Word, nor was
  # it present to be styled. VerbatimStringTok was though.
  xml = sapply(xml, function(line) gsub("VerbatimChar", "VerbatimStringTok", line))

  # Save document.xml
  readr::write_lines(xml, "word/document.xml")

  # Zip files
  .files = c("_rels", "docProps", "word", "[Content_Types].xml")
  zip(zipfile=out, files=.files)

  # Convert to docx
  file.rename(out, fpath)

  # Remove the folders extracted from zip
  sapply(.files, unlink, recursive=TRUE)

  setwd(cur_dir)
}

The style that you'll want to modify in you MS Word template is VerbatimStringTok. Hope that helps!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

RMarkdown Inline Code Format

From Dev

Evaluate inline r code in rmarkdown figure caption

From Java

How to format an inline code in Confluence?

From Dev

rmarkdown/knitr: How to format function output as code?

From Dev

How to display verbatim inline r code with backticks using Rmarkdown?

From Dev

Inline R code in YAML for rmarkdown doesn't run

From Dev

How can I use rmarkdown inline code within a ggtitle

From Dev

How to prevent string escaping in a LaTeX block with inline R code in RMarkdown?

From Dev

Bold inline equation in Rmarkdown

From Dev

RMarkdown: how to print formula inline

From Dev

How to format a dataframe in rmarkdown

From Dev

How to convert provided single line of cURL code with inline parameters to a format using curl_setopt()

From Dev

How to convert provided single line of cURL code with inline parameters to a format using curl_setopt()

From Dev

toggle show/hide code in RMarkdown

From Dev

Highlighting bash code with knitr / rmarkdown

From Dev

rmarkdown: toggling code blocks in the browser

From Dev

Sphinx inline code highlight

From Dev

ObjectDataSource and inline code

From Dev

Nonbreaking space in inline code

From Dev

Is there a way to inline this javascript code?

From Dev

Change inline to code behind

From Dev

Error in inline edit code

From Dev

Code Coverage Inline Function

From Dev

Inline code and shiny, is it possible?

From Dev

JQgrid custom inline edit format

From Dev

Inline code is not executing as the code behind

From Dev

How can I format sessionInfo() in rmarkdown?

From Dev

How to get inline equation in pdf document using rmarkdown?

From Dev

How to get inline equation in pdf document using rmarkdown?

Related Related

HotTag

Archive