1

I want to delete a selected variable from the dataset in R shinydashboard. Please find attached my code. But when I try to select a variable and delete,

if(input$imputation == "Delete Variable"){
  rv$Train <- rv$Train[,-(input$miss_var)]
  return(rv$Train)
}

I get the following error,

Warning: Error in -: invalid argument to unary operator

UI

Please find the ui part of the code,

library(shiny)
    library(shinydashboard)
    library(missForest)

    ui <- dashboardPage(skin = "black",
                        dashboardHeader(title = "Analytics Workbench 2.0", titleWidth = 250, dropdownMenuOutput("msgs")),

                        dashboardSidebar(
                          sidebarMenu(
                            fileInput("Table1", "Historical Data"),
                            radioButtons("sep", "Separator", choices = c(Comma = ",", Semicolon = ";", Tab = "\t", Pipe="|"), selected = ","),
                            menuItem("Variable Transformation", tabName = "transformation",icon = icon("exchange")))),

                        dashboardBody(
                          tabItems(
                            tabItem(tabName = "transformation",
                                    fluidRow(
                                      box(selectInput("miss_var", "Variable", c("1"="1","2"="2")), width = 2, status = "primary"),
                                      box(title = "Missing Value Imputation", width = 4,  status = "primary", solidHeader = TRUE, collapsible = TRUE,
                                          radioButtons("imputation", label = "", 
                                                       choices = list(Delete_Variable = "Delete Variable", Impute = "Impute"), selected = ""),
                                          actionButton("miss_imp", "Select"))))

                          )
                        )
    )

SERVER

Please find the server part of the code,

server <- function(input, output, session) { 

  ## initialize
  rv <- reactiveValues()

  ## update when dataset changes
  observe({
    rv$Train <- read.table(req(input$Table1)$datapath, fill = TRUE, header=T, 
                           sep=input$sep, na.strings = c(""," ",NA), 
                           stringsAsFactors = TRUE)
  }) 

  observe({

    is.miss <- sapply(rv$Train, function(y) sum(length(which(is.na(y))))) 
    dname3 <- names(rv$Train[, is.miss > 0])
    col_options3 <- list()
    col_options3[dname3] <- dname3

    updateSelectInput(session, "miss_var",
                      label = "Missing Variables",
                      choices = col_options3,
                      selected = "")   

  })

  observeEvent(input$miss_imp,{

    if(input$imputation == "Delete Variable"){
      rv$Train <- rv$Train[,-(input$miss_var)]
      return(rv$Train)
    } else if(input$imputation == "Impute"){
      missfor <- missForest(rv$Train)
      rv$Train <- data.frame(missfor$ximp)
      return(rv$Train)
    }
  })

}

shinyApp(ui, server) 

Thanks, Balaji

Balaji N
  • 243
  • 5
  • 14
  • 5
    Try to strip down your issue to the bare minimum, for example, you can try `mtcars[,-('mpg')]` and you will see that it gives the same error. That makes debugging a lot easier; now we know it has nothing to do with Shiny. If you want to drop columns by name, see for example [here](https://stackoverflow.com/questions/4605206/drop-data-frame-columns-by-name). – Florian Apr 17 '18 at 12:51
  • 1
    Possible duplicate of [Drop data frame columns by name](https://stackoverflow.com/questions/4605206/drop-data-frame-columns-by-name) – NicE Apr 17 '18 at 14:05

0 Answers0