【问题标题】:Insert new features from a selectInput in shiny从闪亮的 selectInput 中插入新功能
【发布时间】:2020-08-24 17:32:14
【问题描述】:

朋友们可以帮助我编写下面的闪亮代码。它是用于操作的可执行代码。我设法正常生成散点图,它根据我的 SliderInput 变化。就我而言,我正在生成集群。如果滑块输入选择为 5,散点图将生成 5 个聚类,依此类推。这里一切都很好。我还在滑块输入下方做了一个 selectInput 来显示特定集群的地图。但是,我无法为特定集群生成散点图,也就是说,如果它在我的 selectInput 中选择了 2,我希望它只显示集群 2 的地图。您能帮我解决这个问题吗?

library(shiny)
library(ggplot2)
library(rdist)
library(geosphere)
library(kableExtra)
library(readxl)
library(tidyverse)
library(DT)
library(shinythemes)



function.cl<-function(df,k,Filter1,Filter2,Filter3){

  #database df
  df<-structure(list(Properties = c(1,2,3,4,5), 
                     Latitude = c(-23.8, -23.8, -23.9, -23.9, -23.9), 
                     Longitude = c(-49.6, -49.6, -49.6, -49.6, -49.6), 
                     Waste = c(526, 350, 526, 469, 285)), class = "data.frame", row.names = c(NA, -5L))

  #clusters
  coordinates<-df[c("Latitude","Longitude")]
  d<-as.dist(distm(coordinates[,2:1]))
  fit.average<-hclust(d,method="average") 
  clusters<-cutree(fit.average, k) 
  nclusters<-matrix(table(clusters))  
  df$cluster <- clusters 

  #database df1  
  df1<-df[c("Latitude","Longitude")]
  df1$cluster<-clusters

  #Table to join df and df1
  data_table <- Reduce(merge, list(df, df1))


  #Scatter Plot for all
  suppressPackageStartupMessages(library(ggplot2))
  g<-ggplot(data=df1,  aes(x=Longitude, y=Latitude,  color=factor(clusters))) + geom_point(aes(x=Longitude, y=Latitude), size = 4)
  plotGD<-g

  #Scatter Plot for specific cluster
  suppressPackageStartupMessages(library(ggplot2))
  g<-ggplot(data=df1[df1$cluster == Filter3,],  aes(x=Longitude, y=Latitude,  color=factor(clusters))) + geom_point(aes(x=Longitude, y=Latitude), size = 4)
  plotGD1<-g

  return(list(
    "Plot" = plotGD,
    "Plot1" = plotGD1,
    "Data"=data_table
  ))
}

ui <- bootstrapPage(
  navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
             "Cl", 

             tabPanel("Solution",
                      sidebarLayout(
                        sidebarPanel(
                          radioButtons("filter1", h3("Select properties"),
                                       choices = list("All properties" = 1, 
                                                      "Exclude properties" = 2),
                                       selected = 1),

                          radioButtons("filter2", h3("Select properties"),
                                       choices = list("All properties" = 1, 
                                                      "Exclude properties" = 2),
                                       selected = 1),
                          tags$hr(),
                          tags$b(h3("Satisfied?")),
                          tags$b(h5("(a) Choose other filters")),
                          tags$b(h5("(b) Choose clusters")),  
                          sliderInput("Slider", h5(""),
                                      min = 2, max = 5, value = 3),

                        ),

                        mainPanel(
                          tabsetPanel(      
                            tabPanel("Solution", plotOutput("ScatterPlot"))))

                      ))),


  tabPanel("",
           sidebarLayout(
             sidebarPanel(
               selectInput("Filter3", label = h4("Select just one cluster to show"),""),
             ),

             mainPanel(
               tabsetPanel(
                 tabPanel("Map", plotOutput("ScatterPlot1"))))

           )))



server <- function(input, output, session) {


  Modelcl<-reactive(function.cl(df,input$Slider,1,1,input$Filter3))


  output$ScatterPlot <- renderPlot({
    Modelcl()[[1]]
  })

  output$ScatterPlot1 <- renderPlot({
    Modelcl()[[2]]
  })

  observeEvent(c(df,input$Slider,1,1),{
    abc <- req(Modelcl()$Data)
    updateSelectInput(session,'Filter3',
                      choices=sort(unique(abc$cluster)))
  }) 


}

shinyApp(ui = ui, server = server)

非常感谢!

【问题讨论】:

  • 谢谢本!我会试着做你说的调整,看看我能不能得到一些东西。抱歉,在这种情况下,我没有做一个最小的例子,我没有太多的闪亮技能,但我会尝试根据你的建议进行调整。非常感谢
  • 本,我尽可能地进步了。该代码是可执行的。你可以看看。谢谢朋友
  • 对不起,本,我错了。这是df1。我更新了上面的代码,我相信这是你想让我测试的。我为所有人制作了一个ggplot,为一个特定的集群制作了一个ggplot。但是,为特定集群生成地图时出现错误:错误:美学必须是长度 1 或与数据相同 (2):颜色。

