闪亮 - 访问actionButton值以随机值创建

时间:2018-02-25 12:43:22

标签: r shiny

我正在使用随机字母给出的标签动态创建一个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。但结果并不是我所期待的。

谢谢!

1 个答案:

答案 0 :(得分:1)

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

enter image description here

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)