How to Store Data in Shiny Tables

Asked

Viewed 81 times

2

I created a very simple Shiny that was simply for a "stock control". It has a table that starts with its products (columns) at zero value. The goal was every time you click the action button 1 it increased the stock, for example it has a numeric box and was put the value 5 in it the program would take the previous value of the table, in the case of 0, and would add to 5. If the user repeated the click would be the 5 that already had more than the 5 of the numeric box. However, the value is not stored. When I click on the button it always sums 0 (initial value) plus the value of the text box, if you click again it does not store the previous value and makes again the sum 0 plus the value of the numeric box. I would like to know how to solve, or how to store a variable with the action button. Code:

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      h1("Entrada dos Chops"),
      numericInput("Chop1","Sabor 1",min = 0,max = 100, value = 0),
      numericInput("Chop2","Sabor 1",min = 0,max = 100, value = 0),
      numericInput("Chop3","Sabor 1",min = 0,max = 100, value = 0),
      numericInput("Chop4","Sabor 1",min = 0,max = 100, value = 0),
      numericInput("Chop5","Sabor 1",min = 0,max = 100, value = 0),
      actionButton("Botao1","Adcionar chop!",icon("ice-cream",lib="font-awesome")),
      h1(" "),
      h1("Venda dos Chops"),
      numericInput("Chop6","Sabor 1",min = 0,max = 100, value = 0),
      numericInput("Chop7","Sabor 1",min = 0,max = 100, value = 0),
      numericInput("Chop8","Sabor 1",min = 0,max = 100, value = 0),
      numericInput("Chop9","Sabor 1",min = 0,max = 100, value = 0),
      numericInput("Chop10","Sabor 1",min = 0,max = 100, value = 0),
      actionButton("Botao2","Vender chop!",icon("ice-cream",lib="font-awesome"))
    ),
    mainPanel(
      h1("Tabela estoque chops"),
      tableOutput("SaidaTabela")
    )
  )
)

server <- function(input, output) {
 isolate({ 
  Chopp1=0
  Chopp2=0
  Chopp3=0
  Chopp4=0
  Chopp5=0

  Tabela=data.frame(Chopp1,Chopp2,Chopp3,Chopp4,Chopp5)

  output$SaidaTabela=renderTable(Tabela)
 })

  observeEvent(input$Botao1,{
               Tabela[1,1]=Tabela[1,1]+input$Chop1
               Tabela[1,2]=Tabela[1,2]+input$Chop2
               Tabela[1,3]=Tabela[1,3]+input$Chop3
               Tabela[1,4]=Tabela[1,4]+input$Chop4
               Tabela[1,5]=Tabela[1,5]+input$Chop5
               output$SaidaTabela=renderTable(Tabela)


                              }

               )

  observeEvent(input$Botao2,{
               Tabela[1,1]=Tabela[1,1]-input$Chop6
               Tabela[1,2]=Tabela[1,2]-input$Chop7
               Tabela[1,3]=Tabela[1,3]-input$Chop8
               Tabela[1,4]=Tabela[1,4]-input$Chop9
               Tabela[1,5]=Tabela[1,5]-input$Chop10
               output$SaidaTabela=renderTable(Tabela)


               }

  )
}

shinyApp(ui, server)
  • 1

    The common in this type of case is to create a variable that is being changed. To create this variable see ?shiny::reactiveValues()

1 answer

0

The simplest and dirtiest way, however effective, is to put a command save() to save a . Rdata and read it in the preamble.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.