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,但它不允许我更改面板 反应性输出。
提前致谢
答案 0 :(得分:2)
你可以做这样的事情
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格式