2016-07-26 37 views
2

我有一個反應函數(datasplit)除了根據值sliderInput對數據進行子集化之外什麼都不做。 而我的第二個反應函數(selectedData)應該從第一個反應函數中獲取子集數據,並根據SelectInput再次過濾列。在反應函數之間傳遞數據 - Shiny R

最後根據這些數據進行聚類。

什麼作品是反應函數2,它從輸入輸入,但在第一個反應函數失敗。

任何幫助非常感謝。我不能上傳數據,但參考我的閃亮代碼。

閃亮代碼:

 library(shiny) 
     ui <- fluidPage(
      selectInput("xcol", "X Variable", names(data[,c(2)])), 
      selectInput("ycol", "Y Variable", names(data[,c(1)])), 
      sliderInput(inputId = "cost", 
         label = "Kosten", 
         value = 1000, min = 1, max = 5000), 
      sliderInput(inputId = "count", 
         label = "Anzahl Fälle", 
         value = 500, min = 1, max = 2000), 
      numericInput("clusters", "Anzahl Cluster", 3, min=1, max= 9), 
      plotOutput(outputId = "plot1") 

     ) 

     server <- function(input, output){ 
      DealerDF <- read.csv(file = "dealer_category.csv",header = T) 
      data <- DealerDF 

      datasplit <- reactive({ 
      subset(data, input$xcol() < input$cost() & input$ycol() < input$count()) 


      }) 


      selectedData <- reactive({ 
      #this seems to be not working 
      datasplit()[, c(input$xcol(), input$ycol())] 
      }) 

      clusters <- reactive({ 
      kmeans(selectedData(), input$clusters) 
      }) 

      output$plot1 <- renderPlot({ 

      if (!is.null(selectedData())) { 
       par(mar= c(5.1, 4.1,0,1)) 
       plot(selectedData(), 
        col= clusters()$cluster, 
        pch= 20, cex=3) 
       points(clusters()$centers, pch=4, cex=4, lwd=4) 
      } 
      }) 

     } 

     shinyApp(ui = ui, server = server) 

數據:

 cnt av_cst 
     479 2.359.695 
     479 83.439 
     475 891.863 
     474 2.496.681 
     474 97.654 
     474 821.163 
     473 1.650.016 
     473 143.724 
     472 90.398 
     470 98.745 
     468 681.947 
     468 97.392 
     467 435.477 
     467 97.657 
     466 160.547 
     463 98.454 
     30 24.936 
     30 29.432 
     30 1.599.577 
     30 227.073 
     30 227.887 
     30 187.147 
     30 89.697 
     30 615.427 
     30 32.398 
     30 15.133 
     30 24.445.753 
     30 25.944 
     30 344.933 
     30 10.237 
     30 15.86 
     17082 30.425 
     11358 75.541 
     9788 30.638 
     9667 30.381 
     7302 73.051 
     6849 1.009.921 
     6299 124.441 
     6018 30.158 
     5646 124.569 
     5383 1.133.911 
     5381 30.278 
     4357 123.213 
     3989 3.065 
+0

什麼是外'數據'? –

+0

@PorkChop數據是帶有25列的數據框 – user3560220

+0

請提供數據 –

回答

0

因爲我沒有我無法測試下面的代碼中的數據。此外,我不明白爲什麼你把你的輸入(輸入$ col)作爲函數(輸入$ col())。我已經多次實現了這個嵌套子集。您可以添加多個過濾器。希望您的子功能,工作無故障,下面會有所幫助:

DealerDF <- read.csv(file = "dealer_category.csv",header = T) 
      data <- reactive({ 
       DealerDF 
      }) 

      datasplit <- reactive({ 
      IstSubset <- subset(data(), input$xcol() < input$cost() & input$ycol() < input$count()) 
      selectedData <- IstSubset[,c(input$xcol(), input$ycol())] 
      clusters <- kmeans(selectedData, input$clusters) 
      clusters 
      }) 


      output$plot1 <- renderPlot({ 
      if (!is.null(dataSplit())) { 
       par(mar= c(5.1, 4.1,0,1)) 
       plot(datasplit(), 
        col= clusters()$cluster, 
        pch= 20, cex=3) 
       points(clusters()$centers, pch=4, cex=4, lwd=4) 
      } 
      }) 

如果你沒有得到的情節輸出,你可能會考慮移動計算的Kmeans反應環

+0

我嘗試了你的方法。但仍然失敗。我看到的唯一問題是,子集功能從UI執行輸入功能。我用樣本數據更新了這個問題。你也可以。如果我硬編碼列,那麼它的工作原理,但是當我從UI取得時,它失敗 – user3560220

+0

可以使用dput函數發佈一小部分數據,比如說3-4行 – Apricot

+0

,感謝你對解決問題。我有問題中的數據。 2列(cnt,av_cst),用於聚類。你想上傳和發送鏈接? – user3560220