【问题标题】:updateinputs function package更新输入函数包
【发布时间】:2018-12-11 17:43:46
【问题描述】:

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

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

【问题讨论】:

  • 那么你在哪里定义函数UpdateInputs
  • 我能问一下为什么您使用 &lt;&lt;- 而不是使用响应式全局保存数据吗?
  • 这样,如果我想将新数据添加到现有文件中,我就可以在使用您帮助我的新按钮在一个地方使用之前提交的所有内容。有了它,我可以将我输入的每个新数据绑定到数据数据框中。
  • 2 提示: 1. 如果你想使用 updateInput() - 这不存在,但比如说 updateTextInput() - 需要 session 作为参数,那么你需要有 server 会话)。 2. 为了保持数据原样而不是将其设为 NULL,您可以执行observeEvent(input$new, { output$table &lt;- renderTable(NULL) }),尽管您的下载将包含所有数据。

标签: r shiny shiny-server rstudio-server


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-22
    • 1970-01-01
    • 2018-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2018-08-09
    相关资源
    最近更新 更多