djMohit
djMohit

Reputation: 171

External Data storage in Shiny apps

I am developing a shiny application which save the data entered on the user interface. I have refered the url on shiny rstudio page so by using this page, the code i have written is as mentioned below:

   outputDir <- "C:\\Users/dell/Desktop/"  
saveData <- function(data) {
  data <- t(data)

  fileName <- sprintf("%s_%s.csv", as.integer(Sys.time()), digest::digest(data))

  write.csv(
    x = data, sep = ",",
    file = file.path(outputDir, fileName), 
    row.names = FALSE, quote = TRUE
  )
}

loadData <- function() {

  files <- list.files(outputDir, full.names = TRUE)
  data <- lapply(files, read.csv, stringsAsFactors = FALSE) 

  data <- do.call(rbind, data)
  data
}
library(shiny)

fields <- c("name", "staff_name")

shinyApp(
  ui = fluidPage(
    titlePanel("attendance System"),
    DT::dataTableOutput("responses", width = 300), tags$hr(),
    textInput("name", "Accession Number", ""),
    selectInput("staff_name", "Staff Name",
                c("Rajiv" = "RT",
                  "Arvind " = "AKS",
                  "Ashutosh " = "AS")),

    actionButton("submit", "Submit")
  ),
  server = function(input, output, session) {

    formData <- reactive({
      data <- sapply(fields, function(x) input[[x]])
      data
    })


    observeEvent(input$submit, {
      saveData(formData())
    })



    output$responses <- DT::renderDataTable({
      input$submit
      loadData()
    })     
  }
)

The above code create a new file for each entry. I am looking for a single file in which all entry to be added.

Upvotes: 0

Views: 393

Answers (2)

djMohit
djMohit

Reputation: 171

after looking various solutions. I reached at below code to save the data in a single file as it is entered.

library(shiny)
outputDir <- "C:\\Users/dell/Desktop/"
saveData <- function(data) {
  data <- as.data.frame(t(data))
  if (exists("responsesiq")) {
    responsesiq <<- rbind(responsesiq, data)
  } else {
    responsesiq <<- data
  }
  fileName <- "test_igntu.csv"
  write.csv(
    x = responsesiq, sep = ",",
    file = file.path(outputDir, fileName), 
    row.names = FALSE, quote = TRUE
  )

}

fields <- c("acc", "staff_name")

shinyApp(
  ui = fluidPage(
    titlePanel("Attendance System"),
    DT::dataTableOutput("responsesiq", width = 300), tags$hr(),
    numericInput("acc", "AccNumber", ""),
    selectInput("staff_name", "Staff Name",
                c("Rajiv" = "RT",
                  "Arvind" = "AKS",
                  "Ashutosh" = "AS")),
    actionButton("submit", "Submit")
  ),
  server = function(input, output, session) {

    # Whenever a field is filled, aggregate all form data
    formData <- reactive({
      data <- sapply(fields, function(x) input[[x]])
      data
    })

    # When the Submit button is clicked, save the form data
    observeEvent(input$submit, {
      saveData(formData())
    })


  }
)

Upvotes: 0

SmokeyShakers
SmokeyShakers

Reputation: 3412

This will give you a unique file name based on time of save and content of the file:

fileName <- sprintf("%s_%s.csv", as.integer(Sys.time()), digest::digest(data))

You can give it a single name like:

fileName <- 'input_bu.csv'

Like @ismirsehregal, I'd recommend bookmarking for this though.

Upvotes: 1

Related Questions