File size: 1,760 Bytes
f11cb19
 
9ccff4d
 
f11cb19
9ccff4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f11cb19
9ccff4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f11cb19
 
9ccff4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f11cb19
9ccff4d
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# remotes::install_github("jrosell/ambhtmx", force = TRUE)
library(ambhtmx)
library(ggplot2)
library(plotly)

page_title <- "WIP: ambhtmx slider example"

head_tags <- htmltools::tagList()

#' Generate a plot from rexp_data
generate_htmlwidget <- \(){
  print("generate_htmlwidget")
  rexp_df <- tibble(x = 1:length(rexp_data), y = rexp_data)
  p_plot <- ggplot(rexp_df, aes(x, y)) + geom_line()
  plotly::ggplotly(p_plot, width = 200, height = 100)  
}

counter <- 1
rexp_data <- c(rexp(1), rexp(1))

#' Starting the app
c(app, context, operations) %<-% ambhtmx(host = "0.0.0.0", port = "7860")


#' Main page of the app
app$get("/", \(req, res){
  html <- ""
  tryCatch({
      rexp_widget <- generate_htmlwidget()
      rexp_tags <- amb_htmlwidget(rexp_widget, id = "rexp_widget")
      html <- div(
          style = "margin: 20px",
          h1(page_title),
          div(
            id = "counter",
            p(glue("Counter is set to {counter}")),
            rexp_tags
          ),
          button(
            "+1",
            hx_get="/increment",
            hx_target="#counter",
            # hx_target="#rexp_widget [data-for]",
            # hx_target="#debug",
            hx_swap="outerHTML"
          ),
          div(id = "debug")
        ) |> 
        send_page(res)
  },
    error = \(e) print(e)
  )
})

#' Call to return the plot
app$get("/increment", \(req, res){
  counter <<- counter + 1
  rexp_data <<- c(rexp_data, rexp(1))
  rexp_widget <- generate_htmlwidget()
  rexp_tags <- amb_htmlwidget(rexp_widget, id = "rexp_widget")
  div(
      id = "counter",
      p(glue("Counter is set to {counter}")),
      rexp_tags
    ) |> 
    send_tags(res)
})

#' Start the app with all the previous defined routes
app$start(open = FALSE)