2017-05-21 2 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 클래스 덮어 자바 스크립트를 사용하는 것입니다 : 이 빨간색으로 변경하기를, 다음 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

멋지세요! 감사! 'runApp (shinyApp (ui, server), launch.browser = TRUE) '에 직면 한 아주 작은 문제는 제게는 효과가 없었습니다. –

+0

정확히 무엇이 문제입니까? 'runApp()'는 작동하지 않지만'shinyApp()'는 무엇을합니까? – BigDataScientist

+0

. 그것은 나를 위해 동시에 작동하지 않습니다. 나는'runApp()'을 삭제하고'shinyApp (ui, server)'로 변경했습니다. –