Shiny R:textInput 的条件样式

Shiny R: Conditional style for textInput

我想根据 selectInput 输出中的选择更改一些 textInput 标签 的颜色。

这个想法是显示,新的数据是基于另一个选择的交互式文本。文本本身,我已经管理 (updateTextInput)。我想为他们的标签做一些类似的事情,因为并非所有 textInput 都改变。

那么,如何根据 TagColor 输入指示的颜色更改 Pop textInput 的颜色,然后如何将颜色重置为其默认样式?

library(shiny)

ui<-shinyUI(
  fluidPage(
    selectInput("TagColor", "Color of my Tag",choices=c("Red","Blue",
    "Yellow","Black", "Initial"), 
    selected = "Red", multiple = FALSE),
    textInput("Pop", "Var1", "Test")
  )
)

server<-shinyServer(function(input, output) {


})


shinyApp(ui,server)

一种方法是使用 javascript 覆盖 CSS class: 要将其更改为红色,将使用以下 JS 片段: document.getElementById('Pop').style.border = 'solid red"

所以要将它与您的输入一起使用,您可以这样写:

paste0("document.getElementById('Pop').style.border = 'solid ", tolower(input$TagColor) ,"'")

要将其包含在您闪亮的应用程序中,您可以使用 shinyjs 包。

完整的应用程序:

library(shiny)
library(shinyjs)

ui<-shinyUI(
  fluidPage(
    useShinyjs(),
    selectInput("TagColor", "Color of my Tag",choices=c("Red","Blue",
                                                        "Yellow","Black", "initial"), 
                selected = "Red", multiple = FALSE),
    textInput("Pop", "Var1", "Test")
  )
)

# 

server<-shinyServer(function(input, output) {
    observe({
      color <- paste0("solid ", tolower(input$TagColor))
      if(color == "solid initial") color <- ""
      runjs(paste0("document.getElementById('Pop').style.border =
      '", color ,"'"))
    })
})

shinyApp(ui,server)