Skip to content

Commit 4c5aa1d

Browse files
author
Wasin Pipattungsakul
committed
separate tabs into files
1 parent 9f68811 commit 4c5aa1d

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

src/index.R

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
library(shiny)
22

3+
source("./tab1.R")
4+
source("./tab2.R")
5+
source("./kmean_cluster.R")
6+
37
ui <- fluidPage(
48
# external styles
59
includeCSS("./www/styles.css"),
@@ -20,9 +24,9 @@ ui <- fluidPage(
2024
tabsetPanel(
2125
type = "tabs",
2226
# add tabs here
23-
tabPanel("<Tab 1>", "<Content in tab 1>"),
24-
tabPanel("<Tab 2>", "<Content in tab 2>"),
25-
tabPanel("<Tab 3>", "<Content in tab 3>"),
27+
tab1,
28+
tab2,
29+
kmean_cluster_tab,
2630
)
2731
)
2832

@@ -37,6 +41,9 @@ server <- function(input, output, session) {
3741
)
3842
}) |>
3943
bindEvent(input$about)
44+
45+
# tab io
46+
kmean_cluster_server(input, output, session)
4047
}
4148

4249
# export app

src/kmean_cluster.R

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
library(shiny)
2+
3+
.parameters <- setdiff(names(iris), "Species")
4+
5+
kmean_cluster_tab <- tabPanel("K-Mean Clustering", pageWithSidebar(
6+
headerPanel("Iris k-means clustering"),
7+
sidebarPanel(
8+
selectInput("xcol", "X Variable", .parameters),
9+
selectInput("ycol", "Y Variable", .parameters, selected = .parameters[[2]]),
10+
numericInput("clusters", "Cluster count", 3, min = 1, max = 9)
11+
),
12+
mainPanel(
13+
plotOutput("plot1")
14+
)
15+
))
16+
17+
kmean_cluster_server <- function(input, output, session) {
18+
# Combine the selected variables into a new data frame
19+
selected_data <- reactive({
20+
iris[, c(input$xcol, input$ycol)]
21+
})
22+
23+
clusters <- reactive({
24+
kmeans(selected_data(), input$clusters)
25+
})
26+
27+
output$plot1 <- renderPlot({
28+
palette(c(
29+
"#E41A1C", "#377EB8", "#4DAF4A", "#984EA3",
30+
"#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999"
31+
))
32+
33+
par(mar = c(5.1, 4.1, 0, 1))
34+
plot(selected_data(),
35+
col = clusters()$cluster,
36+
pch = 20, cex = 3
37+
)
38+
points(clusters()$centers, pch = 4, cex = 4, lwd = 4)
39+
})
40+
}

src/tab1.R

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
library(shiny)
2+
3+
tab1 <- tabPanel("<Tab 1>", "<Content in tab 1>")

src/tab2.R

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
library(shiny)
2+
3+
tab2 <- tabPanel("<Tab 2>", "<Content in tab 2>")

0 commit comments

Comments
 (0)