0

I'm trying to make a flexdashboard using IMDb data, that has an interactive jitter plot where you can change the x and y for visualizing hierarchical clustering result. The code that I've already made can change only the x and number of k. I think I should use reactive function but I don't really understand in using that. I've already tried many other ways from youtube and some documentary but still can't change the y. Here is layout of my dashboard, The y stuck at the runtime variable

data=df %>% 
  select(Rating, Votes, Gross, Runtime, Metascore)

selectInput("x", label = "X : ",choices = names(data))

selectInput("y", label = "Y : ",choices = names(data))

sliderInput('k',"Cluster",min = 2,max = 10, value = 6)

selectedData=reactive({
  data %>% select(input$x, input$y)
})

data_scaled=scale(data)

dist_data=dist(data_scaled, method='euclidean')

hc_data=hclust(dist_data, method = "average")

renderPlot({
  ggplot(selectedData(),
         aes(x=!!rlang::sym(input$x), y=!!rlang::sym(input$y),
             col=factor(cutree(hc_data, k=input$k))))+
    geom_jitter(size=5, alpha=0.5 )+
    labs(col="Cluster")
})

1 Answers1

0

Here is an alternative example that seems to work, using the diamonds dataset from ggplot2. My guess is that the scaling and clustering steps take so long to run that the the y reactive only appears not to work. I would suggest pre-processing your data if app run times are a problem.


data=diamonds[1:1e3,] %>% 
    dplyr::select(where(is.numeric))

selectInput("x", label = "X : ",choices = names(data))

selectInput("y", label = "Y : ",choices = names(data))

sliderInput('k',"Cluster",min = 2,max = 10, value = 6)

data_scaled=scale(data)

dist_data=dist(data_scaled, method='euclidean')

hc_data=hclust(dist_data, method = "average")

renderPlot({
    ggplot(data,
           aes(x=!!rlang::sym(input$x), y=!!rlang::sym(input$y),
               col=factor(cutree(hc_data, k=input$k))))+
        geom_jitter(size=5, alpha=0.5 )+
        labs(col="Cluster")
})
stchlk
  • 126
  • 1
  • 7