繁体   English   中英

当我们已经有一个数据框时,另一个跟进“将值添加到Shiny中的反应表中”

[英]Another Follow up to “add values to a reactive table in Shiny” when we already have a dataframe

当数据帧开始时,我想扩展此应用程序。 老实说,我的问题比这更大,您可以在以下链接中找到问题: 如何在闪亮的上传数据表中添加新行

通过这个问题,我将与未成年人一起追逐全局。 我有一个当前数据框,2列和3行。 第一列表示当前日期,另一列将被计算。 新行应显示为(当前日期-在Excel中类似,例如11.02.2015-,[Input $ 1 +“ colum2的行的上一个值”])

但是,我对显示系统日期有疑问。 另外,我无法在newLine中产生警告的新行!

第二版:可以上传数据。 出现错误:read.table中的错误不是subsettable的observeEvent(输入$更新)

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

))

您的代码有多个问题。 您启动了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
      })
    }
    )
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM