Reputation: 1
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
)}
I tried using conditionalPanel but it does not let me change panels based on a reactive output.
Thanks in advance
Upvotes: 0
Views: 2766
Reputation: 4467
You could do something like this
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))
Here the text input is parsed to color numbers smaller than the slider input red and larger than the slider input green. Normal text is not formatted.
The trick is using some html formatting in the output text
Upvotes: 3