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)
uiOutput
andrenderUI
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