javascript - Shiny 的 R : Change border color of selectInput

标签 javascript r shiny shinyjs shiny-reactivity

我正在创建一个相当被动的应用程序,它有一些 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)

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

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>

你有什么建议吗?

最佳答案

你不需要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/

相关文章:

javascript - 如何在谷歌地图中突出显示用户的当前位置?

r - 大合并/内存管理

r - 在命令行 (shell/bash) : what to do when column names contain tilde (~) 中将参数传递给 R 脚本

javascript - API:使构造函数在 JavaScript 中无需括号即可链接

javascript - 如何将 SVG 图像添加到 javascript html5 Canvas 动画?

javascript - 如何让 php `json_encode` 返回 Javascript 数组而不是 Javascript 对象?

r - 有条件地更改 dplyr 链中日期的年份部分

r - 测试模块化 R Shiny (golem) 仪表板

r - 将公司 Logo 添加到 ShinyDashboard 标题

r - 在 ggplotly 中保持 x 和 y 比例相同(所以方形图)