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

时间:2021-01-15 15:14:10

标签: r shiny 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$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]

enter image description here

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

谁能告诉我如何正确地做到这一点?

1 个答案:

答案 0 :(得分:0)

您应该使用 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")
      )
    })
  }
)

enter image description here