1

I want to display a table (with a column name, a number of unique values, a unique value) for a zero variance dataset (no. of unique values =< 1). But when I run this code in shiny, I get this error.

Warning: Error in : arguments imply differing number of rows: 2, 5, 3, 6

Example output

library(shiny)
library(shinydashboard)
library(DT)
library(caret)

ui <- dashboardPage(
   skin = "black",
   dashboardHeader(title = "data"),
   dashboardSidebar(
      sidebarMenu(
         fileInput("Table1", "Historical Data"),
         radioButtons("sep", "Separator", choices = c(Comma = ",", Semicolon = ";", 
         Tab = "\t", Pipe="|"), selected = ","),
            menuItem("Data Overview", tabName = "data_view", icon = icon("table"))
       )
    ),
   dashboardBody(
      tabItems(
         tabItem(tabName = "data_view",
            fluidRow(
               box(title = "Zero Variance Variables", width = 12, solidHeader = TRUE,
               status = "primary", collapsible = TRUE, DT::DTOutput("uniq_data")))
             )
        )
    )
)

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

  ## initialize
  rv <- reactiveValues()

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

  col_uniq <- reactive({

    nzv <- nearZeroVar(rv$Train)
    df1 <- rv$Train[, nzv] 

    d <- data.frame(names(df1), sapply(df1, function(x) length(unique(x[!is.na(x)]))))
    names(d) <- c("Variable", "No. of Uniques") 

    as <- sapply(df1, function(x) length(unique(x))) > 1
    as1 <- df1[, as]

    as2 <- data.frame(names(as1),sapply(as1, function(x) unique(x)))
    names(as2) <- c("Variable", "Unique Value")

    join <- merge(d, as2, by.x = "Variable", by.y = "Variable", all.x = TRUE)

    return(join)
  })

  output$uniq_data <- DT::renderDT({
    dclas <- DT::datatable(col_uniq(), options = list(scrollX = TRUE))
  })
}
shinyApp(ui, server)  

Thanks, Balaji

Ekatef
  • 1,061
  • 1
  • 9
  • 12
Balaji N
  • 363
  • 1
  • 5
  • 15
  • 1
    Please consider creating a reproducible example, see [here](https://stackoverflow.com/questions/48343080/how-to-convert-a-shiny-app-consisting-of-multiple-files-into-an-easily-shareable) for tips on how to create a reproducible example in Shiny. We do not have your csv file, better to just hard-code the table so we can run your code by just copy-pasting it. That makes it a lot easier to help! – Florian Apr 25 '18 at 09:18

0 Answers0