ObserveEvent in shiny app does not work with fileInput

203 Views Asked by At

my shiny example does not work with the observeEvent and I don't know how to fix it !

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose CSV File"),
      checkboxInput("header", "Header", TRUE)
    ),
    mainPanel(
      tableOutput("contents")
    )
  )
)

server <- function(input, output) {
  
  myfile <- reactive({
    
    if(is.null(input$file1)){return(NULL)}
    f = read.csv(input$file1$datapath, header = input$header)
    print(nrow(f))
    return(f)
  })
  observeEvent(input$file1,{

    if (nrow(myfile()) > 100){
    shinyalert("error","file too long",type = "error")
      }
  },ignoreNULL = FALSE)
  
  output$contents <- renderTable({
 myfile()
    
  })
}

shinyApp(ui, server)

I know that can be replaced by something like validate instead of shinyalrert and observeEvent, but I'm looking for a solution with observeEvent!

0

There are 0 best solutions below