Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect columnname displayed in dataTableOutput, when selectinput(multiple=T) - shiny

Tags:

r

shiny

I want to display a table showing duplicate count along with the user defined columns. I have selectinput option in the shiny app by which the user can select multiple columns to check duplicate combinations.

But when the user selects first column, incorrect column name is displayed. when two columns are selected, the column names are correct.

Please help me to find a solution for this issue. When user selects first column, correct column should be displayed.

code,

library(shiny)
library(shinydashboard)

ui <- dashboardPage(skin = "black",
                    dashboardHeader(title = "test"),
                    dashboardSidebar(
                      sidebarMenu(
                        menuItem("Complete", tabName = "comp"))),
                  dashboardBody(useShinyjs(),
                    tabItems(
                    tabItem(tabName = "comp",
                      fluidRow(
                      box(selectInput("dup_var", "Variable", multiple = TRUE, c("1"="1","2"="2")), 
                          width = 3, status = "primary")),
                      fluidRow(
                      box(title = "Duplicate Records", width = 12, solidHeader = TRUE, status = "primary", 
                      collapsible = TRUE, DT::dataTableOutput("dup_data")))))))

server <- function(input, output, session) {
  observe({
    cname <- c("Select All", names(mtcars))
    col_options <- list()
    col_options[ cname] <- cname

    updateSelectInput(session, "dup_var",
                      label = "",
                      choices = c("Choose Attributes"="",col_options))   
  })

  output$dup_data <- DT::renderDT({ 
    if (input$dup_var == "Select All"){
      col_names = colnames(mtcars)
      df = count(mtcars, col_names)
      df = df[df$freq > 1,]
      Dup <- df$freq
      df1 <- cbind.data.frame(Dup, df[,!names(df) %in% "freq"])
      df1 <- df1[order(-df1$Dup),]
      names(df1)[names(df1) == 'Dup'] <- 'Duplicate Count'

      dp <- DT::datatable(df1, rownames = FALSE)
      return(dp)
    } else {
      col_names = colnames(mtcars[,c(input$dup_var)])
      df = count(mtcars[,c(input$dup_var)], col_names)
      df = df[df$freq > 1,]
      Dup <- df$freq
      df1 <- cbind.data.frame(Dup, df[,!names(df) %in% "freq"])
      df1 <- df1[order(-df1$Dup),]
      names(df1)[names(df1) == 'Dup'] <- 'Duplicate Count'

      dp <- DT::datatable(df1, rownames = FALSE)
      return(dp)
    }
  }) 
          }
shinyApp(ui, server)

incorrect column name correct column name

Thanks in Advance.

like image 974
SJB Avatar asked Jun 27 '18 06:06

SJB


People also ask

Why are my DataTables parameter names different in shiny?

If you have used DataTables in Shiny before (specifically, before Shiny v0.10.2), you may need to change some parameter names for your DataTables, because Shiny (<= v0.10.1) was using DataTables v1.9, and DataTables v1.10 has changed the parameter names.

What are the performance issues with selectinput and selectizeinput?

Performance note: selectInput () and selectizeInput () can slow down significantly when thousands of choices are used; with legacy browsers like Internet Explorer, the user interface may hang for many seconds.

How to delete the selected option in a select input?

The selectize input created from selectizeInput() allows deletion of the selected option even in a single select input, which will return an empty string as its value. This is the default behavior of selectize.js.


1 Answers

It looks like you miss a few drop = FALSE. Adding this, you can handle the special case of one column the same way as the cases with multiple columns:

else {
  col_names = colnames(mtcars[, c(input$dup_var), drop = FALSE])
  df = count(mtcars[, c(input$dup_var), drop = FALSE], col_names)
  df = df[df$freq > 1, ]
  Dup <- df$freq
  df1 <- cbind.data.frame(Dup, df[, !(names(df) %in% "freq"), drop = FALSE])
  df1 <- df1[order(-df1$Dup), ]
  names(df1)[names(df1) == 'Dup'] <- 'Duplicate Count'

Note that I am not sure about your function count, but the above seems plausible to me.

like image 56
RolandASc Avatar answered Oct 03 '22 11:10

RolandASc