如何更改 flexdashboard selectInput 中的 y

问题描述

我正在尝试使用 IMDb 数据制作一个 flexdashboard,它具有交互式抖动图,您可以在其中更改 x 和 y 以可视化层次聚类结果。我已经制作的代码只能更改 x 和 k 的数量。我想我应该使用 reactive 函数,但我真的不明白使用它。我已经从 youtube 和一些纪录片中尝试了许多其他方法,但仍然无法更改 y。这里是 layout of my dashboard,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")
})

解决方法

这是一个似乎有效的替代示例,它使用来自 diamondsggplot2 数据集。我的猜测是缩放和聚类步骤需要很长时间才能运行,以至于 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")
})

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...