首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在与updateNumericInput交互的闪亮应用程序中添加计数按钮

在与updateNumericInput交互的闪亮应用程序中添加计数按钮
EN

Stack Overflow用户
提问于 2020-11-27 07:36:47
回答 2查看 250关注 0票数 2

我正在建立一个闪亮的应用程序,我需要一个计数按钮,以添加与数字输入的互动。所以我想要一个数字输入,用户可以自由使用,但也有一个用户可以点击的按钮,数字输入加1(所以如果用户选择20,然后点击按钮,输入就变成了21)。我有两个单独的工作,但不能让交互工作。数字输入会使用add按钮进行更新,但是如果我使用add numeric输入更改了值,那么到目前为止的点击次数就会继续。这就是我现在所拥有的:

代码语言:javascript
复制
library(shiny)

# https://shiny.rstudio.com/reference/shiny/1.3.2/updateNumericInput.html
# https://gist.github.com/aagarw30/69feeeb7e813788a753b71ef8c0877eb

ui <- shinyUI(
    fluidPage(
        tags$b("Simple counter using reactiveValues() - An example"),
        numericInput("inNumber", "Input number", 0),
        actionButton("add1", "+ 1"),
        plotOutput("plot")
    )
)

server <-  function(input, output, session) {
    counter <- reactiveValues(countervalue = 0) # Defining & initializing the reactiveValues object
    
    observeEvent(input$add1, {
        counter$countervalue <- counter$countervalue + 1     # if the add button is clicked, increment the value by 1 and update it
    })

    
    observeEvent(input$add1, {
            updateNumericInput(session, "inNumber", value = counter$countervalue )
    })
    
    output$plot <- renderPlot({
        hist( rnorm(input$add1))
    })
    }
    

shinyApp(ui, server)
EN

Stack Overflow用户

发布于 2020-11-27 08:02:01

您必须引用histogramm中的数字输入:

代码语言:javascript
复制
library(shiny)

# https://shiny.rstudio.com/reference/shiny/1.3.2/updateNumericInput.html
# https://gist.github.com/aagarw30/69feeeb7e813788a753b71ef8c0877eb

ui <- shinyUI(
  fluidPage(
    tags$b("Simple counter using reactiveValues() - An example"),
    numericInput("inNumber", "Input number", 0),
    actionButton("add1", "+ 1"),
    plotOutput("plot")
  )
)

server <-  function(input, output, session) {
  counter <- reactiveValues(countervalue = 0) # Defining & initializing the reactiveValues object
  
  observeEvent(input$add1, {
    counter$countervalue <- input$inNumber + 1     # if the add button is clicked, increment the value by 1 and update it
    updateNumericInput(session, "inNumber", value = counter$countervalue )
  })
  
  output$plot <- renderPlot({
    hist( rnorm(input$inNumber))
  })
}


shinyApp(ui, server)
票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65030433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档