Make bold text in HTML output R shiny

2020-03-01 07:18发布

问题:

Reproducible example:

require(shiny)
runApp(list(ui = pageWithSidebar(
headerPanel("Example"),
  sidebarPanel(
    sliderInput("index", 
                label = "Select a number",
                min = 1,
                max = 4,
                step = 1,
                value = 2)),
  mainPanel(
  htmlOutput("text")
  )),
server = function(input, output) {
  output$text <- renderUI({
    HTML(paste(c("banana","raccoon","duck","grapefruit")))
  })
}
))

I would like to have the word corresponding to index ("raccoon" in the default) displayed in bold and the other words in normal font.

If I do:

HTML(
<b>paste(c("banana","raccoon","duck","grapefruit")[input$index])<\b>,
paste(c("banana","raccoon","duck","grapefruit")[setdiff(1:4,input$index)])
)

I receive an error (< is not recognized)...

回答1:

One more try, is this helpful?

require(shiny)

fruits <- c("banana","raccoon","duck","grapefruit")

runApp(list(ui = pageWithSidebar(
  headerPanel("Example"),
  sidebarPanel(
    sliderInput("index", 
                label = "Select a number",
                min = 1,
                max = 4,
                step = 1,
                value = 2)),
  mainPanel(
    htmlOutput("text")
  )),
  server = function(input, output) {
    output$text <- renderUI({
      fruits[input$index] <- paste("<b>",fruits[input$index],"</b>")
      HTML(paste(fruits))
    })
  }
))


回答2:

This might help you:

shinyApp(
  ui <- basicPage(
    uiOutput(outputId = "text")

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

    output$text <- renderText({
      HTML(paste0("<b>","bold","</b>", " not bold"))
    })

  })

Is that what you were looking for?



回答3:

If you're not set on using the HTML function, I believe you should be able to use strong(paste(character_vector[index])) instead.



回答4:

Just use renderPrint instead of renderText

renderPrint({
HTML(paste0("El valor 1 es:", input$val1,"\n","el valor 2 es:",input$val2))
})