Here are two ways of resetting the file input UI (both the progress bar and the displayed name). The first uses JavaScript and the second uses renderUI.
The examples include both a "Clear" button as well as an optional drop-down menu that resets the file input UI when the selection changes.
Example 1 - using JavaScript
ui.R
shinyUI(bootstrapPage(
tags$head(
tags$style(".clearButton {float:right; font-size:12px;}")
),
headerPanel("Reset file input example"),
sidebarPanel(
HTML("<button id='clearFile1' class='action-button clearButton'>Clear</button>"),
fileInput('file1', NULL, width="80%"),
selectInput('uploadFormat', label = "Select upload format",
choices = c(
"Option 1" = 'f1',
"Option 2" = 'f2',
"Option 3" = 'f3'),
selected = 'f1')
),
mainPanel(
h4("Summary"),
verbatimTextOutput("summary")
),
singleton(includeScript("active.js"))
))
server.R
shinyServer(function(input, output, session) {
values <- reactiveValues(
file1 = NULL
)
observe({
input$clearFile1
input$uploadFormat
values$file1 <- NULL
})
observe({
values$file1 <- input$file1
})
output$summary <- renderText({
return(paste("Uploaded file: ", values$file1$name))
})
})
active.js
$(document).ready(function() {
/* clear file button control */
var fileControl = $("#file1");
$("#clearFile1").on("click", function () {
fileControl.replaceWith( fileControl = fileControl.clone( true ) );
$("#file1_progress").hide();
});
$("#uploadFormat").on("change", function () {
fileControl.replaceWith( fileControl = fileControl.clone( true ) );
$("#file1_progress").hide();
});
/* file input progress bar control */
$( "#file1" ).change(function() {
document.getElementById("file1_progress").setAttribute('style', "height:20px; margin-top:5px;");
});
});
Example 2 - using renderUI
Same as above, but (1) get rid of active.js and the related include statement, (2) in ui.R replace
fileInput('file1', NULL, width="80%"),
with
uiOutput('resettableInput'),
and (3) in server.R add:
output$resettableInput <- renderUI({
input$clearFile1
input$uploadFormat
fileInput('file1', NULL, width="80%")
})
Note that one could enclose multiple UI elements in a list() statement here and they would all be re-rendered. See here.
You can find the code for these examples here and here, and run them like so:
library(shiny)
shiny::runGist('0c2a98a0236f1257fd45')
shiny::runGist('bc09d77fa92457e094c8')