在Shiny App中更改文本的颜色和字体

Piy*_*ush 32 fonts styles r shiny

我使用下面的代码在server.R主面板中显示文本.这正是它应该工作的方式.

output$text1 <- renderText({
  if(input$ag == 0) return(NULL)
  return('First 20 rows for requested AG')
})
Run Code Online (Sandbox Code Playgroud)

有没有办法改变文字的字体和颜色?

jdh*_*son 38

您可以使用css作为@jbaums指示

library(shiny)
runApp(list(
  ui = bootstrapPage(
    numericInput('n', 'Number of obs', 100),
    textOutput('text1'),
    tags$head(tags$style("#text1{color: red;
                                 font-size: 20px;
                                 font-style: italic;
                                 }"
                         )
              )
  ),
  server = function(input, output) {
    output$text1 <- renderText({ paste("hello input is",input$n) })
  }
))
Run Code Online (Sandbox Code Playgroud)

通常你会将它包含在一个styles.css文件中,但它在这里显示为自包含.#text1指的是DOM元素,id=text1大括号的内容是相关的样式.

  • 对于任何_确实_想要分离 R 和 CSS 的人,此博客包含如何使用单独的 CSS 文件实现此目的的好示例:https://shiny.rstudio.com/articles/css.html (2认同)

ath*_*shi 30

如果只想更改返回字符串的某个部分,可以使用htmlOutput而不是textOutput

在服务器端只返回

output$text1 <- renderText({ paste("hello input is","<font color=\"#FF0000\"><b>", input$n, "</b></font>") })
Run Code Online (Sandbox Code Playgroud)

通过这种方式,Shiny UI将执行HTML.


小智 22

ui.r:

span(textOutput("message"), style="color:red")
Run Code Online (Sandbox Code Playgroud)

server.r:

output$message <- renderText({"This is some red text"})
Run Code Online (Sandbox Code Playgroud)

  • 除此之外,如果要在 UI 中保持静态,您也可以在没有“textOutput”的情况下执行相同的操作。 (2认同)

Kas*_*sen 7

@MikeP的解决方案还可以与p()fx一起使用p("some text", style = "color:red),因此,renderText()如果要动态显示它,也可以将其包装在服务器中。