【问题标题】:Another Follow up to “add values to a reactive table in Shiny” when we already have a dataframe当我们已经有一个数据框时,另一个跟进“向 Shiny 中的反应表添加值”
【发布时间】:2016-04-28 02:48:42
【问题描述】:

我想在开始时存在数据框时扩展此应用程序。老实说,我的问题比这更大,您可以在以下链接中找到问题:How to add a new row to uploaded datatable in shiny

通过这个问题,我将与未成年人一起追逐大局。 我有一个当前的数据框,2 列和 3 行。第一列表示当前日期,另一列需要计算。新行应显示为(当前日期 - 像 Excel 中一样,例如 11.02.2015-, [Input$1 + "perivious value of column2's row"])

但是,我在显示系统日期时遇到了问题。另外,我无法生成在 newLine 中发出警告的新行!

第二个版本:可以上传数据。错误:read.table 中的错误(文件 = 文件,标题 = 标题,sep = sep,quote = quote,: 'file' 必须是字符串或连接 警告:观察者中未处理的错误:“闭包”类型的对象不是子集 观察事件(输入$更新)

library(shiny)
library(gtools)
 runApp(
   list(
    ui = fluidPage(

  pageWithSidebar(
    headerPanel("Adding entries to table"),
    sidebarPanel(
      wellPanel(fileInput('file1', 'Choose Planning File:', accept=c('text/csv', 'text/comma-separated-values,text/plain', '.csv'), multiple = FALSE),


                selectInput(inputId = "location",label = "Choose Location",
                            choices = c('All','Lobau'='LOB', 'Graz'='GRA', 'St. Valentin'='VAL'), selected = "GRA"),
                selectInput(inputId = "product",label = "Choose Product",
                            choices = c('All','Gasoline'='OK', 'Diesel'='DK'), selected = "DK")),

      numericInput("spotQuantity", "Enter the Spot Quantity",value=0),

      actionButton("action","Confirm Spot Sales"),


      numericInput("num2", "Column 2", value = 0),

      actionButton("update", "Update Table")),
    mainPanel(tableOutput("table1")))
),
server = function(input, output, session) {

  values <- reactive({ #
    #file.choose()
    dm <-  as.data.frame(read.csv(input$file1$datapath, sep=";",check.names = FALSE))


  })

    addData <- observeEvent(input$update, {
    values$dm <- isolate({
      newLine <- data.frame('Month'=1,'Day ID'=2,'Day'="28-11-2012",'starting inventory'=2,'planned (in kTO)'=2,'lifted (in kTO)'="2",'replenishment (in kTO)'="2", 'Product'="OK",'Location'="GRA", check.names=F)
      rbind.data.frame(values$dm,newLine)
    })
  })

  output$table1 <- renderTable({
    values()
      })
}

) )

【问题讨论】:

  • 试试rhandsontable 包。

标签: r shiny


【解决方案1】:

您的代码存在多个问题。你开始 reactiveValues 但你从来没有给它分配任何东西,所以没有数据希望是反应性的。此外,您可能希望使用observeEvent,以便每次点击Update 按钮时都会收到响应。您还可以isolate 代码块。此外,您应该为新数据使用data.frame 作为数据的“类型”(即numericcharacter 等)。以下对我很有效。

library(shiny)

runApp(
  list(
    ui = fluidPage(

      pageWithSidebar(
        headerPanel("Adding entries to table"),
        sidebarPanel(
          numericInput("num2", "Column 2", value = 0),

          actionButton("update", "Update Table")),
        mainPanel(tableOutput("table1")))
    ),
    server = function(input, output, session) {

      values <- reactiveValues(
        dm = data.frame(Date = as.Date(c("2015-05-10", "2015-10-07", "2015-03-26","2015-07-18")),
                        Col2 = c(160, 150, 121, 93))
      )

      addData <- observeEvent(input$update, {
        values$dm <- isolate({
          newLine <- data.frame(Date = format(Sys.time(), "%Y-%m-%d"), 
                                Col2 = tail(values$dm$Col2, n=1) - 4)
          rbind(values$dm,newLine)
        })
      })


      output$table1 <- renderTable({
          values$dm
      })
    }
    )
)

【讨论】:

  • 非常感谢!但是,当数据由用户上传时,我想通知您我的另一个问题。它发出警告:警告:观察者中未处理的错误:“闭包”类型的对象不可子集。我尝试调用 values stackoverflow.com/questions/34403004/…
  • 我将应用程序的第二个版本放在您可以上传 csv 文件以收集数据的位置。但我不知道我该如何克服这个错误!
猜你喜欢
  • 1970-01-01
  • 2020-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-29
  • 2015-12-31
  • 1970-01-01
相关资源
最近更新 更多