Search code examples
rshinyflexdashboard

How to use actionButton when leaving submitButton in flexdashboard?


I would like the renderValueBox to be calculated when I hit a button, not automatically.

I tried using actionButton but it didn't work. I was also unable to use the submitButton function in flexdashboard, but it didn't work either.

I would like to do this using only Shiny::actionButton function (recommended by documentation). My code:

---
title: "Sum"
runtime: shiny
output: 
  flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: fill
theme: yeti
---

side{.sidebar}
-------------------------------------------

```{r}
library(tibble)
library(shiny)
library(shinyWidgets)
library(flexdashboard)
library(scales)
```


**Análise**

```{r}
autonumericInput(
inputId = "a", 
value = 0, 
label = "Value 1", 
align = "center", 
currencySymbol = "R$", 
currencySymbolPlacement = "p",
decimalPlaces = 2,
digitGroupSeparator = ".",
decimalCharacter = ","
)

autonumericInput(
inputId = "b", 
value = 0, 
label = "Value 2", 
align = "center", 
currencySymbol = "R$", 
currencySymbolPlacement = "p", 
decimalPlaces = 2,
digitGroupSeparator = ".",
decimalCharacter = ","
)

actionButton("execute", "Calcule")
```

```{r}
f_1 <- function(a, b) {
  a + b
}
```

```{r}
reac <- eventReactive(input$execute, {
  x = tibble(
    a = input$a, 
    b = input$b
  )
}, ignoreNULL = FALSE)

pred <- reactive({
  temp <- reac()
  f_1(
    a = input$a, 
    b = input$b
  )
})
```

abc{}
--------------------------------------

###
```{r}
renderValueBox({
  valueBox(
    value = scales::dollar(x = round(x = pred(), digits = 4), prefix = "$ ", suffix = " dollar(s)", big.mark = ",", decimal.mark = "."), 
    caption = "Sum", 
    color = "steelblue", 
    icon = "fa-plus"
  )
})
```

Solution

  • ---
    title: "Sum"
    runtime: shiny
    output: 
      flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: fill
    theme: yeti
    ---
    
    side{.sidebar}
    -------------------------------------------
    
    ```{r}
    library(tibble)
    library(shiny)
    library(shinyWidgets)
    library(flexdashboard)
    library(scales)
    ```
    
    
    **Análise**
    
    ```{r}
    autonumericInput(
    inputId = "a", 
    value = 0, 
    label = "Value 1", 
    align = "center", 
    currencySymbol = "R$", 
    currencySymbolPlacement = "p",
    decimalPlaces = 2,
    digitGroupSeparator = ".",
    decimalCharacter = ","
    )
    
    autonumericInput(
    inputId = "b", 
    value = 0, 
    label = "Value 2", 
    align = "center", 
    currencySymbol = "R$", 
    currencySymbolPlacement = "p", 
    decimalPlaces = 2,
    digitGroupSeparator = ".",
    decimalCharacter = ","
    )
    
    actionButton("execute", "Calcule")
    ```
    
    abc{}
    --------------------------------------
    
    ###
    ```{r}
    renderValueBox({
        req(input$execute)
      valueBox(
        value = scales::dollar(x = round(x = isolate(input$a) + isolate(input$b), digits = 4), prefix = "$ ", suffix = " dollar(s)", big.mark = ",", decimal.mark = "."), 
        caption = "Sum", 
        color = "steelblue", 
        icon = "fa-plus"
      )
    })
    ```
    

    The trick here is to use isolate() wrap your inputs, so these inputs will not trigger the update of the box and add the req(input$execute) to trigger the updates.