闪亮R:更改selectInput的边框颜色

时间:2017-10-17 12:11:20

标签: javascript r shiny shinyjs shiny-reactivity

我正在创建一个反应敏捷的应用,它包含一些numericInput变量和一些selectInput个变量。

由于变量非常多并且它们用于估计干预措施,因此我引入颜色来表示那些与默认值不同的变量。我已经在this问题的@BigDataScientist的帮助下实现了这一点。提出了一个MWE:

library(shiny)
library(shinyjs)
ui<-shinyUI(
  fluidPage(
    useShinyjs(),
    numericInput("V1", "MyV1" , value = 5, min = 0, max= 100, step = 0.01),
    numericInput("V2", "MyV2" , value = 23, min = 0, max= 100, step = 0.01),
    selectInput("V3" , "MyV3" , selected = 1 , choices=c("Yes" = 1,"No" = 0))
  )
)
#
server<-shinyServer(function(input, output) {
  observe({
    if(!is.na(input$V1) & input$V1 != 5){color <- "solid #9999CC"}else{color <- ""}                     
    runjs(paste0("document.getElementById('V1').style.border ='", color ,"'"))
    if(!is.na(input$V2) & input$V2 != 23){color <- "solid #9999CC"}else{color <- ""}                     
    runjs(paste0("document.getElementById('V2').style.border ='", color ,"'"))
    if(!is.na(input$V3) & input$V3 != 1){color <- "solid #9999CC"}else{color <- ""}                     
    runjs(paste0("document.getElementById('V3').style.borderColor ='", color ,"'"))
    })
  })
#
shinyApp(ui,server)

正如您在下图中看到的,我的代码适用于数值,但对于选择输入变量不是(至少在交互式环境中)。

My example

现在不是条件不起作用,它被评估并且runjs运行。

此外,正如您在以下js片段中所看到的,js命令就可以了。可能有一种方法可以让它被反应(没有像numericInput 那样的提交按钮)。

<!DOCTYPE html>
<html>
<body>



<select id="mySelect">
  <option>Apple</option>
  <option>Orange</option>
  <option>Pineapple</option>
  <option>Banana</option>
</select>

<p>Click the button to change the style of the H1 element.</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
    document.getElementById("mySelect").style.borderColor = "red";
}
</script>

</body>
</html>

你有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您不需要selectize.js,所以您可以这样做:

selectInput("V3" , "MyV3" , selected = 1 , choices=c("Yes" = 1,"No" = 0), 
            selectize = FALSE)

这给了普通&#34;普通&#34;选择输入。

接下来,我已经使用color <- "red"尝试了您的代码,这很有效。

相关问题