R 闪亮:突出显示特定颜色的文本输入

R 闪亮:突出显示特定颜色的文本输入,r,shiny,R,Shiny,我有一个闪亮的应用程序,用户可以将文本写入文本输入字段,每当文本出现在我的数据框中,文本就会显示在tableOutput中。然后,用户的文本输入应该在输出表中以某种颜色突出显示,比如红色。 我该怎么做 library(shiny) df = tibble(Text=c("The quick brown fox", "jumps over", "the lazy dog")) ui =fluidPage( fluid

我有一个闪亮的应用程序,用户可以将文本写入文本输入字段,每当文本出现在我的数据框中,文本就会显示在tableOutput中。然后,用户的文本输入应该在输出表中以某种颜色突出显示,比如红色。 我该怎么做

library(shiny)

df = tibble(Text=c("The quick brown fox", "jumps over", "the lazy dog"))

ui =fluidPage(
    
    fluidRow(
        
        textInput("input", "Textinput"),
        tableOutput("output")
    )
)

server = function(input, output){
    
    df_reactive = reactive({
        df %>%
            filter(str_detect(text, input$input))
    })
    
    output$output = renderTable({
        df_reactive()["text"]
    })
}

库(闪亮)
图书馆(tibble)
css

库(闪亮)
图书馆(tibble)

谢谢你的回答,但不完全是。彩色文本应显示在输出表中:例如,用户键入“dog”,则输出表中的“dog”为红色。我本应该更努力的concrete@lucaskr啊好的。这听起来更复杂。让我想想。@lucaskr看到我编辑的答案。我会添加一个
字体权重:粗体或另一种
背景色
,因为只有红色突出显示的文本不是很直观。这看起来不错,方向正确,但不幸的是,数据框中不匹配的行没有被过滤掉。e、 g.如果用户键入“dog”,则只应显示出现“dog”的行,并且只应为dog着色red@lucaskr编辑添加。谢谢你的回答,但不完全是。彩色文本应显示在输出表中:例如,用户键入“dog”,则输出表中的“dog”为红色。我本应该更努力的concrete@lucaskr啊好的。这听起来更复杂。让我想想。@lucaskr看到我编辑的答案。我会添加一个
字体权重:粗体或另一种
背景色
,因为只有红色突出显示的文本不是很直观。这看起来不错,方向正确,但不幸的是,数据框中不匹配的行没有被过滤掉。e、 g.如果用户键入“dog”,则只应显示出现“dog”的行,并且只应为dog着色red@lucaskr已添加编辑。是否要使用
DT
包?它具有突出显示的“搜索”功能,请参阅。我相信这是一个更容易的解决方案。使用
DT
肯定更容易,但是由于
DT
不适合我整个应用程序的需要,我不能使用它。你不想使用
DT
包吗?它具有突出显示的“搜索”功能,请参阅。我相信这是一个更容易的解决方案。使用
DT
肯定更容易,但由于
DT
不适合我整个应用程序的需要,我无法使用它
library(shiny)
library(tibble)

css <- "
mark {
  padding: 0;
  background-color: white;
  color: red;
}
"

df = tibble(text = c("The quick brown fox", "jumps over", "the lazy dog"))

ui = fluidPage(
  
  tags$head(
    tags$style(HTML(css))
  ),
  
  fluidRow(
    column(
      width = 12,
      textInput("input", "Textinput"),
      tableOutput("output")
    )
  )
  
)

server = function(input, output){
  
  highligthed <- reactive({
    if(input$input != ""){
      gsub(paste0("(", input$input, ")"), "<mark>\\1</mark>", df[["text"]])
    }else{
      df[["text"]]
    }
  })
  
  df_reactive = reactive({
    tibble(text = highligthed())
  })
  
  output$output = renderTable({
    df_reactive()["text"]
  }, sanitize.text = function(x) x)
  
}

shinyApp(ui, server)
  highligthed <- reactive({
    x <- df[["text"]][str_detect(df[["text"]], input$input)]
    if(input$input != ""){
      gsub(paste0("(", input$input, ")"), "<mark>\\1</mark>", x)
    }else{
      x
    }
  })
library(shiny)
library(tibble)
library(dplyr)
library(stringr)

df = tibble(text = c("The quick brown fox", "jumps over", "the lazy dog"))

ui = fluidPage(
  
  tags$head(
    uiOutput("CSS")
  ),
  
  fluidRow(
    column(
      width = 12,
      textInput("input", "Textinput"),
      tableOutput("output")
    )
  )
  
)

server = function(input, output){
  
  detect <- reactive({
    str_detect(df[["text"]], input$input)
  })
  
  df_reactive = reactive({
    df %>% filter(detect())
  })
  
  output$output = renderTable({
    df_reactive()["text"]
  })
  
  output$CSS = renderUI({
    color <- ifelse(any(detect()), "red", "black")
    css <- sprintf("#input {color: %s;}", color)
    tags$style(HTML(css))
  })
  
}

shinyApp(ui, server)