rshinysearchbar

How to set up a search bar to search the entire Shiny app page?


I want to put a search bar that looks for all occurrences of a word inside a Shiny app. I have this code which works with the boton_google so I would like to have it work with all buttons. For example, if I am searching for "Abrir", then the counted and marked words have to be two. I think the problem is in the contextId parameter of the function searchbar().

library(shiny)
library(bs4Dash)
library(stringr)
library(rintrojs)
library(shinyjs)
library(shinyWidgets)
library(fresh)
library(shinyWidgets)
library(shinySearchbar)


caja_con_botones <- function(titulo, ..., botones) {
  div(
    h3(titulo, style = "color: steelblue;"),
    div(
      lapply(botones, function(boton) {
        div(boton, style = "margin-bottom: 5px;")
      })
    ),
    style = "text-align:center; border: 1px solid steelblue; padding: 10px;"
  )
}

# Función para crear botones
caja_elemento <- function(nombre, id, link, color) {
  color_class <- switch(color,
                        "rojo" = "btn-danger",
                        "azul" = "btn-primary",
                        "verde" = "btn-success",
                        "primary")  # Default to "primary" if color is not recognized

  tags$button(
    id = id,
    class = paste("btn", color_class),
    type = "button",
    onclick = paste0("window.open('", link, "', '_blank');"),
    nombre
  )
}

# Crear botones
boton_google <- caja_elemento("Abrir Google", "boton_google", "https://www.google.com/", "azul")
boton_facebook <- caja_elemento("Abrir Facebook", "boton_facebook", "https://www.facebook.com/", "red")

# UI de la aplicación Shiny
ui <- fluidPage(
  fluidRow(
    searchbar("buscador", contextId = "boton_google")
  ),
  fluidRow(
    caja_con_botones("Mis Enlaces",
                     botones = list(boton_google, boton_facebook))
  )
 
)

server <- function(input, output) {

}

shinyApp(ui, server)

How to fix the problem when I have multiple contextId?


Solution

  • Assign an id to the fluidPage and pass this as the contextId to searchbar.

    fluidPage(
      id = "appId",
      fluidRow(
        searchbar("buscador", contextId = "appId")
      ),
    ...
    

    enter image description here