我正在创建一个相当被动的应用程序,它有一些 numericInput
变量和一些 selectInput
变量。
由于变量非常多并且它们用于估计干预措施,因此我引入颜色来表示那些与默认值不同的变量。我已经在@BigDataScientist 的帮助下在 this 中实现了这个问题。提出了一个 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)
正如您在下图中看到的,我的代码适用于数值,但它不适用于选择输入变量(至少在交互式环境中)。
现在不是条件不工作,它被评估并且 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>
你有什么建议吗?
最佳答案
你不需要selectize.js
所以你可以这样做:
selectInput("V3" , "MyV3" , selected = 1 , choices=c("Yes" = 1,"No" = 0),
selectize = FALSE)
这给出了一个“普通的”选择输入。
接下来我用 color <- "red"
试过你的代码这行得通。
关于javascript - Shiny 的 R : Change border color of selectInput,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46789835/