在反应函数之间传递数据 - Shiny R.

时间:2016-07-26 12:40:22

标签: r function shiny

我有一个反应函数(datasplit)除了基于值sliderInput对数据进行子集外,它什么也没做。 我的第二个反应函数(selectedData)应该从第一个反应函数中获取子集数据,然后再根据SelectInput过滤列。

最后根据这些数据进行聚类。

什么有效是反应函数2,它从 SelectInput 获取输入,但在第一个反应函数时失败。

任何帮助非常感谢。我无法上传数据,但可以参考我的闪亮代码。

闪亮的代码:

        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

1 个答案:

答案 0 :(得分:0)

由于我没有数据,因此无法测试以下代码。而且,我无法理解为什么要将输入(输入$ col)视为函数(输入$ col())。我已经多次实现了这个嵌套的子集。您可以添加多个过滤器。希望你的子集函数没有故障,以下内容将有所帮助:

class Abstract {
    virtual auto foo() -> int = 0;
};

class Concrete: public Abstract {
    int foo() { cout << "blah!" << endl; return 1; }
} instance;

如果你没有得到绘图输出,你可以考虑在反应循环之外移动kmeans计算

相关问题