View source: R/input-code-editor.R
| input_code_editor | R Documentation |
Creates an interactive light-weight code editor input that can be used in
Shiny applications. The editor provides syntax highlighting, line numbers,
and other basic code editing features powered by Prism Code Editor. For a
complete example, run shiny::runExample("code-editor", package = "bslib").
The editor value is not sent to R on every keystroke. Instead, updates are
reflected on the server when the user moves away from the editor or when they
press Ctrl/Cmd + Enter.
Note that this input is not designed for editing or rendering large files. Displaying files with 1,000 lines or more may lead to performance issues.
input_code_editor(
id,
label = NULL,
value = "",
...,
language = "plain",
height = "auto",
width = "100%",
theme_light = "github-light",
theme_dark = "github-dark",
read_only = FALSE,
line_numbers = NULL,
word_wrap = NULL,
tab_size = 2,
indentation = c("space", "tab"),
fill = TRUE
)
update_code_editor(
id,
...,
value = NULL,
label = NULL,
language = NULL,
theme_light = NULL,
theme_dark = NULL,
read_only = NULL,
line_numbers = NULL,
word_wrap = NULL,
tab_size = NULL,
indentation = NULL,
session = get_current_session()
)
id |
Input ID. Access the current value with |
label |
Display label for the input. Default is |
value |
Code content. Default is an empty string. |
... |
Named arguments, e.g. |
language |
Programming language for syntax highlighting. Supported
languages include |
height |
CSS height of the editor. Default is |
width |
CSS width of the editor. Default is |
theme_light, theme_dark |
Theme to use in light or dark mode. Defaults to
|
read_only |
Whether the editor should be read-only. Default is |
line_numbers |
Whether to show line numbers. Default is |
word_wrap |
Whether to wrap long lines, by default disabled when
|
tab_size |
Number of spaces per tab. Default is |
indentation |
Type of indentation: |
fill |
Whether or not to allow the card to grow/shrink to fit a
fillable container with an opinionated height (e.g., |
session |
a shiny session object (the default should almost always be used). |
An HTML tag object that can be included in a Shiny UI.
The editor supports the following keyboard shortcuts:
Ctrl/Cmd+Enter: Submit the current code to R
Ctrl/Cmd+Z: Undo
Ctrl/Cmd+Shift+Z: Redo
Tab: Indent selection
Shift+Tab: Dedent selection
The editor automatically switches between theme_light and theme_dark
when used with input_dark_mode(). Otherwise, the editor will use
theme_light by default.
A variety of themes are available for use. The full list of bundled themes
is: "atom-one-dark", "dracula", "github-dark-dimmed", "github-dark", "github-light", "night-owl-light", "night-owl", "prism-okaidia", "prism-solarized-light", "prism-tomorrow", "prism-twilight", "prism", "vs-code-dark", "vs-code-light".
Other input controls:
input_dark_mode(),
input_switch()
library(shiny)
library(bslib)
ui <- page_fluid(
input_code_editor(
"sql_query",
value = "SELECT * FROM table",
language = "sql"
),
verbatimTextOutput("code_output")
)
server <- function(input, output, session) {
output$code_output <- renderPrint({
input$sql_query
})
}
shinyApp(ui, server)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.