0

I have created the following Shiny App. It takes two values, it adds them to the table and when the button "Save" is hit it saves the table locally in a csv file. What I would like to do next (and I cannot wrap my mind around) is, every time I open the app to check if the file values already exists and, if TRUE to open that file and start adding from there:

#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

library(shiny)

# Define UI for application that draws a histogram
ui=pageWithSidebar(headerPanel("Adding entries to table"),
                   sidebarPanel(textInput("text1", "Column 1"),
                                textInput("text2", "Column 2"),
                                actionButton("update", "Update Table"),
                                actionButton("save", "Save")
                                )                   ,
                   mainPanel(tableOutput("table1")))

# Define server logic required to draw a histogram
server=function(input, output, session) {
    values <- reactiveValues()
    values$df <- data.frame(Column1 = NA, Column2 = NA)
    newEntry <- observe({
        if(input$update > 0) {
            newLine <- isolate(c(input$text1, input$text2))
            isolate(values$df <- rbind(values$df, newLine))
            }
        })
    Save <- observe({
        if(input$save > 0){
            write.csv(values$df, "responses/values.csv", row.names = FALSE)
        }
    })
    output$table1 <- renderTable({values$df})
}#Server End


# Run the application 
shinyApp(ui = ui, server = server)

1 Answer 1

1

You can use file.exists to check if the file is already created and append the new values to already existing data.

library(shiny)

ui=pageWithSidebar(headerPanel("Adding entries to table"),
                   sidebarPanel(textInput("text1", "Column 1"),
                                textInput("text2", "Column 2"),
                                actionButton("update", "Update Table"),
                                actionButton("save", "Save")
                   )                   ,
                   mainPanel(tableOutput("table1")))

# Define server logic required to draw a histogram
server=function(input, output, session) {
  values <- reactiveValues()
  if(file.exists('responses/values.csv')) values$df <- read.csv('responses/values.csv')
  else values$df <- data.frame(Column1 = NULL, Column2 = NULL)
  newEntry <- observe({
    if(input$update > 0) {
      newLine <- isolate(data.frame(Column1 = input$text1, Column2 = input$text2))
      isolate(values$df <- rbind(values$df, newLine))
    }
  })
  Save <- observe({
    if(input$save > 0){
      write.csv(values$df, "responses/values.csv", row.names = FALSE)
    }
  })
  output$table1 <- renderTable({values$df})
}#Server End


# Run the application 
shinyApp(ui = ui, server = server)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.