1
votes

I would like the Slider to be shown only when the database via fileInput is loaded. In addition, as the maximum value (max) of the sliderInput, I would like it to be the total number of properties in the database, that is, it can vary depending on the database. Could you help me on this issue? A test database can be downloaded from the following website: https://github.com/JovaniSouza/JovaniSouza5/blob/master/Example.xlsx

The executable code is below:

library(shiny)
library(ggplot2)
library(shinythemes)
library(rdist)
library(geosphere)
library(rgdal)

function.cl<-function(df,k){
  
  #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)
  
  #Colors
  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
  
  
  return(list(
    "Plot" = plotGD
  ))
}

ui <- bootstrapPage(
  navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
             "Cl", 
             tabPanel("Solution",
                      fileInput("data", h3("Excel import")), 
                      sidebarLayout(
                        sidebarPanel(
                          
                          sliderInput("Slider", h5(""),
                                      min = 2, max = 4, value = 3),
                        ),
                        mainPanel(
                          tabsetPanel(      
                            tabPanel("Solution", plotOutput("ScatterPlot"))))
                        
                      ))))

server <- function(input, output, session) {
  
  v <- reactiveValues(df = NULL)
  observeEvent(input$data, {
    v$df <- read_excel(input$data$datapath)
  })
  
  
  Modelcl<-reactive({if (!is.null(v$df)) {
    function.cl(v$df,input$Slider)
  }
  })
  
  
  output$ScatterPlot <- renderPlot({
    Modelcl()[[1]]
  })
  

}

shinyApp(ui = ui, server = server)
1
You could use uiOutput and renderUI to create a sliderInput only if the data loaded is not null. If you need more help, please include a reproducible dataset to your example, links are problematic because they can be broken in the future. Also, you should consider reducing your example because there is a lot of unnecessary stuff for your question.bretauv
You could also hide and show your inputs using shinyjs.thmschk

1 Answers

1
votes

This could be achived via the uiOutput and renderUi functions. uiOutput puts a placeholder in the UI which is filled via renderUi when the User has loaded a dataset.

ui <- bootstrapPage(
  navbarPage(
    theme = shinytheme("flatly"), collapsible = TRUE,
    "Cl",
    tabPanel(
      "Solution",
      fileInput("data", h3("Excel import")),
      sidebarLayout(
        sidebarPanel(
          uiOutput("slider")
        ),
        mainPanel(
          tabsetPanel(
            tabPanel("Solution", plotOutput("ScatterPlot"))
          )
        )
      )
    )
  )
)

server <- function(input, output, session) {
  
  output$slider <- renderUI({
    req(input$data)
    sliderInput("Slider", h5(""), min = 2, max = max_k(), value = 3, step = 1)
  })
  
  v <- reactiveValues(df = NULL)
  observeEvent(input$data, {
    v$df <- read_excel(input$data$datapath)
  })
  
  max_k <- reactive({
    req(input$data)
    nrow(v$df)
  })

  Modelcl<-reactive({
    req(input$data)
    req(input$Slider)
    function.cl(v$df, input$Slider)
  })
  
  
  output$ScatterPlot <- renderPlot({
    Modelcl()[[1]]
  })
  
  
}

enter image description here