gpt4 book ai didi

r - 如何通过 Shiny 的应用程序将更新的表格或数据框保存在现有的 csv 文件中?

转载 作者:行者123 更新时间:2023-12-04 11:31:17 25 4
gpt4 key购买 nike

我是 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)

最佳答案

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

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

关于r - 如何通过 Shiny 的应用程序将更新的表格或数据框保存在现有的 csv 文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39611962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com