我希望一个表的列是另一个表的两列的和积,如以下脚本所示.当我在RStudio中运行代码时,弹出以下错误消息:"Error in As:no method or defaulate for forcing‘teger’to‘data.Frame’".

有没有人能解释和修改一下, playbook 有什么地方错了?

    library(shiny)
    library(shinydashboard)
    library(rhandsontable)
    library(data.table)
    library(dplyr)

    DF1 = data.table(
        "Batch"= as.character(),
        "Amount of goods"= as.numeric(0),
        "Unit cost of goods"= as.numeric(0),
        stringsAsFactors=FALSE)

    DF2= data.table(
        "Cost of goods available for sale" = as.numeric(0),
        "Cost of goods sold" = as.numeric(0),
        stringsAsFactors=FALSE)

    ui <- dashboardPage(
      dashboardHeader(title = "Accounting"),
      dashboardSidebar(
        menuItem("Home", tabName = "home"),
        menuItem("Measurement", tabName = "measurement", 
              menuSubItem("Inventory", tabName = "table1")
        )
      ),
      dashboardBody(
        tabItems(
          tabItem(
            tabName = "table1",
            column(
              "DF1",
          width = 12,
              rHandsontableOutput("Table1")
            ),
            column(
              "DF2",
          width = 12,
              rHandsontableOutput("Table2")
            )
          )
        )
      )
    )

    server <- function(input, output, session) {

      data <- reactiveValues()

      observe({
        input$recalc
        data$df1<- as.data.frame(DF1)
        data$df2 <- as.data.frame(DF2)
      })

      observe({
        if(!is.null(input$Table1))
          data$df1<- hot_to_r(input$Table1)
      })

      observe({
        if(!is.null(input$Table2))
          data$df2 <- hot_to_r(input$Table2)
      })

    sumProduct1<-reactive({data$df1%>%
               summarize(sum(`Amount of goods` * `Unit cost of goods`))}) 

    observe({data$df2$`Cost of goods available for sale` <- sumProduct1()})

    output$Table1 <- renderRHandsontable({
        rhandsontable(data$df1, stretchH = "all") |> 
          hot_cols(colWidths = c(300, 150, 150))
    })

    output$Table2 <- renderRHandsontable({
       rhandsontable(data$df2, stretchH = "all") |> 
          hot_cols(colWidths = c(150, 150))
    })
   }
   
   shinyApp(ui, server)

推荐答案

sumProduct1() reactive表达式返回一个汇总数据帧,其中一列包含sumproduct.在你的observe函数中,你试图将这个数据框直接分配给一列数据$df2:

更改:

 observe({data$df2$`Cost of goods available for sale` <- sumProduct1()})

有:

  observe({
    if(!is.null(sumProduct1())){
      data$df2$`Cost of goods available for sale` <- sumProduct1()[[1]]
    }
  })

R相关问答推荐

创建重复删除的唯一数据集组合列表

混淆矩阵,其中每列和等于1

无法运行通过R中的Auto.arima获得的ARIMA模型

具有多个依赖变量/LHS的逻辑模型

使用ggplot 2根据R中的类别排列Likert比例gplot

x[[1]]中的错误:脚注越界

如何 bootstrap glm回归、估计95%置信区间并绘制它?

如何在格子中添加双曲曲线

如何在R中合并两个基准点?

打印XTS对象

R中边际效应包中Logistic回归的交互作用风险比

从圆到R中的多边形的标绘雷达图

如何移除GGPlot中超出与面相交的任何格网像元

使用ggplot2中的sec_axis()调整次轴

如何使用ggplot2根据绘图中生成的斜率对小平面进行排序?

每行不同列上的行求和

如何在R中使用因子行求和?

如何创建一个由一个连续变量和一个因素变量组成的复杂方框图?

如何为包创建自定义roxygen2标签?

修复标签重叠和ggploy内的空间