通过闪亮的常量更新变量

时间:2016-04-01 06:58:03

标签: r shiny

社区帮助我开发了这段代码

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)

我遇到了一个新问题。

现在,我希望应用程序执行此操作:点击更新后,变量A应始终添加5,即如果我的起始值为5,则下次单击时应为10比15,20等? 现在,当我点击更新时,会不断出现相同的数字

1 个答案:

答案 0 :(得分:1)

这是你想要的吗?你也不需要那里的isolate

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)