0
votes

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 show 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

1 Answers

0
votes

You should access it as a function with update_unicode(), also you got your button name wrong its input$actbtn_unicode

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")
      )
    })
  }
)

enter image description here