Shiny R:textInput

时间:2017-05-21 17:31:30

标签: r shiny conditional

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

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

那么,如何根据Pop输入指示的颜色更改TagColor 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)

1 个答案:

答案 0 :(得分:2)

一种方法是使用javascript覆盖CSS类: 要将其更改为红色,将使用以下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)