I have a shiny R app and I want to append files only when user click on checkbox 'append' otherwise if the user select fileinput it should just select single file.So every time user selects fileinput it should keep overwriting the dataframe unless checkbox.input is true when the files should be appended.Right now I am appending files but that is within R not within shiny.Secondly, I want to display error message in shiny rather than in R console if number of columns of the table is lets say not equal to 12.
ui <- fluidPage(
titlePanel("Client Files Portal"),
sidebarLayout(
sidebarPanel(
selectInput(inputId = "dataset",
label = "Select File Type:",
choices = c("MA", "AGR","SS","OW","SA","UN","AD","AL","AV")),
fileInput("file1", "Choose a file:", accept = ".txt"),
checkboxInput("append", "Append", FALSE),
actionButton("action", "Validate",ignoreInit = TRUE),
),
mainPanel(
textOutput("summary")
)
))
server <- function(input, output) {
options(shiny.maxRequestSize=3000*1024^2)
observeEvent(input$file1, {
output$summary <- renderText({
paste("Validation Summary", input$dataset)
})
})
observeEvent(input$action, {
if (input$dataset=="MA"){
MA<- data.frame()
ma_i<- data.frame()
MA_in<- data.frame()
req(input$file1)
inFile <- input$file1
if (is.null(inFile))
return(NULL)
ma_i<- read.delim(inFile$datapath,sep="|", header = FALSE)
stopifnot("Column Mismatch:No. of columns for MA must be 12"=ncol(ma_i)==12)
MA<-ma_i
answer<-winDialog("yesno", "Append another MA File?")
while (answer=='YES') {
fname <- file.choose()
ma_i <- read.delim(fname,header=FALSE,sep="|" )
stopifnot("Column Mismatch:No. of columns for MA must be 12"=ncol(ma_i)==12)
MA<-rbind(MA,ma_i)
answer<-winDialog("yesno", "Append another MA File?")
}
MA_in<-MA
} })
}
shinyApp(ui = ui, server = server)