2017-10-17 159 views
1

我正在創建一個反應靈敏的應用程序,它有幾個numericInput變量和一些selectInput變量。Shiny R:更改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>

你有什麼建議嗎?

回答

1

你不需要selectize.js所以你可以做:

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

這給出了一個「普通」的選擇輸入。

Next我試過你的代碼color <- "red",這個工程。

+0

所以如果它不是固體,沒有selectize.js,它的工作原理!好的,非常感謝! 它也可以是: 'if(!is.na(input $ V3)&input $ V3!= 1){color < - 「solid#9999CC」} else {color < - 「」} runjs(paste0 (「document.getElementById('V3')。style.border ='」,color,「'」))' –