2016-04-17 267 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格式