2

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)
user1820133
  • 41
  • 1
  • 4

1 Answers1

0

This reproducible example takes an input file and renders a table with it. If the columns are different than 12 then it shows a message (but the app is still running). Also, if append is selected, the new data is appended with the previous one.

library(shiny)
library(tidyverse)
library(DT)

#create dataset with 12 columns
rerun(3, iris) %>% 
    reduce(bind_cols) %>%
    select(1:12) %>% 
    write_delim(file = 'iris_12_cols.txt', delim = '|')

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 and Show",ignoreInit = TRUE),
        ),
        
        mainPanel(
            DTOutput("summary")
        )
    ))
server <- function(input, output) {
    options(shiny.maxRequestSize=3000*1024^2) 
    
    #this will remember the last data to append it if the checkbox is selected.
    data_uploaded <- reactiveValues()
    
    observeEvent(input$action, {
            req(input$file1)
        
                
            inFile <- input$file1
            #update reactiveValues with the new data
            data <-  read_delim(inFile$datapath, delim = "|")
            
            #Check that ncols is 12 otherwide print a message and perform no further actions
            if (ncol(data) != 12) {
                showModal(modalDialog(
                    title = "Total number of columns must be 12",
                    paste0("Total columns are ", ncol(data), ' but must be 12.\nPlease upload another file to continue.'),
                    easyClose = TRUE,
                    footer = NULL
                ))
            } else if (!input$append) {
                   
                   #overwrite last data
                   data_uploaded$MA <- data 
                   
                   #render a new table 
                   output$summary <- renderDT({ 
                       data_uploaded$MA
                   })
                  
            } else {
                   #append
                   data_uploaded$MA  <- rbind(data_uploaded$MA, data)
                   
                   #render the data appended
                   output$summary <- renderDT({ 
                       data_uploaded$MA
                   })
                
               }
            }) 
            
            
}

shinyApp(ui = ui, server = server)
jpdugo17
  • 6,816
  • 2
  • 11
  • 23
  • https://stackoverflow.com/questions/68048195/split-decimal-digits-that-are-less-than-4-from-a-character-string @jpdugo17 – user1820133 Jun 19 '21 at 15:27