你可以做這樣的事情
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格式