Choosing which metrics to be included in the table (Shiny)

Viewed 59

I have a shiny app here, which after uploading a dataset, can calculate Rsq, RMSE, and MAE, based on the chosen x and y variables.

I would like to give the user the ability to choose the metrics as well, so right now, when the variables are chosen, all the metrics are calculated, however with the help of the "selectizeInput" the user should be able to choose what is included in the table, so if the user only wants to see "RMSE", so only calculated values for RMSE is shown and generated in the table.

Thanks for all help

library(shiny)
library(DT)
library(caret)
library(dplyr)

allstats <- c("Rsq",
              "RMSE",
              "MAE",
              "iqRMSE",
              "RAC",
              "CCC")





ui <- shinyUI(fluidPage(
  titlePanel("Metrics"),
  tabsetPanel(
    tabPanel("Upload File",
             titlePanel("Uploading Files"),
             sidebarLayout(
               sidebarPanel(
                 fileInput('file1', 'Choose CSV File',
                           accept=c('text/csv', 
                                    'text/comma-separated-values,text/plain', 
                                    '.csv')),
                 tags$br(),
                 checkboxInput('header', 'Header', TRUE),
                 radioButtons('sep', 'Separator',
                              c(Comma=',',
                                Semicolon=';',
                                Tab='\t'),
                              ','),
                 radioButtons('quote', 'Quote',
                              c(None='',
                                'Double Quote'='"',
                                'Single Quote'="'"),
                              '"')
                 
               ),
               mainPanel(
                 tableOutput('contents')
               )
             )
    ),
    tabPanel("Statistics",
             sidebarLayout(
               sidebarPanel(
                 fluidRow(
                   column(6,
                          selectInput('xcol1', 'X Variable', "",width=140)),
                   column(6,
                          selectInput('ycol2', 'Y Variable', "", selected = "",width=140)
                   ),
                   selectizeInput("stat_metrics",label="Statistics to display",choices=allstats,
                                  selected=c("Rsq"),
                                  multiple=TRUE,
                                  options=list(plugins=list('drag_drop','remove_button')))
                   
                   
                 ),
                 fluidRow(
                   actionButton("calculate_1", "Calculate")
                 )
                 
               ),
               mainPanel(
                 DTOutput("table_met")
               )
             )
             
             
    )
    
  )
)
)

server <- shinyServer(function(input, output, session) {
  
  data <- reactive({ 
    req(input$file1) 
    
    inFile <- input$file1 
    
    df <- read.csv(inFile$datapath, header = input$header, sep = input$sep,
                   quote = input$quote)
    
    
    updateSelectInput(session, inputId = 'xcol1', label = 'X Variable',
                      choices = names(df), selected = names(df))
    updateSelectInput(session, inputId = 'ycol2', label = 'Y Variable',
                      choices = names(df), selected = names(df)[2])
    
    return(df)
  })
  
  output$contents <- renderTable({
    data()
  })
  
  
  
  
  
  tableR <- reactive({
    
    summ <-data() %>%
      summarise(
        Rsq = cor(.data[[input$xcol1]], .data[[input$ycol2]])^2,
        RMSE = RMSE(.data[[input$xcol1]], .data[[input$ycol2]]),
        MAE = MAE(.data[[input$xcol1]], .data[[input$ycol2]])

        
        
        
      ) %>%
      mutate_if(is.numeric, round, digits=2)
    
  }) |> bindEvent(input$calculate_1)
  
  
  
  
  output$table_met <-renderDT({
    tableR()
  })
  
  
  
  
})

shinyApp(ui, server)

0 Answers
Related