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!!