簡體   English   中英

僅當 fileInput 加載到 Shiny 時才顯示滑塊輸入

[英]Show sliderInput only when fileInput is loaded into Shiny

我希望僅在通過 fileInput 加載數據庫時才顯示 Slider。 另外,作為sliderInput的最大值(max),我希望它是數據庫中屬性的總數,也就是說,它可以根據數據庫而變化。 你能幫我解決這個問題嗎? 可以從以下網站下載測試數據庫: https://github.com/JovaniSouza/JovaniSouza5/blob/master/Example.xlsx

可執行代碼如下:

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)

這可以通過uiOutputrenderUi函數來實現。 uiOutput在 UI 中放置一個占位符,當用戶加載數據集時,該占位符通過renderUi填充。

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]]
  })
  
  
}

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM