server <- function()

in ml_example/breast-cancer-prediction/app.R [83:149]


server <- function(input, output) {
    
    # Compute the formula text ----
    # This is in a reactive expression since it is shared by the
    # output$caption and output$mpgPlot functions
    formulaText <- reactive({
        paste(input$variable_y, "~", input$variable_x)
    })

    # Compute the formula text ----
    # This is in a reactive expression since it is shared by the
    # output$caption and output$mpgPlot functions
    total_count <- reactive({
        data.frame(Class = colnames(prediction),
                   Count = c(sum(prediction$malignant<input$threshold),
                             sum(prediction$malignant>=input$threshold)))
    })
    
    # Compute the formula text ----
    # This is in a reactive expression since it is shared by the
    # output$caption and output$mpgPlot functions
    threshold_proba <- reactive({
        cbind(Prediction = ifelse(prediction$malignant>=input$threshold, 
                                  "malignant", "benign"),
              test_data)
    })
    
    # return prediction summary
    output$predictions <- renderTable({
        total_count()
    })
        
    # Return the formula text for printing as a caption ----
    output$caption <- renderText({
        "Breast cancer test data summary"
    })
    
    # Generate a summary of the dataset ----
    # The output$summary depends on the datasetInput reactive
    # expression, so will be re-executed whenever datasetInput is
    # invalidated, i.e. whenever the input$dataset changes
    output$summary <- renderPrint({
        summary(test_data)
    })
    
    # Return the formula text for printing as a caption ----
    output$formula <- renderText({
        formulaText()
    })
    
    # Generate a plot of the requested variable against mpg ----
    # and only exclude outliers if requested
    output$scatterPlot <- renderPlot({
        plot(as.formula(formulaText()), data = threshold_proba())
        #ggplot(test_data, aes(x=input$variable_x, y=input$variable_y)) + geom_point()
    })
    
    output$info <- renderTable({
        # With base graphics, need to tell it what the x and y variables are.
        #nearPoints(test_data, input$plot_click, 
        #           xvar = "Cl.thickness", yvar = "Epith.c.size")
        # nearPoints() also works with hover and dblclick events
        brushedPoints(threshold_proba(), input$plot_brush, 
                      xvar = input$variable_x, yvar = input$variable_y)
    })
    
}