如何将更新后的表或dataframe保存到已有的csv文件中?

时间:2021-05-24 16:52:07

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.

我是新手。我的代码运行得很好。我可以在我的表中上传文本,但是我没有得到一个合适的方法来将更新的表保存到我的现有*中。csv文件。

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) 

2 个解决方案

#1


2  

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.

你得加上一个字。csv函数,它将替换您的“旧”csv文件。注意它会替换它,所以在测试之前保存一个副本。

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

在您的实际配置中,它将在保存闪亮应用程序的路径中读写文件(适用于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) 

#2


1  

Inserted save button to your app, so you can easily save your data after entering fields

将save按钮插入到应用程序中,这样您可以在输入字段后轻松保存数据

error.no.7 <- read.csv(choose.files("file.csv"),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!"),
                    downloadButton('downloadData', 'Save')

                       ),
 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)))
  }
 })


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

 output$downloadData <- downloadHandler(
    filename = function() { paste(values$df, '.csv',sep="") },
    content = function(file) {
      write.csv(values$df, file, row.names=F)
    }
  )
  }

 shinyApp(ui = ui, server = server)  

#1


2  

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.

你得加上一个字。csv函数,它将替换您的“旧”csv文件。注意它会替换它,所以在测试之前保存一个副本。

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

在您的实际配置中,它将在保存闪亮应用程序的路径中读写文件(适用于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) 

#2


1  

Inserted save button to your app, so you can easily save your data after entering fields

将save按钮插入到应用程序中,这样您可以在输入字段后轻松保存数据

error.no.7 <- read.csv(choose.files("file.csv"),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!"),
                    downloadButton('downloadData', 'Save')

                       ),
 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)))
  }
 })


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

 output$downloadData <- downloadHandler(
    filename = function() { paste(values$df, '.csv',sep="") },
    content = function(file) {
      write.csv(values$df, file, row.names=F)
    }
  )
  }

 shinyApp(ui = ui, server = server)