标签: r shiny


【解决方案1】:

一些想法:

  • 您的 observeEvent 可以仅依赖于 input$Slider - 我不确定那里的其他数字和数据框是什么意思

  • inputFilter3 传递给您的 function.cl - 再次记住,由于该函数涉及响应式输入,您可能希望在 server 中作为响应式表达式

  • 您需要过滤特定聚类图的数据,例如:df1[df1$cluster == Filter3,]

  • 要在两个绘图之间使用相同的配色方案,您可以制作一个颜色矢量(使用您想要的任何调色板),然后使用 scale_color_manual

  • 引用它

这似乎对我有用。对于您的下一个示例,如果可能,请尝试简化为“最小”工作示例以演示问题所在。祝你好运!

library(shiny)
library(ggplot2)
library(rdist)
library(geosphere)
library(kableExtra)
library(readxl)
library(tidyverse)
library(DT)
library(shinythemes)

function.cl<-function(df,k,Filter1,Filter2,Filter3){

  #database df
  df<-structure(list(Properties = c(1,2,3,4,5), 
                     Latitude = c(-23.8, -23.8, -23.9, -23.9, -23.9), 
                     Longitude = c(-49.6, -49.6, -49.6, -49.6, -49.6), 
                     Waste = c(526, 350, 526, 469, 285)), class = "data.frame", row.names = c(NA, -5L))

  #clusters
  coordinates<-df[c("Latitude","Longitude")]
  d<-as.dist(distm(coordinates[,2:1]))
  fit.average<-hclust(d,method="average") 
  clusters<-cutree(fit.average, k) 
  nclusters<-matrix(table(clusters))  
  df$cluster <- clusters 

  #all cluster data df1 and specific cluster df_spec_clust
  df1<-df[c("Latitude","Longitude")]
  df1$cluster<-as.factor(clusters)
  df_spec_clust <- df1[df1$cluster == Filter3,]

  #Table to join df and df1
  data_table <- Reduce(merge, list(df, df1))

  #Setup colors to share between both plots
  my_colors <- rainbow(length(df1$cluster))
  names(my_colors) <- df1$cluster

  #Scatter Plot for all clusters
  g <- ggplot(data = df1,  aes(x=Longitude, y=Latitude, color=cluster)) + 
    geom_point(aes(x=Longitude, y=Latitude), size = 4) +
    scale_color_manual("Legend", values = my_colors)
  plotGD <- g

  #Scatter Plot for specific cluster
  g <- ggplot(data = df_spec_clust,  aes(x=Longitude, y=Latitude, color=cluster)) + 
    geom_point(aes(x=Longitude, y=Latitude), size = 4) +
    scale_color_manual("Legend", values = my_colors)
  plotGD1 <- g

  return(list(
    "Plot" = plotGD,
    "Plot1" = plotGD1,
    "Data" = data_table
  ))
}

ui <- bootstrapPage(
  navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
             "Cl", 
             tabPanel("Solution",
                      sidebarLayout(
                        sidebarPanel(
                          radioButtons("filter1", h3("Select properties"),
                                       choices = list("All properties" = 1, 
                                                      "Exclude properties" = 2),
                                       selected = 1),

                          radioButtons("filter2", h3("Select properties"),
                                       choices = list("All properties" = 1, 
                                                      "Exclude properties" = 2),
                                       selected = 1),
                          tags$hr(),
                          tags$b(h3("Satisfied?")),
                          tags$b(h5("(a) Choose other filters")),
                          tags$b(h5("(b) Choose clusters")),  
                          sliderInput("Slider", h5(""),
                                      min = 2, max = 5, value = 3),
                        ),
                        mainPanel(
                          tabsetPanel(      
                            tabPanel("Solution", plotOutput("ScatterPlot"))))

                      ))),
  tabPanel("",
           sidebarLayout(
             sidebarPanel(
               selectInput("Filter3", label = h4("Select just one cluster to show"),""),
             ),
             mainPanel(
               tabsetPanel(
                 tabPanel("Map", plotOutput("ScatterPlot1"))))
           )))

server <- function(input, output, session) {

  Modelcl<-reactive({
    function.cl(df,input$Slider,1,1,input$Filter3)
  })

  output$ScatterPlot <- renderPlot({
    Modelcl()[[1]]
  })

  output$ScatterPlot1 <- renderPlot({
    Modelcl()[[2]]
  })

  observeEvent(input$Slider, {
    abc <- req(Modelcl()$Data)
    updateSelectInput(session,'Filter3',
                      choices=sort(unique(abc$cluster)))
  }) 

}

shinyApp(ui = ui, server = server)

【讨论】:

猜你喜欢
  • 2023-04-06
  • 2015-05-07
  • 1970-01-01
  • 2014-01-20
  • 2018-07-09
  • 2015-10-27
  • 1970-01-01
  • 1970-01-01
  • 2021-09-23
相关资源
最近更新 更多