Skip to contents

Parses the content of a Shinylive code block, extracting YAML-style options, file definitions, and content. Handles both single-file and multi-file applications for R and Python.

Usage

parse_code_block(code_text, engine)

Arguments

code_text

Character string. The raw text content of a Shinylive code block, which may contain YAML-style options, file markers, and file content.

engine

Character string. The type of Shinylive application, either "r" or "python". Determines default file extension when no explicit file is specified.

Value

A list with three components:

  • engine: Character string indicating the application type ("r" or "python")

  • options: List of parsed YAML-style options from block headers

  • files: Named list of file definitions, where each file contains:

    • name: Character string of the file name

    • content: Character string of the file content

    • type: Character string indicating the file type (defaults to `"text"“)

Code Block Structure

The code block can contain several types of lines:

  • YAML-style options: Lines starting with `'#|'“

  • File markers: Lines starting with '## file:'

  • Type markers: Lines starting with '## type:'

  • Content: All other non-empty lines

For single-file applications with no explicit file marker, the content is automatically placed in:

  • "app.R" for R applications

  • "app.py" for Python applications

See also

Examples

if (FALSE) { # \dontrun{
# Single-file R application
code <- '
#| viewerHeight: 500
library(shiny)
ui <- fluidPage()
server <- function(input, output) {}
shinyApp(ui, server)
'
result1 <- parse_code_block(code, "r")

# Multi-file Python application
code <- '
#| fullWidth: true
## file: app.py
from shiny import App, ui
app = App(app_ui)
## file: requirements.txt
## type: text
shiny>=0.5.0
'
result2 <- parse_code_block(code, "python")
} # }