0

I'm trying to figure out the number of unique vector in selected input using my data.
However, all of the results of the variables' length(unique(x)) is 1.
Could you help me how to fix this problem?

Here's my code.

library(shiny)

ui <- fluidPage(
  fluidRow(
    box(width = 6, height=200, title ="File select", 
        fileInput("file","select file", buttonLabel = "file", accept = c(".csv")),
        DTOutput('dt')),
  selectInput("sel","select",colnames(file)),
  verbatimTextOutput("results")
)
)

server <- function(input, output, session) {
  
  data <- reactive({
    data.table::fread(input$file$datapath)
  })
  
  observeEvent(input$file, {
    mytable <- read.csv(input$file$datapath) %>% as_tibble()
    req(mytable)
    updateSelectInput(session, "sel", label = "select", choices = colnames(mytable))
  })
  
  output$results <- renderPrint({
    length(unique(data()[,input$sel]))
  })
  
}

shinyApp(ui, server)

################### solution ######################

server <- function(input, output, session) {
  
  appdata <- reactive({
    read.csv(input$file$datapath)
  })
  
  observeEvent(input$file, {
    mydf <- read.csv(input$file$datapath)
    updateSelectInput(session, "sel", label = "select", choices = colnames(mydf))
  })
  
  output$results <- renderPrint({
    cat(input$sel, '\n')
    length(unique(appdata()[[input$sel]]))
  })
  
}

It does work for me!!

YH Jang
  • 1,306
  • 5
  • 15

1 Answers1

0

The data object returned by fread is a data.table:

library(data.table)
data <- function(){ as.data.table(mtcars)}
length(unique(data()[,1]))
#> [1] 1

nrow(unique(data()[,1]))
#> [1] 25

Created on 2020-08-30 by the reprex package (v0.3.0)

The reason for this is that with a data.frame / data.table, data[,1] returns another data.frame / data.table (a list of list with 1 column).
length gives the number of columns, nrow is what you're looking for.

Waldi
  • 39,242
  • 6
  • 30
  • 78
  • Thanks for your answer. ```nrow``` definitely worked in already existed data like ```mtcars``` but not for input ```.csv``` data. – YH Jang Aug 30 '20 at 07:35
  • does nrow also return 1? Could you provide a sample of the data getting using [dput](https://stackoverflow.com/questions/49994249/example-of-using-dput)? – Waldi Aug 30 '20 at 07:39
  • Sure. structure(list(q1 = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2), q3 = c(2, 2, 2, 1, 1, 1, 1, 2, 2, 1, 2, 2, 2, 2, 1, 2, 1, 2, 2, 1), q5 = c(1, 1, 1, 2, 2, 4, 4, 3, 3, 2, 2, 5, 2, 3, 2, 2, 4, 2, 2, 2), hospital = c("yes", "yes", "yes", "no", "yes", "no", "no", "no", "no", "no", "no", "no", "yes", "no", "no", "no", "no", "yes", "yes", "no")), row.names = c(NA, -20L), class = c("tbl_df", "tbl", "data.frame")). It keeps showing me ```Error: incorrect number of dimensions```. – YH Jang Aug 30 '20 at 07:49
  • the first column of the data you provided contains 2 unique values : 1, 2, so nrow returns 2. From the error you mention, I think there's a mismatch with the data() function from R base dataset. What happens if you type data() in the console? Could you try renaming data() in appdata()? – Waldi Aug 30 '20 at 08:06
  • Oh it worked when renaming from ```data()``` to ```appdata()```! I appreicate it. But only works ```nrow(unique(appdata()[,1]))```, not ```nrow(unique(appdata()[, input$sel]))```. My purpose is to check the number of ```input$sel```, not just designated columns. ```nrow(unique(appdata()[, "input$sel"]))``` also doesn't work for me. Is there any way I can do to check the reactive ```nrow(unique())```? – YH Jang Aug 30 '20 at 08:19
  • you could try to debug by inserting `cat(input$sel,'\n')` one line before the unique to see in console what is the value the UI is setting in input. – Waldi Aug 30 '20 at 09:00