使用 R Shiny 中的操作按钮更新值框

Update valuebox using action button in R Shiny

我目前正在接受培训以构建一个闪亮的应用程序。

我想做一个值框,值是基于文本输入的,点击操作按钮后更新

我使用这个脚本:

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

并且显示错误:

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

我也尝试使用 numericInput 而不是 textInput,但仍然出现错误。

谁能告诉我正确的做法?

您应该使用 update_unicode() 作为函数来访问它,而且您的按钮名称错误 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")
      )
    })
  }
)