I have been working on a shiny app that will allow users to pick a pdf from a file directory that is in the app and display it on screen using an iframe by letting users first select the folder that holds the pdfs, and then select a specific pdf inside of that folder. However, the way I have it setup now will not display the pdf on the screen, it instead shows a not found error in the iframe.
Here is my code:
library(shiny)
ui <- fluidPage(
headerPanel("Client"),
selectInput("folder", "Select the report type",
c(dir("Reports/"))),
conditionalPanel(
condition = "input.folder == 'Invoice Analysis'",
selectInput("report", "Choose the PDF",
choices = dir("Reports/Invoice Analysis"))),
conditionalPanel(
condition = "input.folder == 'Pole Attachments'",
selectInput("report", "Choose the PDF",
choices = dir("Reports/Pole Attachments"))),
htmlOutput("pdfviewer")
)
server <- function(input, output){
output$pdfviewer <- renderUI({
tags$iframe(src=(input$report), height=300, width=600)
})
}
shinyApp(ui = ui, server = server)
Here is the current output:
I can replace the src in the iframe with a website and it displays the site correctly.
example:
tags$iframe(src=("http://www.w3schools.com"), height=300, width=600)
This leads me to believe that somehow I am getting this section of my code wrong. I have also tried something like this:
tags$iframe(src=(paste0(input$folder, input$report), height=300, width=600)
but it gives me the same not found error. Can anyone think of a way to make the source of the iframe a selection from the user? I am very new at r and shinyapps so I don't know if this is a syntax problem, or if I am going about this the wrong way. Thank you in advance for the help.
Related
In my app, users should insert some data from an Excel file, but I want to make it possible for them to download one (TesteR.xlsx) in their computer and use it as an example. I have tried to apply this solution but it didn't work, when I click the button it downloads a kinda weird file.
library(shiny)
ui <- fluidPage(
downloadButton("downloadOP", label = "Download")
)
server <- function(input, output){
output$downloadOP <- downloadHandler(
filename = "ph1data",
content = function(file) {
file.copy("www/TesteR.xlsx", file)
}
)
}
shinyApp(ui, server)
I also included the file in a www folder like suggested in the other question, but maybe I am missing something.
Any help would be very much appreciated!
Make sure your www folder is in the same directory as your app.r or server.r/ui.r files. It must be readable by the shiny server.
As you don't state how your app is started/served (from your computer, on a server, what kind of server, using shiny/server, on shinyapps.io, shinyproxy, ...) further advice won't be very useful.
I will also add the file extension to filename = "ph1data.xlsx".
If you add a A tag to your UI, does it work ? (target="self" prevent opening a new tab)
ui <- fluidPage(
downloadButton("downloadOP", label = "Download"),
tags$a("Download", href="TesteR.xlsx", target="self")
)
If you put and image (eg test.jpg) in your www folder and add an IMG tag to your UI, does it show the image ?
ui <- fluidPage(
downloadButton("downloadOP", label = "Download"),
tags$a("Download", href="TesteR.xlsx", target="self", class="btn btn-primary"),
tags$img(src="test.jpg")
)
I am trying to build a front end using Shiny to pass some parameters to a markdown document. One parameter I want to pass is a user selected file name, including the path. I would like to use a file open type dialogue box for the user to navigate through directories and select the file. I do not need/want to actually open the file, that happens in the markdown file, I just want to pass the path and file name of the selected file out of the shiny app. I have this set up using fileInput() but of course this opens the file and makes a copy in a temp directory and the associated path is to the temp directory not the original directory. There have been some related questions about this and the only answers are that this is a security issue related to the server-based nature of shiny. Again, I don't want to open the file, just grab the original path and name. Any thoughts on how to achieve this? Here's the code stripped down to just this issue ...
library(shiny)
ui <- fluidPage(
titlePanel("Input"),
mainPanel(
fileInput(inputId = "rtffile", "Choose RTF File", accept = ".rtf", ),
)
server <- function(input, output, session) {
observe({
filename <<- input$rtffile
})
}
shinyApp(ui = ui, server = server)
In general, you can’t get a web browser to give you the path to a file on the
user’s local machine.
However, it’s possible to get a path to a file on the server.
If the server and the local machine happen to be the same, you can use e.g. shinyFiles to
pick a path:
library(shiny)
library(shinyFiles)
ui <- fluidPage(
titlePanel("Input"),
mainPanel(
shinyFilesButton("file", "Choose File", "Choose a file", multiple = FALSE),
verbatimTextOutput("file")
)
)
server <- function(input, output, session) {
roots <- getVolumes()
shinyFileChoose(input, "file", roots = roots)
file <- reactive(parseFilePaths(roots, input$file))
output$file <- renderPrint(file())
}
shinyApp(ui = ui, server = server)
i'm trying to show a logo in my Shiny app, but when i try to see i only can see a small icon, but not the image, this is my code:
library(shiny)
ui <- fluidPage(
mainPanel(
img(src='C:/Users/carlo239/image[![enter image description here][1]][1]/Capture2.jpg',
align = "right", height = '300px'),
### the rest of your code
)
)
server <- function(input, output, session) {
}
shinyApp(ui, server)
And this is the issue, i tried to reload, but is not working,
I know that the image is ok, because if i click on Download i get the same image, but i'm unable to see in my outputs. THANKS !!
There are two ways:
Put the file in the www folder of your app (as suggested by #YBS). That is in most cases the recommended solution.
"Register" your folder as resource folder for your app (see below). However, that solution is intended to help package authors make resources available to the package's components. Also be aware that absolute paths can pose problems when you deploy your app.
library(shiny)
ui <- fluidPage(
mainPanel(
img(src='/foo/Capture2.jpg',
align = "right", height = '300px'),
### the rest of your code
)
)
server <- function(input, output, session) {
addResourcePath("foo", "C:/Users/wherever/your/file/may/be/located")
}
shinyApp(ui, server)
I would like to be able to use display.mode = 'showcase' in an app run with the shinyApp() function call. According to the docs I should be able to pass any arguments that go runApp() through the options argument. The showcase mode works (the window is split) but it does not show the code. What's interesting is that if I run runExample("01_hello") everything works fine. I'm using shiny 1.0.5.
Code:
library(shiny)
ui <- fluidPage(
titlePanel("Sample App"),
sidebarLayout(
sidebarPanel(
selectInput("data", label = "Data set",
choices = c("mtcars", "iris"))
),
mainPanel(
tableOutput("table")
)
)
)
server <- function(input, output) {
data <- reactive({
get(input$data, 'package:datasets')
})
output$table <- renderTable({
head(data())
})
}
shinyApp(ui, server, options = list(display.mode = 'showcase'))
Output:
I was having the same issue. I had the app.R file and created the DESCRIPTION file using notepad but couldn't deploy a shinyApp with the code. Then I copied the DESCRIPTION file from shiny\examples\01_hello and noticed this:
Turns out my file had a TXT extension, so Shiny wasn't reading it as a metadata file. Once I used the correct DESCRIPTION file (which you can edit using notepad), everything worked out fine.
This is more of an addendum to Gus_est's answer, since I had the same problem and wasn't able to get it run right from there.
Create a file inside the directory your app.R-file resides in, e.g. a txt-file. Write into the file what display mode is to be used using Debian Control file format. In our case it would look like that (Title is not necessary):
Title: My App
DisplayMode: Showcase
Then rename the file DESCRIPTION without providing a file ending. Ignore the warning.
When you run the app now, it will always be in display mode "showcase", you can override this only inside the runApp()-statement. So I find the documentation to be misleading.
Check your current working directory. This problem seems to occur, if the working directory is not set to the folder with the app code.
I am trying to show some PDFs from around the web in an app on shinyapps.io. Unfortunately, the standard way of using an iframe with the URL is not an option because of the mixed-content safeguards (the pdfs are served via http). I think that a possible option is to download the pdfs from the url then display them in an iframe from the local file, but I cannot get this to work with tempfile().
A sample app:
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
textInput("url", "add a url"),
actionButton("button","hit the button"),
h5("use case - embed a pdf user guide in the app - embed as a local pdf or from web URL")
),
mainPanel(
tabsetPanel(
tabPanel("PDF",
htmlOutput("pdf")
)
)
)
)
)
server <- function(input, output, session) {
observeEvent(input$button, {
temp <- tempfile(fileext = ".pdf")
download.file(input$url, temp)
output$pdf <- renderUI({
tags$iframe(src=temp)
})
})
}
shinyApp(ui, server)
Sample pdf: http://www.pdf995.com/samples/pdf.pdf
When I open this in the browser I get an error in the browser console:
Not allowed to load local resource: file:///C:/Users/.../Local/Temp/Rtmp8subWX/file19403a2a2fc8.pdf and nothing in the panel where the iframe is.
A similar attempt uploaded to shinyapps.io failed as well, showing a 404 Not Found error in the pdf viewer.
I think this may be an issue with how shiny/shinyapps.io deal with temp files, but can't quite figure it out. Thanks.
You need to download the PDF in binary mode in a subfolder of your current directory, then call addResourcePath to allow shiny to serve it:
observeEvent(input$button, {
pdf_folder <- "pdf_folder"
if(!file.exists(pdf_folder))
dir.create("pdf_folder")
temp <- tempfile(fileext = ".pdf", tmpdir = "pdf_folder")
download.file(input$url, temp, mode = "wb")
addResourcePath("pdf_folder",pdf_folder)
output$pdf <- renderUI({
tags$iframe(src=temp)
})
})