Value box based on textInput and update the value by action button

Hello guys, I'm currently training to build a shiny app.

I want to make a value box, the value is based on text input, and its updating after click action button

I use this script:

shinyApp(
  ui <- dashboardPage(
    dashboardHeader(
      title = "Test action button"
    ),
    dashboardSidebar(),
    dashboardBody(
      fluidRow(
        box(
          textInput(
            "unicode",
            "Your Unique ID:",
            placeholder = "Input your unique ID here"
          ),
          actionButton(
            "actbtn_unicode",
            "Submit"
          ),
          width = 8
        ),
        valueBoxOutput(
          "vbox_unicode"
        )
      ),
    )
  ),
  server <- function(input, output){
    update_unicode <- eventReactive(input$act_unicode,{
      input$unicode
      
    })
    output$vbox_unicode <- renderValueBox({
      valueBox(
        update_unicode,
        "Your Unique ID",
        icon = icon("fingerprint")
      )
    })
  }
)

And it shows error:

Warning: Error in as.vector: cannot coerce type 'closure' to vector of type 'character'
  [No stack trace available]

enter image description here

I also try using numericInput instead of textInput and error still appear.

Can anyone tell me how to do it correctly?

1 Like

It solved guys. I got help from stackoverflow

So I miss to use function update_unicode().
The right script is look like this:

library(shiny)
library(shinydashboard)
shinyApp(
  ui <- dashboardPage(
    dashboardHeader(
      title = "Test action button"
    ),
    dashboardSidebar(),
    dashboardBody(
      fluidRow(
        box(
          textInput(
            "unicode",
            "Your Unique ID:",
            placeholder = "Input your unique ID here"
          ),
          actionButton(
            "actbtn_unicode",
            "Submit"
          ),
          width = 8
        ),
        valueBoxOutput(
          "vbox_unicode"
        )
      ),
    )
  ),
  server <- function(input, output){
    
    update_unicode <- eventReactive(input$actbtn_unicode,{
      input$unicode
    })
    
    output$vbox_unicode <- renderValueBox({
      valueBox(
        update_unicode(),
        "Your Unique ID",
        icon = icon("fingerprint")
      )
    })
  }
)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.