【问题标题】:Add extra cells with each entry to table output将每个条目的额外单元格添加到表输出
【发布时间】:2021-06-28 00:13:33
【问题描述】:

我正在尝试为某些商品的客户订单制作 xlsx 表。我创建了一个闪亮的应用程序,以数据表行的形式输入每个订单,单击“添加”按钮将其添加到表中。但是我遇到了一个问题,每次我添加一个新订单(行)时,都会在之前的所有订单中生成一个额外的行号单元格,如图所示。 并且所有的行单元格都向右移动了!!

这是我的代码:

library(shiny)
library(shinythemes)
library(readxl)
library(xlsx)
library(DT)

items <- read_excel("items.xlsx",col_names = F)
colnames(items) <- c("Items", "Euro", "Cost")


ui <- fluidPage(theme = shinytheme('journal'),
                sidebarLayout(
                  sidebarPanel(
                    tags$img(height = 118, width = 160, src="logo.jpg"),
                    br(),br(),
                       textInput('name','Name'),
                    br(),
                    selectizeInput(inputId = "item",
                                   label = "Item",
                                   choices = c(items$Items),
                                   options = list(
                                     placeholder = '',
                                     onInitialize = I('function() { this.setValue(""); }'))),
                    br(),
                    numericInput('number','Quantity',value = 1,step = 1),
                    br(),
                    numericInput('price','Price/pc',value = ''),
                    br(),
                    actionButton(inputId = "button", label = "Add", icon = icon("plus"),
                                 style="color: #fff; background-color: #337ab7; border-color: #2e6da4"),
                    
                    width = 3),
              mainPanel(DTOutput('out')))
                  )
                
                    


server <- function(input, output,session) {
   observeEvent(input$button,{
    order <- data.frame(read_excel("Order.xlsx"))
    price <- items[items$Items == input$item,][c(2,3)]
    order[nrow(order) + 1,] <- c(ifelse(input$name %in% order$Name,'',input$name),input$item,input$number,
                                 price,price[2]* input$number,input$price*input$number,
                                 (input$price*input$number) - (price[2]* input$number))
    
    write.xlsx(order,'order.xlsx')
    
  
   
    output$out<- renderDT({datatable(order)})
   })
    }

shinyApp(ui = ui, server = server)

items.xlsx

Items             Euro        Cost
some item          2.5         10
some item2         5           20
some item3         4           18

order.xlsx

Name   Item   Quantity   Euro   Cost   Total cost    Total price    Gain

有人可以知道这是什么原因以及如何解决吗? 谢谢大家

【问题讨论】:

    标签: r excel shiny


    【解决方案1】:

    我无法重新创建您的确切应用,因为我无法将 xlsx 包加载到我的系统上。我通过将两个 excel 文件保存为 CSV 文件并使用 readr read_csv 和 write_csv 代替 read_excel 和 write.xlsx 重新创建了一个类似的工作示例。此版本似乎可以按照您的意愿工作,但使用 csv 输出而不是 excel。阅读 xlsx 文档后,我可能会猜测每次您 write.xlsx 时都会写出行名,并且当您将它们读回时会出现这种情况。可能是您需要传递 row.names = FALSE到你的 write.xlsx 电话? https://cran.r-project.org/web/packages/xlsx/xlsx.pdf

    library(shiny)
    library(shinythemes)
    library(DT)
    library(readr)
    
    items <- readr::read_csv("items.csv",col_names = T)
    
    
    ui <- fluidPage(theme = shinytheme('journal'),
                sidebarLayout(
                  sidebarPanel(
                    tags$img(height = 118, width = 160, src="logo.jpg"),
                    br(),br(),
                    textInput('name','Name'),
                    br(),
                    selectizeInput(inputId = "item",
                                   label = "Item",
                                   choices = c(items$Items),
                                   options = list(
                                     placeholder = '',
                                     onInitialize = I('function() { this.setValue("");       }'))),
                    br(),
                    numericInput('number','Quantity',value = 1,step = 1),
                    br(),
                    numericInput('price','Price/pc',value = ''),
                    br(),
                    actionButton(inputId = "button", label = "Add", icon = icon("plus"),
                                 style="color: #fff; background-color: #337ab7; border-color: #2e6da4"),
                    
                    width = 3),
                  mainPanel(DTOutput('out')))
    )
    
    
    
    
    server <- function(input, output,session) {
      observeEvent(input$button,{
        order <- data.frame(read_csv("order.csv"))
        price <- items[items$Items == input$item,][c(2,3)]
        order[nrow(order) + 1,] <- c(ifelse(input$name %in% order$Name,'',input$name),input$item,input$number,
                                 price,price[2]* input$number,input$price*input$number,
                                 (input$price*input$number) - (price[2]* input$number))
    
        write_csv(order,'order.csv')
    
    
    
        output$out<- renderDT({datatable(order)})
      })
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 非常感谢@Graeme-diack,你是对的。现在可以了
    • 我很高兴它对你有用!您是否更改为使用 CSV 文件,或者您是否将 row.names = FALSE 传递给您的 write.xlsx?干杯,GD
    • 我通过了 row.names = FALSE
    【解决方案2】:

    尝试在观察者之外执行数据读取和数据整理,如下所示。

    items <- read.table(text='"Items","Euro","Cost"
    some item, 2.5, 10
    some item2, 5,20
    some item3, 4,18', header=TRUE, sep=",")
    
    
    ui <- fluidPage(theme = shinytheme('journal'),
                    sidebarLayout(
                      sidebarPanel(
                        tags$img(height = 118, width = 160, src="logo.jpg"),
                        br(),br(),
                        textInput('name','Name'),
                        br(),
                        selectizeInput(inputId = "item",
                                       label = "Item",
                                       choices = c(items$Items),
                                       options = list(
                                         placeholder = '',
                                         onInitialize = I('function() { this.setValue(""); }'))),
                        br(),
                        numericInput('number','Quantity',value = 1,step = 1),
                        br(),
                        numericInput('price','Price/pc',value = ''),
                        br(),
                        actionButton(inputId = "button", label = "Add", icon = icon("plus"),
                                     style="color: #fff; background-color: #337ab7; border-color: #2e6da4"),
    
                        width = 3),
                      mainPanel(DTOutput('out')))
    )
    
    
    server <- function(input, output,session) {
    
      order <- eventReactive(input$button,{
        req(input$number,input$price,input$name)
        order <- data.frame(read_excel("order.xlsx"))
        price <- items[items$Items == input$item,][c(2,3)]
        order[nrow(order) + 1,] <- c(ifelse(input$name %in% order$Name,'',input$name),input$item,input$number,
                                     price,price[2]* input$number,input$price*input$number,
                                     (input$price*input$number) - (price[2]* input$number))
        order
      })
    
      observeEvent(input$button,{  write.xlsx(order(),'order.xlsx') })
    
      output$out<- renderDT({datatable(order())})
       
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 我收到错误:错误:'closure' 类型的对象不是子集,因为 order() 不能被子集
    • 我也没用,存在同样的问题
    • 使用openxlsx 包对我来说效果很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-25
    • 2013-05-27
    • 2021-01-29
    • 1970-01-01
    • 2021-06-07
    • 2013-10-21
    • 1970-01-01
    相关资源
    最近更新 更多