【问题标题】:How to change the y in flexdashboard selectInput如何更改 flexdashboard selectInput 中的 y
【发布时间】:2021-04-08 22:42:08
【问题描述】:

我正在尝试使用 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")
})

【问题讨论】:

    标签: r reactive flexdashboard selectinput


    【解决方案1】:

    这是一个似乎可行的替代示例,使用来自ggplot2diamonds 数据集。我的猜测是缩放和集群步骤需要很长时间才能运行,以至于 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-05-17
      • 2021-05-06
      • 1970-01-01
      • 2021-11-08
      • 2018-02-09
      • 2013-10-01
      • 2018-10-27
      • 2021-07-26
      • 2020-12-09
      相关资源
      最近更新 更多