我想创建一个闪亮的应用程序,该应用程序从用户那里获取数据并将其保存为.csv格式。但是,每次用户输入数据时,过去的数据都不会被删除,因此,在附加所有先前的数据(不一定是同一用户)时,都会附加已输入的新数据。我想创建一个新的“新”按钮,该按钮删除过去的输入数据并提供一个新的输入表。
library(shiny)
ui <- fluidPage(
titlePanel("Creating a database"),
sidebarLayout(
sidebarPanel(
textInput("name", "Company Name"),
textInput("income", "Income"),
textInput("expenditure", "Expenditure"),
dateInput("date", h3("Date input"),value = Sys.Date() ,min = "0000-01-01",
max = Sys.Date(), format = "dd/mm/yy"),
actionButton("Action", "Submit"),#Submit Button
actionButton("new", "New")),
mainPanel(
tableOutput("table"), #Table showing what is there in the data frame
textInput("filename", "Enter Filename for download"), #filename
helpText(strong("Warning: Append if want to update existing data.")),
downloadButton('downloadData', 'Download'), #Button to save the file
downloadButton('Appenddata', 'Append') #Button to update a file
)
)
)
# Define server logic
server <- function(input, output){
#Global variable to save the data
Data <- data.frame()
Results <- reactive(data.frame(input$name, input$income, input$expenditure,
as.character(input$date),
as.character(Sys.Date())))
#To append the row and display in the table when the submit button is clicked
observeEvent(input$Action,{
#Append the row in the dataframe
Data <<- rbind(Data,Results())
#Display the output in the table
output$table <- renderTable(Data)
})
observeEvent(input$new, {
UpdateInputs(CreateDefaultRecord(), session)
})
output$downloadData <- downloadHandler(
# Create the download file name
filename = function() {
paste(input$filename , ".csv", sep="")
},
content = function(file) {
write.csv(Data, file,row.names = FALSE) # put Data() into the download file
})
output$Appenddata <- downloadHandler(
# Append data
filename = function() {
paste(input$filename, ".csv", sep="")
},
content = function(file) {
write.table( Data, file=file.choose(),append = T, sep=',',
row.names = FALSE, col.names = FALSE)
})
}`enter code here`
# Run the application
shinyApp(ui = ui, server = server)
代码运行正常,我可以在需要的时候输入新数据,但是当我按下“新”按钮以清除数据时,我想输入新的详细信息,应用程序将关闭并给出跟随错误
Warning: Error in UpdateInputs: could not find function "UpdateInputs"
在按下新按钮之前,警告不会关闭应用程序。我想念的是什么?请帮忙。谢谢。
答案 0 :(得分:2)
如果您只想清除数据,则可以替换
observeEvent(input$new, {
UpdateInputs(CreateDefaultRecord(), session)
})
使用
observeEvent(input$new, {
#Append the row in the dataframe
Data <<- NULL
#Display the output in the table
output$table <- renderTable(Data)
})
尽管我不确定为什么您要使用<<-
在全球范围内保存数据,并且此方法显然会清除它。