You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shiny应用中URL新窗口打开时去除多余域名问题解决

解决Shiny DT表格中URL被自动添加仪表盘域名的问题

问题核心是你生成的URL没有添加协议前缀(https://),浏览器将其识别为相对路径,因此自动拼接了当前仪表盘的域名。要解决这个问题,只需在生成<a>标签时给URL加上https://前缀,让浏览器识别为绝对路径即可。

关键代码修改

找到原代码中生成链接的这一行:

data$Enlace <- paste0("<a href='",data$Enlace,"' target='_blank'>",data$Enlace,"</a>")

替换为:

# 给URL添加https://前缀,确保解析为绝对路径
data$Enlace <- paste0("<a href='https://", data$Enlace, "' target='_blank'>Ver Documento</a>")

注:这里把显示文本从长URL改成了Ver Documento(查看文档),让表格更整洁;如果需要保留原URL显示,只需把最后一个参数改回data$Enlace即可。

修改后的完整代码

library(shiny)
library(DT)

##### ####
data <- read.csv('data/competencia_desleal_v2.csv', sep = ",")
data <- data[, !names(data) %in% c("Sumilla", "Partes")]  ## Saque columnas que son muy anchas
# 修正URL路径问题:添加https://前缀
data$Enlace <- paste0("<a href='https://", data$Enlace, "' target='_blank'>Ver Documento</a>")
#download
#### SHINY UI ###
ui <- fluidPage(
  titlePanel("Resoluciones"),
  fluidRow(
    lapply(names(data)[5:length(names(data))], function(col) {  ## Exclude first four columns
      column(width = 3,
             selectInput(paste0("filter_", col), paste(col, "Filter"), choices = c("Todos", "Si", "No")))
    })
  ),
  DTOutput("table"),
  tags$head(
    tags$style(HTML("
      .dataTables_wrapper .dataTables_filter input {
        width: 100%;
        height: 40px;
        font-size: 16px;
        text-align: center;
        margin-bottom: 10px;
      }
      
      .dataTables_wrapper .dataTables_filter label {
        text-align: center;
        width: 100%;
      }
    "))
  )
)

server <- function(input, output) {
  
  
  filtered_data <- reactive({
    data_filtered <- data
    for (col in names(data)[5:length(names(data))]) {  ## Exclude first four columns
      if (!is.null(input[[paste0("filter_", col)]])) {
        filter_value <- input[[paste0("filter_", col)]]
        if (filter_value != "Todos") {  ## Change from "All" to "Todos"
          data_filtered <- data_filtered[data_filtered[[col]] == filter_value, ]
        }
      }
    }
    return(data_filtered)
  })
  
  output$table <- renderDT({
    datatable(filtered_data(), selection = "none", 
              options = list(
                language = list(
                  search = "Buscar",
                  lengthMenu = "Ver _MENU_ entradas"
                )
              ),  escape = FALSE)
  })
  
}

shinyApp(ui = ui, server = server)

修改后,点击链接时浏览器会直接访问https://servicio.indecopi.gob.pe/...,不会再拼接仪表盘的域名,即可正常打开并下载PDF文件。

内容的提问来源于stack exchange,提问作者Juan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 15:23:15