reindent_chromote: Reindent code using chromote

View source: R/prettify_chromote.R

reindent_chromoteR Documentation

Reindent code using chromote

Description

Reindent some code using chromote.

Usage

reindent_chromote(contents = NA, language = NA, tabSize = NULL)

Arguments

contents

the code to be reindented; there are three possibilities for this argument: NA (default), to use the file currently opened in RStudio; the path to a file; or the code given as a character vector

language

the language of the code, such as "python"; see getPrettifiableLanguages; if the contents are read from a file and language=NA, then the language is guessed from the file extension

tabSize

number of spaces of the indentation (usually 2 or 4); if NULL (the default), there are two possibilities: if the contents are read from the current file in RStudio, then the number of spaces will be the one you use in RStudio; otherwise it is set to 2

Value

The reindented code in a character string.

Note

This function uses chromote::find_chrome() to find the executable of Google Chrome or another Chromium-based browser. If it is not found you will get an error. In this case set the environment variable CHROMOTE_CHROME to the path of such an executable (e.g. Sys.setenv(CHROMOTE_CHROME = "path/to/chrome.exe")).

Examples

library(prettifyAddins)

code <- c(
  'if test == 1:',
  'print "it is one"',
  'else:',
  'print "it is not one"'
)

if(Sys.which("chrome") != "") {
  cat(reindent_chromote(code, "python"))
}

prettifyAddins documentation built on Sept. 14, 2023, 5:06 p.m.