【问题标题】:Shiny - Access actionButton value created dinamically with random valuesShiny - 使用随机值动态创建的访问操作按钮值
【发布时间】:2018-02-25 21:18:35
【问题描述】:

我正在使用随机字母给出的标签动态创建一个 actionButton(简化示例)。单击按钮后,我希望将标签粘贴到文本输入中。

library(shiny)
runApp(list(ui=
        shinyUI(fluidPage(

            mainPanel(
                textInput("text", label = ""),
                div(style="display:inline-block", uiOutput("wordOneButton"))
            )
        ))
,
server=shinyServer(function(session, input, output) {
# Fill the buttons with the random letter
output$wordOneButton <- renderUI({
    actionButton("action", label = LETTERS[sample(1:length(LETTERS), 1)])})


# Include predicted word in the text after click event on button
observeEvent(input$action, {
    name <- paste0(input$text, ????????, sep = " ")   <--------------
    updateTextInput(session = session, "text", value = name)
})

})

))

我应该放什么而不是???????。我已经尝试过input$wordOneButtoninput$action。但结果不是我所期待的。

谢谢!

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    也许您可以将字母存储在reactiveVal 中并调用该值。工作示例如下所示,请注意,您可以通过删除 observeEvent 的最后一行来保持 actionButton 的值相同。希望这会有所帮助!



    library(shiny)
    ui = fluidPage(
    
      mainPanel(
        textInput("text", label = ""),
        div(style="display:inline-block", uiOutput("wordOneButton"))
      )
    )
    
    server = function(session, input, output) {
    
      # reactiveVal to store our random letter.
      reval <- reactiveVal(sample(LETTERS,1))
    
      # Fill the buttons with the random letter
      output$wordOneButton <- renderUI({
        actionButton("action", label = reval())
      })
    
    
      # Include predicted word in the text after click event on button
      observeEvent(input$action, {
        name <- paste0(input$text, reval(), sep = " ") 
        updateTextInput(session = session, "text", value = name)
        reval(sample(LETTERS,1)) # comment this line to keep actionButton the same.
      })
    }
    
    shinyApp(ui,server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-08
      • 2019-07-18
      相关资源
      最近更新 更多