Mateusz1981
Mateusz1981

Reputation: 1867

Update of the variable by constant in shiny

The community helped me in developing this code

library(shiny)
server <- shinyServer(function(input, output, session) {

  values <- reactiveValues()
  values$df <- data.frame(N = numeric(0), A = numeric(0), C = numeric(0))

  newEntry <- observeEvent(input$update,{
    gr <- 10 + input$n
    newLine <- isolate(c(input$n, input$nr1, gr))
    values$df[nrow(values$df) + 1,] <- c(input$n, input$nr1, gr)
  })
  output$table1 <- renderTable({values$df})
})

ui <- shinyUI(fluidPage(
  titlePanel("submitButton example"),
  fluidRow(
    column(3, wellPanel(
      sliderInput("nr1", "Give a number: ",  min = 0, max = 100, value = 0, step = 2),
      sliderInput("n", "N:", min = 10, max = 1000, value = 200, step = 10),
      actionButton("update", "Update Table"))),
    column(6, tableOutput("table1"))
  )
))
shinyApp(ui, server)

And I come to a new problem with that.

Now, I'd like that the app do like this: on click on the update, the variable A should always add a 5 i.e. if I have starting value of 5 than on the next click it should be 10 than 15, 20 etc? Now when I click update the same number appears continously

Upvotes: 0

Views: 535

Answers (1)

Pork Chop
Pork Chop

Reputation: 29417

Is this what you want? Also you dont need the isolate in there.

rm(list = ls())
library(shiny)

ui <- shinyUI(fluidPage(
  titlePanel("submitButton example"),
  fluidRow(
    column(3, wellPanel(
      sliderInput("nr1", "Give a number:",  min = 1, max = 100, value = 1, step = 1),
      sliderInput("n", "N:", min = 10, max = 1000, value = 200, step = 10),
      actionButton("update", "Update Table"))),
    column(6, tableOutput("table1"))
  )
))

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

  values <- reactiveValues()
  values$df <- data.frame(N = numeric(0), A = numeric(0), C = numeric(0))

  newEntry <- observeEvent(input$update,{

    if(!is.null(input$nr1) & is.null(values$A)){
      values$A <- input$nr1
    }
    if(nrow(values$df) > 0){
      values$A <- values$A + 5
    }  
    gr <- 10 + input$n
    values$df[nrow(values$df) + 1,] <- c(input$n, values$A, gr)
  })
  output$table1 <- renderTable({values$df})
})


shinyApp(ui, server)

Upvotes: 1

Related Questions