我有多个表要更新我可以在一个表中上传文本,但我想在shiny app
中一次性上传所有表中的不同文本。我搜索了很多,但由于我是shiny app
的新手,我没有办法做到这一点。以下是我的代码。请帮忙。提前谢谢。
error7 <- read.csv(file.path("D:/puma/error7.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"), width = 12)
)))
server = function(input, output) {
values <- reactiveValues()
values$df <- error7
row.names(error7) <- NULL
observe({
if(input$addButton > 0) {
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("D:/puma/error7.csv"), sep = "," ,
row.names = FALSE,append=FALSE)
}
})
output$table <- renderTable({values$df}, include.rownames=F)
}
shinyApp(ui = ui, server = server)
答案 0 :(得分:0)
您的代码存在多处问题。我正在逐步完成它:
"./error7.csv"
(并将文件复制到项目文件夹)。 如果你想要,你可以保持原样。stringsAsFactors = F
以避免处理factor
。代码:
error7 <- read.csv(file.path("./error7.csv"), sep = "," , header =TRUE, stringsAsFactors = F)
row.names
并不重要,因为您没有使用它。我们可以删除此行:row.names(error7) <- NULL
observeEvent
代替observe
:isolate
rbind
部分:使用names(newLine) <- names(values$df)
data.frame
和newLine
vector
可以
捆绑在一起。write.csv
始终使用逗号作为分隔符,因此无需指定它。也未使用append=FALSE
。代码:
observeEvent(input$addButton,{
newLine <- c(input$Possible.cause, input$Check, input$Remedy)
names(newLine) <- names(values$df)
values$df <- rbind(values$df, newLine)
write.csv(values$df,file.path("./error7.csv"), row.names = FALSE)
})
完整代码:
error7 <- read.csv(file.path("./error7.csv"), sep = "," , header =TRUE, stringsAsFactors = F)
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"), width = 12)
)))
server = function(input, output) {
values <- reactiveValues()
values$df <- error7
observeEvent(input$addButton,{
newLine <- c(input$Possible.cause, input$Check, input$Remedy)
names(newLine) <- names(values$df)
values$df <- rbind(values$df, newLine)
write.csv(values$df,file.path("./error7.csv"), row.names = FALSE)
})
output$table <- renderTable({values$df}, include.rownames=F)
}
shinyApp(ui = ui, server = server)