0

我正在尝试使用 IMDb 数据制作一个 flexdashboard,它具有交互式抖动图,您可以在其中更改 x 和 y 以可视化层次聚类结果。我已经编写的代码只能更改 x 和 k 的数量。我认为我应该使用reactive函数,但我并不真正了解如何使用它。我已经从 youtube 和一些纪录片中尝试了许多其他方法,但仍然无法更改 y。这是我的仪表板的布局,y 卡在运行时变量上

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")
})

4

1 回答 1

0

这是一个似乎可行的替代示例,使用diamonds来自ggplot2. 我的猜测是缩放和集群步骤需要很长时间才能运行,以至于 y 反应似乎不起作用。如果应用程序运行时间有问题,我建议预处理您的数据。


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")
})
于 2021-01-02T17:12:41.067 回答