How to update the file related to a `fileInput` variable in R Shiny without user interaction?
Here is how I figured it out :
I edited the original code, so that all the read.csv(...)
are replaced with calls to a data.frame
global variable. I also added a small button that you need to click on before you continue. This button saves what you just loaded in the Database (if you chose a file with the fileInput
) and assigns the right values to the global variables that will be needed for the following operations. If you chose no file at all, it will directly assign the variables from the data found in the Database.
So I did not find a proper solution to the problem, but this is a workaround that will do the job in my case.
@brittenb I couldn't get your reactive solution to work as I wanted to, that's why I ended up doing this another way. Thanks for having taken the time to think about it though.
I'm still open to suggestions on how to update the file in a fileInput
without user interaction.
Update on File Upload Shiny R
Hi don't pass variable to the global environnement, use shiny reactive feature instead, your server should be :
library(shiny)
library(ggplot2)
# Test data
# write.csv(x = iris, file = "iris.csv", row.names = FALSE)
# write.csv(x = data.frame(V1 = 1:10, V2 = rnorm(10)), file = "test.csv", row.names = FALSE)
shinyServer(function(input, output) {
tableData <- reactive({
inFile <- input$file1
if (!is.null(inFile)){
read.csv(inFile$datapath)
} else {
mtcars
}
})
#for x values
output$opt.x <- renderUI({
selectInput("xcolumn", "X column to Plot", names(tableData()))
})
output$your_data <- renderTable({
head(tableData())
})
})
And replace your main panel with :
mainPanel(
tableOutput("your_data")
)
I remove the params in read.csv
since those input are not defined in the UI.
EDIT
Try this app, it's not a shiny solution but a jquery/js one (the difference is in the ui) (see this answer) :
library("shiny")
ui <- fluidPage(
titlePanel("App"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose CSV File',
accept = c('.csv','.tsv')
),
tags$script(
'$( "#file1" ).on( "click", function() {
this.value = null;
});'
),
uiOutput("opt.x")
),
mainPanel(
tableOutput("your_data")
)
)
)
server <- function(input, output) {
tableData <- reactive({
inFile <- input$file1
if (!is.null(inFile)){
read.csv(inFile$datapath)
} else {
mtcars
}
})
#for x values
output$opt.x <- renderUI({
selectInput("xcolumn", "X column to Plot", names(tableData()))
})
output$your_data <- renderTable({
head(tableData())
})
}
shinyApp(ui = ui, server = server)
For testing I used :
write.csv(x = data.frame(V1 = 1:10, V2 = rnorm(10)), file = "test.csv", row.names = FALSE)
write.csv(x = data.frame(V11 = 11:20, V12 = rnorm(10), ABC = letters[1:10]), file = "test.csv", row.names = FALSE)
Resetting fileInput in Shiny App
Ideally fileInput
would properly reset, but you can do this as a workaround. Add an explicit flag variable (rv$clear
) to indicate whether you're in cleared state, and toggle that on and off in high-priority observers when reset and upload occur, respectively.
library(shiny)
library(shinyjs)
library(xlsx)
library(tidyverse)
ui <- fluidPage(
useShinyjs(),
fileInput('inFile', 'Choose file'),
actionButton('reset', 'Reset'),
radioButtons("type","Choose file type",choices = c('csv','xls')),
tableOutput('tbl')
)
server <- function(input, output, session) {
rv <- reactiveValues(
data = NULL,
clear = FALSE
)
observe({
req(input$inFile)
req(!rv$clear)
if(input$type=='csv'){
rv$data <- read.csv(input$inFile$datapath)
}
if(input$type=='xls'){
rv$data <- read_excel(input$inFile$datapath)
}
})
observeEvent(input$inFile, {
rv$clear <- FALSE
}, priority = 1000)
observeEvent(input$reset, {
rv$data <- NULL
rv$clear <- TRUE
reset('inFile')
}, priority = 1000)
output$tbl <- renderTable({
rv$data
})
}
shinyApp(ui, server)
Adjust fileinput in Shiny
You can try this
data <- eventReactive(input$file, {
if (is.null(input$file)) return(NULL)
df <- read_excel(input$file$datapath)
df
})
How can I change the style of shiny fileInput object?
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
titlePanel(""),
sidebarLayout(
sidebarPanel(
fileInput("file", "Select a file")
),
mainPanel()
)
)
server <- function(input, output){
runjs("$('#file').parent().removeClass('btn-default').addClass('btn-danger');")
}
shinyApp(ui, server)
Related Topics
Convert Accented Characters into Ascii Character
What Are the Differences Between Concatenating Strings with Cat() and Paste()
Ternary Plot and Filled Contour
How to Jitter Both Geom_Line and Geom_Point by the Same Magnitude
How to Create a World Map in R with Specific Countries Filled In
Count Every Possible Pair of Values in a Column Grouped by Multiple Columns
R X-Axis Date Labels Using Plot()
How to Convert Month-Year String to Date in R
Dplyr::N() Returns "Error: Error: N() Should Only Be Called in a Data Context "
Annotate Values Above Bars (Ggplot Faceted)
Sum of Two Columns of Data Frame with Na Values
Represent Numeric Value with Typical Dollar Amount Format
Shiny Dashboard - Display a Dedicated "Loading.." Page Until Initial Loading of the Data Is Done
Ggplot2: Fix Colors to Factor Levels