2017-05-21 19 views
2

textInputラベルの色をselectInputの出力に基づいて変更したいとします。Shiny R:textInputの条件付きスタイル

アイデアは、インタラクティブテキストの別の選択肢に基づいて、どのデータが新しいかを示すことです。テキスト自体は、私が管理している(updateTextInput)。私は彼らのラベルに似た何かをしたいと思います。すべてtextInputが変更されていません。

TagColor入力から指定された色に基づいてPop 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) 

答えて

2

一つの方法は、CSSクラスを上書きするためにJavaScriptを使用して次のようになります。 赤にそれを変更するには、以下の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) 
+0

のwonderfull!ありがとう! 'runApp(shinyApp(ui、server)、launch.browser = TRUE) 'に直面するちょっとした問題ですが、それは私の場合はうまくいきませんでしたが、それはちょうど私かもしれません。 –

+0

何が問題なのですか? 'runApp()'は動作しませんが、 'shinyApp()'はどうしますか? – BigDataScientist

+0

。それは私のために並行して動作しません。私は 'runApp()'を削除し、 'shinyApp(ui、server)'に変更しました。 –

関連する問題