-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinteractive-task-map.Rmd
92 lines (79 loc) · 2.13 KB
/
interactive-task-map.Rmd
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
---
output: html_document
runtime: shiny
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(ggplot2)
library(e1071)
library(plotly)
library(dplyr)
library(tidyverse)
```
```{r load-data, echo=FALSE, message=FALSE, warning=FALSE}
task_map <- read_csv('./task_map.csv')
```
```{r interactive_map, echo = F}
task_map_pca <- prcomp(task_map[, -1], scale = TRUE)
task_map_pca_result <- as.data.frame(task_map_pca$x[, 1:2])
task_map_pca_result$TaskName <- task_map$task
# Define the UI
ui <- fluidPage(
titlePanel("Interactive Task Map"),
sidebarLayout(
sidebarPanel(
numericInput("k_value", "Enter the value of k:", min = 1, value = 2),
actionButton("cluster_btn", "Apply Clustering"),
actionButton("reset_btn", "Reset Clustering")
),
mainPanel(
plotlyOutput("map_plot")
)
)
)
# Define the server
server <- function(input, output) {
# Initialize clustering data frame
clustering_data <- reactiveValues(data = NULL)
# Apply k-means clustering
applyClustering <- function(k) {
if (k > 1) {
cluster_result <- kmeans(task_map[, -1], centers = k)
clustering_data$data <- task_map_pca_result
clustering_data$data$cluster <- as.factor(cluster_result$cluster)
}
}
# Default clustering
default_clustering <- reactive({
applyClustering(2) # Set the default value of k
clustering_data$data
})
# Apply k-means clustering when the button is clicked
observeEvent(input$cluster_btn, {
k <- input$k_value
applyClustering(k)
})
# Reset clustering
observeEvent(input$reset_btn, {
clustering_data$data <- task_map_pca_result
clustering_data$data$cluster <- NULL
})
# Plotly map plot
output$map_plot <- renderPlotly({
plot_data <- clustering_data$data
ggplotly(
ggplot(plot_data, aes(x = PC1, y = PC2, color = cluster, text = TaskName)) +
geom_point(size = 3) +
theme_minimal()
)
})
# Update default clustering when task_map_pca_result changes
observe({
if (is.null(clustering_data$data)) {
clustering_data$data <- default_clustering()
}
})
}
# Run the Shiny app
shinyApp(ui, server)
```