## ----setup--------------------------------------------------------------------
#| include: false
library(livelink)


## -----------------------------------------------------------------------------
webr_repl_project(list(
  "main.R"    = { source("utils.R"); summarise(mtcars) },
  "utils.R"   = { summarise <- function(d) summary(d) },
  "README.md" = "# Analysis\n\nRun main.R to start."
))


## -----------------------------------------------------------------------------
project <- list(
  "main.R"    = "source('utils.R')\nsummarise(mtcars)",
  "utils.R"   = "summarise <- function(d) summary(d)",
  "README.md" = "# Analysis\n\nRun main.R to start."
)

webr_repl_project(project)


## -----------------------------------------------------------------------------
dir <- file.path(tempdir(), "project")
dir.create(dir, showWarnings = FALSE)
writeLines("source('utils.R')",  file.path(dir, "main.R"))
writeLines("f <- function() 42", file.path(dir, "utils.R"))

webr_repl_project(c(file.path(dir, "main.R"), file.path(dir, "utils.R")))


## -----------------------------------------------------------------------------
link <- webr_repl_project(project, autorun_files = "main.R")

preview_webr_link(as.character(link))$autorun_files


## -----------------------------------------------------------------------------
webr_repl_project(project, autorun_files = "all")


## -----------------------------------------------------------------------------
#| error: true
try({
webr_repl_project(project, autorun_files = "nonexistent.R")
})


## -----------------------------------------------------------------------------
webr_repl_project(project, base_path = "/home/web_user/analysis/")


## -----------------------------------------------------------------------------
#| eval: false
# shinylive_r_link({
#   library(shiny)
# 
#   ui <- fluidPage(
#     titlePanel("Old Faithful"),
#     sliderInput("bins", "Bins:", 1, 50, 30),
#     plotOutput("hist")
#   )
# 
#   server <- function(input, output) {
#     output$hist <- renderPlot({
#       hist(faithful$waiting, breaks = input$bins, col = "steelblue")
#     })
#   }
# 
#   shinyApp(ui, server)
# })


## -----------------------------------------------------------------------------
app <- "library(shiny)\nshinyApp(fluidPage(), function(input, output) {})"

shinylive_r_link(app)


## -----------------------------------------------------------------------------
#| eval: false
# shinylive_r_link(list(
#   "app.R"    = { library(shiny); source("ui.R"); source("server.R"); shinyApp(ui, server) },
#   "ui.R"     = { ui <- fluidPage(titlePanel("Split app")) },
#   "server.R" = { server <- function(input, output) {} }
# ))


## -----------------------------------------------------------------------------
# code and app side by side -- for teaching and review
shinylive_r_link(app, mode = "editor")


## -----------------------------------------------------------------------------
# the running application only -- for the people who just want to use it
shinylive_r_link(app, mode = "app")


## -----------------------------------------------------------------------------
shinylive_r_link(app, mode = "app", header = FALSE)


## -----------------------------------------------------------------------------
py_app <- "
from shiny import App, render, ui

app_ui = ui.page_fluid(
    ui.h2('Hello from Python'),
    ui.output_text('greeting'),
)

def server(input, output, session):
    @render.text
    def greeting():
        return 'Running in the browser, no server required.'

app = App(app_ui, server)
"

shinylive_py_link(py_app, mode = "app")


## -----------------------------------------------------------------------------
shinylive_project(
  list("app.py" = py_app, "utils.py" = "def helper():\n    return 42"),
  engine = "python"
)


## -----------------------------------------------------------------------------
apps <- file.path(tempdir(), "apps")
dir.create(file.path(apps, "histogram"), recursive = TRUE, showWarnings = FALSE)
dir.create(file.path(apps, "scatter"),   recursive = TRUE, showWarnings = FALSE)
writeLines(app, file.path(apps, "histogram", "app.R"))
writeLines(app, file.path(apps, "scatter",   "app.R"))

links <- shinylive_directory(apps, engine = "r", mode = "app")

repl_urls(links)


## -----------------------------------------------------------------------------
empty <- file.path(tempdir(), "no-apps")
dir.create(empty, showWarnings = FALSE)

shinylive_directory(empty, engine = "r")


## -----------------------------------------------------------------------------
scripts <- file.path(tempdir(), "scripts")
dir.create(scripts, showWarnings = FALSE)
writeLines("source('utils.R')", file.path(scripts, "main.R"))
writeLines("f <- function() 42", file.path(scripts, "utils.R"))

webr_repl_directory(scripts, single_link = TRUE, panels = c("editor", "plot"))


## -----------------------------------------------------------------------------
src <- paste(
  unlist(lapply(c("lm", "glm", "aov"), function(f) {
    paste(deparse(get(f, envir = asNamespace("stats"))), collapse = "\n")
  })),
  collapse = "\n\n"
)

url <- as.character(webr_repl_link(src, filename = "stats.R"))

c(source_chars = nchar(src), url_chars = nchar(url))

