2016-04-17 13 views
0

server.R変更色

reference <- input$a * input$b (More complex formula) 

output$text <- rendertext({ 
if(reference > 20) 
# Some text in red 
else 
# Some text in green 
)} 

私はconditionalPanelを使用してみましたが、それは私が 反応性出力に基づいて、パネルを変更することはできません。事前

答えて

1

おかげであなたはここで、テキスト入力は、赤と緑のスライダー入力よりも大きなスライダー入力よりも小さい色番号に解析され、この

library(shiny) 

ui <- fluidPage(
    sidebarLayout(
    sidebarPanel(
     # Input to react to 
     sliderInput('slider1','Render text format condition',-1000,1000,value=0,step=1) 
    ), 
    mainPanel(
     # Text input 
     textInput('txtIn','Text input'), 
     # Text output 
     htmlOutput('txtOut') 
    ) 
) 
) 

server <- function(input, output, session) { 

    output$txtOut <- renderText({ 
    # Split into words 
    words <- strsplit(input$txtIn,' ')[[1]] 
    outTxt <- ' ' 

    # Parse input based on logic 
    if(length(words) == 0) return('') 

    # Loop trough words 
    for (i in 1:length(words)){ 
     curr.word <- words[i] 

     # If string can be converted to a number 
     if(!is.na(as.numeric(curr.word))){ 
     curr.word.num <- as.numeric(curr.word) # Just in case 

     # Determine formating 
     if (curr.word.num >= input$slider1){ 
      font <- 'green' 
     } else if (curr.word.num < input$slider1){ 
      font <- 'red' 
     } else { 
      font <- 'gray' 
     } 

     # Create html 
     formatedFont <- sprintf('<font color="%s">%s</font>',font,curr.word) 

     # Append to text to show 
     outTxt <- paste(outTxt, formatedFont,collapse=' ') 

     } else{ 
     outTxt <- paste(outTxt, curr.word,collapse=' ') 
     } 
    } 
    outTxt 
    }) 

} 

runApp(shinyApp(ui,server)) 

ような何かを行うことができます。通常のテキストはフォーマットされていません。

トリックは、出力テキストにいくつかのhtmlフォーマットを使用しています