如何通过闪亮的应用程序在已存在的csv文件中保存更新的表或数据框?

时间:2016-09-21 09:00:19

标签: r csv shiny

我是Shiny的新手。我的代码运行正常。 我可以在我的表中上传文本,但我没有得到一个正确的方法来保存我现有的* .csv文件中的更新表。

请建议。提前谢谢!

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 个答案:

答案 0 :(得分:2)

你必须添加一个write.csv函数,它取代你的“旧”csv文件。请注意,它会替换它,因此请在测试前保存副本。

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

答案 1 :(得分:1)

在您的应用中插入了保存按钮,因此您可以在输入字段后轻松保存数据

["one", "two", "three"]