Purva Neema Purva Neema - 2 months ago 27
R Question

How to save updated table or dataframe in already existing csv file through shiny app?

I am new to Shiny. My code is working fine.
I am able to upload text in my table but I am not getting a proper way to save that updated table in my existing *.csv file.

Please suggest. Thanks in advance!

error.no.7 <- read.csv(file.path("file.csv"), sep = "," , header = TRUE)

library(shiny)
library(shinythemes)

ui <- shinyUI(
fluidPage(theme=shinytheme("readable"),
titlePanel(h3("PUMA", style = "color:black")),
sidebarLayout(
sidebarPanel(
tags$head(
tags$style("body {background-color: pink; }")
),
textInput("Possible.cause", label="Add a new Possible.cause ", value="Enter Possible.cause"),
textInput("Check", label="Add a new Check", value="Enter Check"),
textInput("Remedy", label="Add a new Remedy", value="Enter Remedy"),
actionButton("addButton", "UPLOAD!")
),
mainPanel(
tableOutput("table")
)
)
)
)

server = function(input, output) {
row.names(error.no.7) <- NULL

values <- reactiveValues()
values$df <- error.no.7

observe({
if(input$addButton > 0) {
# create the new line to be added from your inputs
newLine <- isolate(c(input$Possible.cause, input$Check, input$Remedy))
isolate(values$df <- rbind(as.matrix(values$df), unlist(newLine)))
}
})

output$table <- renderTable({values$df}, include.rownames=F)
}

shinyApp(ui = ui, server = server)

Answer

You've got to add a write.csv function, which replaces your "old" csv file. Be aware that it replaces it, so save a copy before testing.

In your actual configuration it will read and write the file in the path where the shiny app is saved (works on Windows7).

error.no.7 <- read.csv(file.path("file.csv"), sep = "," , header = TRUE)
#error.no.7 <- read.csv(choose.files("file.csv"), sep = "," , header = TRUE)

library(shiny)
library(shinythemes)

ui <- shinyUI( fluidPage(theme=shinytheme("readable"),
                         titlePanel(h3("PUMA", style = "color:black")),
                         sidebarLayout(
                           sidebarPanel(
                             tags$head(
                               tags$style("body {background-color: pink; }")),
                             textInput("Possible.cause", label="Add a new Possible.cause ", value="Enter Possible.cause"),
                             textInput("Check", label="Add a new Check", value="Enter Check"),
                             textInput("Remedy", label="Add a new Remedy", value="Enter Remedy"),
                             actionButton("addButton", "UPLOAD!")
                           ),
                           mainPanel(
                             tableOutput("table"))
                         )))

server = function(input, output) {    
  row.names(error.no.7) <- NULL

  values <- reactiveValues()
  values$df <- error.no.7
  observeEvent(input$addButton, {

    if(input$addButton > 0) {
      # create the new line to be added from your inputs
      newLine <- isolate(c(input$Possible.cause, input$Check, input$Remedy))
      isolate(values$df <- rbind(as.matrix(values$df), unlist(newLine)))
      write.csv(values$df,file.path("file.csv"), sep = "," ,row.names = FALSE,append=FALSE)
    }
  })

  output$table <- renderTable({values$df}, include.rownames=F)
}

shinyApp(ui = ui, server = server) 
Comments