Skip to content

Commit 5e780b5

Browse files
committed
Switch to using a split example structure
1 parent 8940260 commit 5e780b5

File tree

5 files changed

+93
-58
lines changed

5 files changed

+93
-58
lines changed

inst/demo-py-shiny-containerization.R

+10-29
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,20 @@
1-
# Create a simple Shiny for Python app
1+
# Quick demo of containerizing a simple Shiny for Python app
2+
3+
## Setup a temporary directory that gets destructed after the session ----
24
app_dir <- tempfile()
35
dir.create(app_dir)
4-
writeLines(
5-
'from shiny import App, ui, render
6-
import numpy as np
7-
import matplotlib.pyplot as plt
8-
9-
app_ui = ui.page_fluid(
10-
ui.panel_title("Hello Docker"),
11-
ui.layout_sidebar(
12-
ui.sidebar(
13-
ui.input_slider("obs", "Number of observations:", min=1, max=1000, value=500)
14-
),
15-
ui.output_plot("distPlot")
16-
)
17-
)
18-
19-
def server(input, output, session):
20-
@output
21-
@render.plot
22-
def distPlot():
23-
data = np.random.normal(size=input.obs())
24-
fig, ax = plt.subplots()
25-
ax.hist(data)
26-
return fig
276

28-
app = App(app_ui, server)',
29-
file.path(app_dir, "app.py")
7+
## Create a simple Shiny for Python app from a template ----
8+
writeLines(
9+
readLines(system.file("examples", "shiny", "python", "hello-docker-plain", "app.py", package = "shinydocker")),
10+
file.path(app_dir, "app.py")
3011
)
3112

32-
# Export the app
13+
## Export the app ----
3314
shinydocker::export(app_dir, run = TRUE, detach = TRUE)
3415

3516
# Stop the container
36-
stop_container(app_dir)
17+
shinydocker::stop_container(app_dir)
3718

3819
# Restart the container:
3920
shinydocker::run_container(app_dir, detach = TRUE)
@@ -44,4 +25,4 @@ shinydocker::run_container(app_dir, detach = TRUE)
4425
# Build Docker image
4526
# shinydocker::build_image(app_dir)
4627
# Run the containerized app
47-
# shinydocker::run_container(app_dir, detach = TRUE)
28+
# shinydocker::run_container(app_dir, detach = TRUE)

inst/demo-r-shiny-containerization.R

+13-29
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,26 @@
1-
# Create a simple R Shiny app
1+
# Quick demo of containerizing a simple R Shiny app
2+
3+
## Create a simple R Shiny app ----
24
app_dir <- tempfile()
35
dir.create(app_dir)
46
writeLines(
5-
'library(shiny)
6-
7-
ui <- fluidPage(
8-
titlePanel("Hello Docker"),
9-
sidebarLayout(
10-
sidebarPanel(
11-
sliderInput("obs", "Number of observations:",
12-
min = 1, max = 1000, value = 500)
13-
),
14-
mainPanel(
15-
plotOutput("distPlot")
16-
)
17-
)
18-
)
19-
20-
server <- function(input, output) {
21-
output$distPlot <- renderPlot({
22-
hist(rnorm(input$obs))
23-
})
24-
}
25-
26-
shinyApp(ui = ui, server = server)',
7+
readLines(system.file("examples", "shiny", "r", "hello-docker-ggplot2", "app.R", package = "shinydocker")),
278
file.path(app_dir, "app.R")
289
)
2910

30-
# Export the app
31-
shinydocker::export(app_dir, run = TRUE, detach = TRUE)
11+
## Export the app ----
12+
container <- shinydocker::export(app_dir, run = TRUE, detach = TRUE)
13+
14+
## Stop the container ----
15+
shinydocker::stop_container()
3216

33-
## Alternatively, steps can be run separately ----
17+
# Alternatively, steps can be run separately ----
3418

35-
# Create Docker configuration
19+
## Create Docker configuration ----
3620
dockerize(app_dir)
3721

38-
# Build Docker image
22+
## Build Docker image ----
3923
build_image(app_dir)
4024

41-
# Run the containerized app
25+
## Run the containerized app ----
4226
run_container(app_dir, detach = TRUE)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from shiny import App, ui, render
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
5+
app_ui = ui.page_fluid(
6+
ui.panel_title("Hello Docker"),
7+
ui.layout_sidebar(
8+
ui.sidebar(
9+
ui.input_slider("obs", "Number of observations:", min=1, max=1000, value=500)
10+
),
11+
ui.output_plot("distPlot")
12+
)
13+
)
14+
15+
def server(input, output, session):
16+
@output
17+
@render.plot
18+
def distPlot():
19+
data = np.random.normal(size=input.obs())
20+
fig, ax = plt.subplots()
21+
ax.hist(data)
22+
return fig
23+
24+
app = App(app_ui, server)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
library(shiny)
2+
library(ggplot2)
3+
4+
ui <- fluidPage(
5+
titlePanel("Hello Docker"),
6+
sidebarLayout(
7+
sidebarPanel(
8+
sliderInput("obs", "Number of observations:",
9+
min = 1, max = 1000, value = 500)
10+
),
11+
mainPanel(
12+
plotOutput("distPlot")
13+
)
14+
)
15+
)
16+
17+
server <- function(input, output) {
18+
output$distPlot <- renderPlot({
19+
ggplot(data.frame(x = rnorm(input$obs))) + aes(x = x) +
20+
geom_histogram()
21+
})
22+
}
23+
24+
shinyApp(ui = ui, server = server)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
library(shiny)
2+
3+
ui <- fluidPage(
4+
titlePanel("Hello Docker"),
5+
sidebarLayout(
6+
sidebarPanel(
7+
sliderInput("obs", "Number of observations:",
8+
min = 1, max = 1000, value = 500)
9+
),
10+
mainPanel(
11+
plotOutput("distPlot")
12+
)
13+
)
14+
)
15+
16+
server <- function(input, output) {
17+
output$distPlot <- renderPlot({
18+
hist(rnorm(input$obs))
19+
})
20+
}
21+
22+
shinyApp(ui = ui, server = server)

0 commit comments

Comments
 (0)