I'm trying to get videos saved locally to play in RShiny but I only get an empty video player with no video loaded or way to load one. I've seen several posts on this when searched but none seem to reach an answer. My code is:
library(shiny)
ui <- fluidPage(
tags$video(src = "C:/Downloads/big_buck_bunny_720p_30mb.mp4", type
= "video/mp4", autoplay = TRUE, controls = TRUE)
)
server <- function(input, output, session) {
}
shinyApp(ui, server)
This results in a mini player loading in RStudio browser or Edge but no video:
Screenshot
However, if I view source on the Edge page, save that as an .html file and load that file in Edge it works fine. It just doesn't work when run from RStudio as in Edge won't display videos when the address is of the form http://127.0.0.1:6190/. Any ideas? Security settings or something? RStudio plays the same video fine if I instead use a web URL rather than local video file as well.
I've tried loads of iterations of ui.r / server.r, files in www folder, etc. none of which work. Rstudio native browser has same behaviour as Edge (although it can't display the .html file either).
Thanks #ismirsehregal - this gets me a lot further. The video now plays but weirdly looks washed (greyed) out. If I select fullscreen the colour comes back fully. The actual video is https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_30mb.mp4 if that helps.
In RStudio I also get the message:
Listening on http://127.0.0.1:7109
Warning: Error in value[[3L]]: Couldn't normalize path in addResourcePath, with arguments: prefix = 'videos'; directoryPath = 'C:/Downloads'
55: stop
54: value[[3L]]
53: tryCatchOne
52: tryCatchList
51: tryCatch
50: addResourcePath
49: server [#2]
Error in value[3L] :
Couldn't normalize path in addResourcePath, with arguments: prefix = 'videos'; directoryPath = 'C:/Downloads'
but apart from the colour it works.
You can add static resources to Shiny's web server via addResourcePath.
Please try the following:
library(shiny)
ui <- fluidPage(
tags$video(src = "videos/big_buck_bunny_720p_30mb.mp4", type = "video/mp4", autoplay = TRUE, controls = TRUE)
)
server <- function(input, output, session) {
addResourcePath(prefix = "videos", directoryPath = "C:/Downloads")
}
shinyApp(ui, server)
However, using the www subdirectory should also be fine. The prefix for the www folder is "/". Please see this.
Related
I am trying to get an image to display in my shiny app based on a user selection by clicking points on a map that display the photos’ location. The app works fine and displays the photo when I use the app locally via Rstudio but the photo does not display when I deploy the app to shinyapp.io. I believe the problem is that the fpath is not annotated correctly to reach the image but I am not sure what the path should read to reach the image when deployed to shinyapp.io.
I have tried a number of solutions including:
As per think link, I saved copies of the images to both the base folder that holds the app file and the www folder thinking that shiny might automatically look in the www folder since that is where images are normally sourced for displaying in the app but that did not work as paste0(dat$PhotoID,".jpg") or paste0(“www”,dat$PhotoID,".jpg") or paste0(“/www”,dat$PhotoID,".jpg") or paste0(“/www/”,dat$PhotoID,".jpg"). When I run the app locally I can access the image using paste0("./",dat$PhotoID,".jpg") or paste0(dat$PhotoID,".jpg")
I tried to link to a “resource folder” but could not find a function for this call nor do I have an Rmd file as per this link.
This link looked useful but altering if a backslash occurred above did not affect the outcome. I also am unfamiliar with relative vs absolute file paths although for apps deployed on shinyapp.io I would expect relative file paths would be the most appropriate.
I have previously used a static image in my app accessed using img(src = "FigureCombo.png") but since I am using an imageOutput() I don’t think that is appropriate.
Does anyone have any advice on how to access stored images when app is deployed to shinyapp.io?
Thanks
Here is the relevant code:
`
ui <- fluidPage(
mainPanel(
imageOutput("myImage", height = "200px"))
)
server <- function(input, output, session) {
output$myImage <- renderImage({
photo <- selected_photo()
if(is.null(photo)) {
return(NULL)
}
else {
list(src = photo,
contentType = 'image/jpg',
width = 300,
height = 400,
style = 'border-style: solid; border-color: red;',
alt = "No point selected yet or that point does not have an image...")
}
}, deleteFile = FALSE)
selected_photo <- reactive({
dat <- reactive_data()
photoid <- input$wsmap_marker_click$id
dat <- dat[dat$PhotoID %in% photoid,]
fpath <- paste0(dat$PhotoID,".jpg") # works when local but not shinyapp.io
return(fpath)
})
}
shinyApp(ui = ui, server = server)
`
To get an idea of the directory structure on an unfamiliar host, you might list the subdirectories of the work directory (as seen by server.R) and log it to your user interface like so:
ui <- fluidPage(
## ...
verbatimTextOutput('log')
## ...
)
server <- function(input, output) {
## ...
output$log <- renderPrint(list.dirs(getwd()))
## ...
}
I wrote a shiny app that includes generating an rmd file and then rendering it into html report.
As shown in the simple example below, there is a variable created inside the server function of shinyapp, then the created rmd file should have access to this variable while rendering into html.
according to other posts and articles, it seems that we have to copy the rmd file into a temporary folder to work and that's what I tried to do below.
the app is not working.
when I try locally, I get this error:
Quitting from lines 8-9 (x3.Rmd)
Warning: Error in print: object 'xz' not found
so it seems that shiny was able to find the generated rmd, started rendering it but did not have access to the xz variable generated in shiny. I did some reading, and it seems to be related to the render environment (ie it renders in a new session) but I do not know how to fix that. (in the real app I am working on, the render process should have access to a dataframe not just a string variable, but I believe the concept is the same for illustration purpose).
when I tested on shinyapps.io, it says Failed-server problem when I click on the download button. Surprisingly, there is nothing in the application log, it says currently no logs.
when I test the original app I am writing (not this simple example), I get this error in shinyapp.io logs:
2022-04-10T18:01:45.357461+00:00 shinyapps[6055802]: Warning in normalizePath(path, winslash = winslash, mustWork = mustWork) :
2022-04-10T18:01:45.357710+00:00 shinyapps[6055802]: [No stack trace available]
2022-04-10T18:01:45.357627+00:00 shinyapps[6055802]: Warning: Error in abs_path: The file '/tmp/Rtmp27RVU8/x3.Rmd' does not exist.
2022-04-10T18:01:45.357543+00:00 shinyapps[6055802]: path[1]="/tmp/Rtmp27RVU8/x3.Rmd": No such file or directory
My goal is to make this work from shinyapp.io. Any suggestions on where to go from there, I believe there are two issues:
1- make sure the rmd render will have access to the variables generated in shiny
2- save the rmd file in a place that is "convenient" for shiny to find and render.
library(shiny)
ui <- fluidPage(
downloadButton("report", "Download sample report.")
)
server <- function(input, output, session) {
#create content and export it into rmd
observe({
xz= "hello there"
x3 = '---
title: sample
output: html_document
---
``` {r}
print(xz)
```
';write(x3, file="x3.rmd", append = FALSE)
})
#Render the report and pass it to download handler
output$report <- downloadHandler(
filename = "sample.html",
content = function(file) {
tempReport <- file.path(tempdir(), "x3.Rmd")
file.copy("x3.Rmd", tempReport, overwrite = TRUE)
output <- rmarkdown::render(
input = tempReport
)
file.copy(output, file)
})
}
shinyApp(ui, server)
These are the resources I used (but still unable to make it work). All deal with parameterized reports of a user uploaded .rmd, which won't work in my case:
https://shiny.rstudio.com/articles/generating-reports.html
https://community.rstudio.com/t/where-do-i-save-the-rmd-file-i-am-generating-with-a-shiny-r-app/65987/3
https://community.rstudio.com/t/generating-downloadable-reports/39701
https://mastering-shiny.org/action-transfer.html#downloading-reports
I have done this with Shiny App but on a shiny server (in my work place) and can share only the approach here. Currently the access to exact code will take time but this shall give you idea. No idea how it works with shinyapps.io but will try that sometime.
Within UI of shiny provide for a "Generate Report" Button.
In server, with
observeEvent(input$btn_Id,{
#Code to render the html in rmarkdown and then also `downloadHandler()`
})
Please check this also :
Use the downloandHanlder() referring this documentation.
This one gives idea to download data
Solution to passing variables is not special. Just ensure data is already present when you are calling render()
like this:
rmarkdown::render(input = "D:/YourWorkingDirectly/Letters.rmd")
If this doesn't help you , please let me know and will delete the answer.
I am trying to render a local image file onto the Shiny app interface using a snippet of the example code provided by Shiny website below:
ui <- fluidPage(
imageOutput("plot3")
)
server <- function(input, output, session) {
# Send a pre-rendered image, and don't delete the image after sending it
# NOTE: For this example to work, it would require files in a subdirectory
# named images/
output$plot3 <- renderImage({
filename <- normalizePath(file.path('./images',
paste('image', '2', '.jpeg', sep='')))
# Return a list containing the filename
list(src = filename, alt = "Alternate text")
}, deleteFile = FALSE)
}
shinyApp(ui, server)
It didn't work when I ran the application. However when I added back the interactive() function that was originally in the example code, encase the main code body, I was able to display the local image file without any problems.
if (interactive()) {
ui <- fluidPage(
imageOutput("plot3")
)
server <- function(input, output, session) {...
}
This puzzles me as many of Shiny's tutorials and demonstrations showed rendering can be done without encasing the code body in a scope.
Is there anyone who encounter a similar scenario such as this? Rendering a local image file into a Shiny app?
I just discovered what was wrong with the code through trial and error. The directory which the shiny code app is saved in a different location from the location where the image file is saved. So the solution is either to place a setwd([www image folder location]) or relocate image folder www to where the shiny app resides which it worked - the image is successfully rendered.
I am working on a project, trying to render audio dynamically. That is, I can click a button and play a selected local audio.
I have read this post and tried zedii's trick. The problem remains still.
Well I build a test app as shown below.
library(shiny)
# test set ----
ui <- fluidPage(
textInput('my_music','path:',value="questionF"),
actionButton("ok", "Okay"),
uiOutput('my_audio')
# tags$audio(src = "questionF.mp3", type = "audio/mp3")
)
get_audio_tag <- function(filename) {
tags$audio(src = filename,
type = "audio/mp3",
controls = "controls")
}
server <- function(input, output, session){
# Render the audio player
observeEvent(input$ok, {
wav_name = input$my_music
# output$my_audio <-renderUI(get_audio_tag("questionF.mp3"))
output$my_audio <-renderUI(get_audio_tag(wav_name))
})
}
shinyApp(ui = ui, server = server)
When I click the button, the first song turns out okay. But the following ones seemed pretty hard to load, for my computer would freeze with the memory used by Rstudio rising.
Any thoughts will be appreciated.
updates:
I tried on different browsers. These codes would failed on Chrome but works fine on Microsoft edges.
Looks like a caching problem.
So now my question is how can I make the codes work on every platform using shiny/R?
My codes works on most browser other than Chrome. I think it's more like a Chrome's problem.
I want to play an mp4 video in a shiny output. It plays fine on my computer when I double click on the file name. I copied the file to the www folder below the main app folder. I have tried variations on the following code.
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(debug = TRUE),
mainPanel(
"video test",
uiOutput('ozoneVideo'), id = "mainPanel")
)
server <- function(input, output, session) {
output$ozoneVideo <- renderUI({
h6("ozone video", tags$video(src = "MesaCountyOzone2017_07_10.mp4", type = "video/mp4", width = "1080px", height = "480px", controls = "controls", autoplay = NA))
})
}
shinyApp(ui = ui, server = server)
The web page that is generated shows the text elements (video test, ozone video) and horizontal bar with a video play button and the word "Error" with no explanation. No error appears in the javascript console.
When I add www/ to the filename, the javascript console has an error message "Failed to load resource: the server responded with a status of 404 (Not Found) and a url of http://127.0.0.1:4817/www/MesaCountyOzone2017_07_10.mp4
Any help clarifying this greatly appreciated!
Update: July 11, 2017.
I am now pretty sure this is a problem with Safari. I run the app with run external chosen. The web page that opens up in Safari has an error message in the video control section. If I copy the url and past to a Chrome web page, the video plays fine.
So my question is now what do I do to make this play in Safari???