【发布时间】: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
有人可以知道这是什么原因以及如何解决吗? 谢谢大家
【问题讨论】: