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)
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]]
})
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.