ViviG
ViviG

Reputation: 1726

How do I change the color of an infobox in shinydashboard based on a string, not a value?

I have been trying to change the color of the InfoBox if the entry is "x" or "y". I found this question here: How do I change the color of an infobox in shinydashboard based on the value displayed. The answer given works for numerical data, but I can't get to work with string. Here is a reproducible code that works with numerical values.

```{r library and source, eval= FALSE }

library(shiny)
library(shinydashboard)

```


```{r UI, eval = FALSE}
ui <- dashboardPage(
  #Header
  dashboardHeader(title = "dashboard"),
  
  #Sidebar with download button
  dashboardSidebar(width = 130),
  
  #Body
  dashboardBody ((box
                  (
                    width = 6,
                    textInput("wt1", " Choose wt:", "")
                  )),
                 
                 infoBoxOutput("Output"))
)

```

```{r Server, eval = FALSE}

server <- function(input, output, session) {
  output$Output <- renderInfoBox({
    InfoTotal <- paste(input$wt1, "")
    
    if (input$wt1 > 2)
    {
      infoBox(
        "Change colors, please",
        InfoTotal,
        icon = icon("tint", lib = "glyphicon"),
        color = "red"
      )
    }
    else
      
    {
      infoBox(
        "Change colors, please",
        InfoTotal,
        icon = icon("tint", lib = "glyphicon"),
        color = "blue"
      )
    }
    
    
  })
  
  
}

shinyApp(ui, server)

```

Also, it doesn't work for me is I try if (input$wt1 = 2) instead of > 2. Any ideas?

Upvotes: 0

Views: 250

Answers (1)

Ash
Ash

Reputation: 1513

Use two equal signs, e.g. if (input$wt1 == 2) or if (input$wt1 == "red")

R treats = differently to ==. The first is for assigning things while the second is for testing whether two things are equal.

Upvotes: 1

Related Questions