简体   繁体   English

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

[英]Update valuebox using action button in R Shiny

I'm currently training to build a shiny app.我目前正在训练构建 shiny 应用程序。

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]

在此处输入图像描述

I also try using numericInput instead of textInput and error still appear.我也尝试使用numericInput而不是textInput并且仍然出现错误。

Can anyone tell me how to do it correctly?谁能告诉我如何正确地做到这一点?

You should access it as a function with update_unicode() , also you got your button name wrong its input$actbtn_unicode您应该使用update_unicode()作为 function 访问它,而且您的按钮名称错误,它的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")
      )
    })
  }
)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM