Amin Shn
Amin Shn

Reputation: 598

Reseting Shiny app via fileInput (when the user browses new data)

I am developing an app which requires data input from the user. I need the app to reset everything once the user clicks on the "Browse" button to upload new data (I think it's a bad practice to ask the user to reset everything via a separate button and then browse new data!). I hoped this code to work but it didn't! The "Browse" button removes nothing!

library(shiny)

x = 0.1
y = 0.1
df = data.frame(x,y) #Just define a simple dataset


ui <- shinyUI(fluidPage(
  

  fileInput('datafile', 'Select your data CSV file',
            accept=c('csv', 'comma-separated-values','.csv')),
  
  tableOutput('table'),
  
))

server <- shinyServer(function(input, output, session) {
  
  
  
  output$table = renderTable({
    
    df
    
  })
  

  # 
  
  
  observeEvent(input$datafile, {
    
    output$table = NULL #This one didn't work
    df = NULL           #This one didn't work as well
    
    
  })
  
})

shiny::shinyApp(ui, server)

This is just a minimal example code. Can someone help me to remove the previously entered variables via "Browse" button so the app will be fresh for the new data to come in?

Upvotes: 1

Views: 441

Answers (2)

Amin Shn
Amin Shn

Reputation: 598

The problem is solved using reactiveVal(). This is what I really wanted to see:

library(shiny)

ui <- shinyUI(fluidPage(
  
  fileInput('datafile', 'Select your data CSV file',
            accept=c('csv', 'comma-separated-values','.csv')),
  textInput("txt", "Enter the text to display below:"),
  verbatimTextOutput("default"),
  tableOutput('table'), #This is just to check whether the variable will be deleted
  
))

server <- shinyServer(function(input, output, session) {
  
  X <- reactiveVal(c(1,3))
  Y <- reactiveVal(NULL)
  
  observe({
   
    Y(input$txt)
    
     
  })
  
  output$default <- renderText({Y() })
  output$table = renderTable({ #This is just to check whether the variable will be deleted
                           X()
    })
  
  
  
  observeEvent(input$datafile,{
    Y(NULL)
    X(NULL)
  })
  
})

shiny::shinyApp(ui, server)

"Browse" button is used as an action button to reset everything defined before clicking the button.

Upvotes: 0

Waldi
Waldi

Reputation: 41220

df should be reactive so that it gets modified according to UI input.
You could use a reactiveVal updated after browsing a new file:

library(shiny)

x = 0.1
y = 0.1


ui <- shinyUI(fluidPage(
  
  
  fileInput('datafile', 'Select your data CSV file',
            accept=c('csv', 'comma-separated-values','.csv')),
  
  tableOutput('table'),
  
))

server <- shinyServer(function(input, output, session) {
  
  df <- reactiveVal(data.frame(x,y))
  
  observe({
    file <- input$datafile
    ext <- tools::file_ext(file$datapath)
    
    req(file)
    validate(need(ext == "csv", "Please upload a csv file"))
    
    df(read.csv(file$datapath, header = T))
  })
  
  output$table = renderTable({
    
    df()
    
  })
  
})

Upvotes: 2

Related Questions