Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to link a local file to an html query in the shiny UI.R?

Tags:

html

href

r

shiny

I have a shiny app which I would like to have links for an external web tool (GenomeCompiler) to read files in the app '~/www' folder.

The example html code from GenomeCompiler uses a file in a web repository and works fine in the app (see code below). The expected behaviour is that after you run the code (note that 'source' won't work, you need to run it for shinyApp() to work) it opens a tab in the web browser (I'm using Firefox) with a link called 'plasmid1'. When you click on it, it opens a new tab in the browser which loads the file data in the GenomeCompiler web site and shows a circumference with annotations plus other data.

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

I would like to use that approach to read a file in the local shiny directory '~/www', but cannot find the correct syntax. In the example above have used

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

but the after the genomecompiler web loads it gives an 'error loading file (internal server error)'. From my ignorance I understand that the app is working locally, but this external tool is working from a different address and therefore cannot see the app local file. My guess is that I have to give the tool the location of the local file with an URL, but I don't know how to do it. Also, I have read that Firefox and other browsers do not allow local files to be read due to security, so I would like to write this code in the right way so I can safely deploy the app online.

I would appreciate help to use the appropriate syntax or approach for the app local file to be read in the tool.

Thanks in advance!

like image 475
Rubbaa Avatar asked Nov 28 '25 15:11

Rubbaa


1 Answers

You can point to a local file using something like this:

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

if you want to download the file:

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

Note: the plasmid1.txt (file) have to be inside the www folder.

You can try this simple example:

Directory tree

ui.R
server.R
www/data/plasmid1.txt

(create an empty text file called "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")
  )
))

server.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)
  })
})

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

like image 153
Guz Avatar answered Dec 01 '25 04:12

Guz