如何根据字符串而不是值更改 shinydashboard 中信息框的颜色?

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

如果条目是“x”或“y”,我一直在尝试更改 InfoBox 的颜色。我在这里发现了这个问题:How do I change the color of an infobox in shinydashboard based on the value displayed。给出的答案适用于数值数据,但我无法使用字符串。这是一个可重现的代码,适用于数值。

```{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)

```

此外,它对我不起作用,我尝试 if (input$wt1 = 2) 而不是 > 2。有什么想法吗?

使用两个等号,例如if (input$wt1 == 2)if (input$wt1 == "red")

R 对待 = 的方式与 == 不同。第一个用于分配事物,而第二个用于测试两个事物是否相等。