html - 如何将本地文件链接到 Shiny 的 UI.R 中的 html 查询?

标签 html r shiny href

我有一个 Shiny 的应用程序,我想要一个外部网络工具 (GenomeCompiler) 的链接来读取应用程序“~/www”文件夹中的文件。

来自 GenomeCompiler 的示例 html 代码使用网络存储库中的文件并在应用程序中运行良好(参见下面的代码)。预期的行为是在您运行代码后(请注意“源代码”不起作用,您需要运行它以使 shinyApp() 起作用)它会在网络浏览器(我使用的是 Firefox)中打开一个选项卡名为“plasmid1”的链接。单击它时,它会在浏览器中打开一个新选项卡,加载 GenomeCompiler 网站中的文件数据,并显示带有注释和其他数据的圆周。

##WORKING EXAMPLE
#Directory tree
#ui.R
#server.R
#www/OG34_OG34_pSF-OXB19.gb   # this is the file read by the tool, which can be downloaded and saved to the ~/www folder in the shiny app from the link:
#    <http://s3.amazonaws.com/gcc_production/plasmid_viewer/OG34_OG34_pSF-OXB19.gb>
library(shiny)
ui <- fluidPage(
# the GenomeCompiler supplied code is "https://designer.genomecompiler.com/plasmid_iframe?file_url=http://s3.amazonaws.com/gcc_production/plasmid_viewer/OG34_OG34_pSF-OXB19.gb"

  tags$a(href='https://designer.genomecompiler.com/plasmid_iframe?file_url=http://s3.amazonaws.com/gcc_production/plasmid_viewer/OG34_OG34_pSF-OXB19.gb', target='blank', 'plasmid1')
  )
server <- function(input, output){}

shinyApp(ui = ui, server = server)

我想使用这种方法读取本地 Shiny 目录“~/www”中的文件,但找不到正确的语法。在上面的例子中使用了

tags$a(href='https://designer.genomecompiler.com/plasmid_iframe?file_url=OG34_OG34_pSF-OXB19.gb', target='blank', 'plasmid1')
  )

但是在 genomecompiler web 加载之后,它给出了“错误加载文件(内部服务器错误)”。由于我的无知,我知道该应用程序在本地运行,但是这个外部工具在不同的地址运行,因此看不到应用程序本地文件。我的猜测是我必须通过 URL 为该工具提供本地文件的位置,但我不知道该怎么做。此外,我了解到 Firefox 和其他浏览器出于安全原因不允许读取本地文件,因此我想以正确的方式编写此代码,以便我可以安全地在线部署应用程序。

如果能帮助我使用适当的语法或方法在工具中读取应用程序本地文件,我将不胜感激。

提前致谢!

最佳答案

您可以使用如下方式指向本地文件:

tags$a(href='data/plasmid1.txt', target='blank', 'plasmid1_localfile')

如果要下载文件:

tags$a(href='data/plasmid1.txt', target='blank', 'plasmid1_localfile', download = 'plasmid1.txt')

注意:plasmid1.txt(文件)必须位于 www 文件夹中。

你可以试试这个简单的例子:

目录树

ui.R
服务器.R
www/data/plasmid1.txt

(创建一个名为“plasmid1.txt”的空文本文件)

ui.R

library(shiny)

# Define UI for application that plots random distributions 
shinyUI(pageWithSidebar(

  # Application title
  headerPanel("Hello Shiny!"),

  # Sidebar with a slider input for number of observations
  sidebarPanel(
    sliderInput("obs", 
                "Number of observations:", 
                min = 1,
                max = 1000, 
                value = 500), 

# Line spacing
hr(),

# Adding the 'a' tag to the sidebar linking external file
tags$p("'a' tag linking external file"),
tags$a(href='https://designer.genomecompiler.com/plasmid_iframe?file_url=http://s3.amazonaws.com/gcc_production/plasmid_viewer/OG34_OG34_pSF-OXB19.gb', target='blank', 'plasmid1_URLfile'),

# Line spacing
hr(), 

# Adding the 'a' tag to the sidebar linking local file
tags$p("'a' tag linking local file"),
tags$a(href='data/plasmid1.txt', target='blank', 'plasmid1_localfile', download = 'plasmid1.txt')
),

  # Show a plot of the generated distribution
  mainPanel(
    plotOutput("distPlot")
  )
))

服务器.R

library(shiny)

# Define server logic required to generate and plot a random distribution
shinyServer(function(input, output) {

  # Expression that generates a plot of the distribution. The expression
  # is wrapped in a call to renderPlot to indicate that:
  #
  #  1) It is "reactive" and therefore should be automatically 
  #     re-executed when inputs change
  #  2) Its output type is a plot 
  #
  output$distPlot <- renderPlot({

    # generate an rnorm distribution and plot it
    dist <- rnorm(input$obs)
    hist(dist)
  })
})

另见:how-do-i-add-a-link-to-open-a-pdf-file-in-a-new-window-from-my-r-shiny-app

关于html - 如何将本地文件链接到 Shiny 的 UI.R 中的 html 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39039424/

相关文章:

html - 如何在 CSS 中将子元素的边框绘制在其父元素之外(或模拟这种效果)?

html - Youtube iframe自动播放功能不适用于Chromium,但适用于Manjaro Linux上的Firefox

r - 修复数据框中的混合日期格式?

R Leaflet (CRAN) - 如何注册点击标记

r - 如何使用 tmap 与 Shiny

html - 网格布局的自动裁剪图像

javascript - 在 Highcharts 中无法将 y 轴设置为 100

标签旋转以跟随ggplot2中的x轴,极坐标投影?

string - 将整数字符串日期转换为实际日期

r - Shiny R - 下载表格的结果