如何设置搜索栏来搜索整个Shiny应用页面?

问题描述 投票:0回答:1

我想放置一个搜索栏,用于在 Shiny 应用程序中查找某个单词的所有出现位置。我有这段代码可以与 boton_google 一起使用,所以我希望它可以与所有按钮一起使用。例如,如果我正在搜索“Abrir”,那么计数和标记的单词必须是两个。我认为问题出在函数

contextId
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)

当我有多个

contextId
时如何解决问题?

r shiny searchbar
1个回答
0
投票

id
分配给
fluidPage
并将其作为
contextId
传递给
searchbar

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

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.