0

Hopefully the title is pretty self explanatory. Below is simple example that reproduces the problem. What I want is for the letter (a-z) that labels the button to be added into the textInput box in the app once the button is clicked.

Note that somehow pulling the letter value from the vector of letters (p1) will not work, as my real app has a virtually limitless number of words that a button could be labeled. I need to get the actual value that is passed to the button label (value1).

Right now, I get the (HTML?) code, instead.

Note that I have tried:

Capture label of actionButton in Shiny app

AND

Capture the label of an actionButton once it is clicked

and while they helped me get closer, they do not solve the current problem.

library(shiny)
shinyUI(fluidPage(
# Application title
    titlePanel("Letter Predictor"),
# Sidebar with action button
    sidebarLayout(
        sidebarPanel(
            actionButton("word1", label = textOutput("value1"))
        ),
# Text entry box
        mainPanel(
            textInput(inputId = "text", label = h3("Text input"), value = "Enter a number (1-26)"),
        hr()
            )
        )
    )
)

shinyServer(function(input, output, session) {
# Letter prediction function to be used in Shiny Application
    predictalg <- function(userinput, predictor, predicted){
            pred <- predicted[which(predictor == userinput)]
            return(pred)
    }
# Vectors for making predictions
    p1 <- letters
    p2 <- seq_along(letters)
# Prediction function applied
    value1 <- reactive({
            predictalg(userinput = { trimws(input$text) },
                       predictor = p2,
                       predicted = p1)
    })
    output$value1 <- value1
# Register button click and (ideally) pass label (value1) to updateTextInput
    observeEvent(input$word1, {
        name <- paste(input$text, textOutput("value1"))#PROBLEM WITH value1
            updateTextInput(session, "text", value = name)
    })
})

I'm a novice with shiny, and I'm stuck. Any help would be appreciated!

Community
  • 1
  • 1
davo1979
  • 211
  • 1
  • 3
  • 12

1 Answers1

0

Per Michal Majka:

You get HTML code because this is what

textOutput("value1")

is. If you make following change:

name <- paste(input$text, value1() ) 

you will (probably) get what you want.

The parentheses "()" after value1 were the key.

davo1979
  • 211
  • 1
  • 3
  • 12