首页 > 解决方案 > 更新输入函数包

问题描述

我想创建一个闪亮的应用程序,从用户那里获取数据并将其保存为 .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"

在按下新按钮之前,警告不会关闭应用程序。我错过了什么?请帮忙。谢谢你。

标签: rshinyshiny-serverrstudio-server

解决方案


如果您只想清除可以替换的数据

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

尽管我不确定您为什么要使用全局保存数据<<-,但这种方法显然会清除它。


推荐阅读