Audio files not loading properly in Shiny App - r

I am trying to load an mp3 file held in the www folder in my shiny app. After having lots of issues with this, I have just reproduced the problem in a very simple shiny App (code below).
The audio file I am using, "brand.mp3" is a 19MB mp3 file
When I run this, it returns a 500 error. What is really weird is, if I close RStudio, and then restart, the first time I run the app, the file loads. Then if I reload, there is nothing at all. The first image shows first load of app:
And on the the reload I get this:
The Chrome console then gives an error: Failed to load resource: the server responded with a status of 500 (Internal Server Error)
Code for this is below.
Would be grateful for any ideas on this one. The only thing I can think of is if the app has some kind of memory issue. I have the same problem if I running RStudio in Windows or if I am running the app through a Docker Shiny Server Image found here
library(shiny)
ui <- fluidPage(
# App title ----
titlePanel("Test app"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Slider for the number of bins ----
sliderInput(inputId = "bins",
label = "Number of bins:",
min = 1,
max = 50,
value = 30),
tags$audio(src = "brand.mp3", type = "audio/mp3", autoplay = NA, controls = NA)
),
mainPanel(
plotOutput(outputId = "distPlot")
)
)
)
server <- function(input, output) {
output$distPlot <- renderPlot({
x <- faithful$waiting
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = "#75AADB", border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times")
})
}
shinyApp(ui = ui, server = server)

Related

Upload Shiny Logfiles to WebDAV Server

We are running several shiny apps for educational purposes on shinyapps.io. In order to track the usage we tried the shinylogs package locally, which worked out fine. Now the goal is to upload the logfiles created by shinylogs on shinyapps.io to a WebDAV server.
The file upload itself can be achieved with the following code. Note, that I have not revealed my true credentials for security reasons. So this request won't actually work for you.
username <- "xxx"
password <- "yyy"
file <- upload_file("test.txt")
PUT("https://fernuni-hagen.sciebo.de/public.php/webdav/test.txt", authenticate(username, password), body = file)
As the next step I created a function out of it, which also works fine.
upload <- function(filename){
body <- upload_file(filename)
PUT(paste0("https://fernuni-hagen.sciebo.de/public.php/webdav/", filename), authenticate(username, password), body = body)
}
upload("test.txt")
Finally, I tried to combine this code with the track_usage command of shinylogs. According to the documentation the store_custom mode should be suitable. Not finding any working example on the web, I was unable to figure out the right syntax, though.
Instead of a single file specified upfront, the function should upload any new logfile to the WebDAV server. In order to clarify my requirements, I have created this simple demo app.
library(shiny)
library(shinylogs)
library(httr)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
fluidRow(
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins1",
"Number of bins:",
min = 1,
max = 50,
value = 30)),
mainPanel(
# Show a plot of the generated distribution
plotOutput("distPlot1")
))),
fluidRow(
# Sidebar with a slider input for number of bins (and action button)
sidebarLayout(
sidebarPanel(
sliderInput("bins2",
"Number of bins:",
min = 1,
max = 50,
value = 30),
actionButton("go", "Update Plot")),
mainPanel(
# Show a plot of the generated distribution
plotOutput("distPlot2")
)))
)
# Define server logic required to draw a histogram
server <- function(input, output) {
username <- "xxx"
password <- "yyy"
track_usage(storage_mode = store_custom(FUN = function(logs){
body <- upload_file(logs)
url <- paste0("https://fernuni-hagen.sciebo.de/public.php/webdav/", logs)
PUT(url, authenticate(username, password), body = body)
}))
output$distPlot1 <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins1 + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
output$distPlot2 <- renderPlot({
# generate bins based on input$bins from ui.R (only upon click)
input$go
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = isolate(input$bins2) + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
}
# Run the application
shinyApp(ui = ui, server = server)
When I run this shiny app locally, there is no error message in the R console, but the files will never arrive on the WebDAV server. Any help to fix this issue would be much appreciated.
So this is my own solution.
# Configuration of logging directory
logsdir <- "~/logs"
# Create directory for log files, if not existent
if(!dir.exists(logsdir)) dir.create(logsdir)
# Usage tracking with JSON file being saved on Shiny and WebDAV Servers
track_usage(
storage_mode = store_custom(FUN = function(logs) {
jsondata <- toJSON(logs)
filename <- paste0("shinylogs_", session$token, ".json")
filepath <- paste0(logsdir, "/", filename)
write(jsondata, file = filepath)
body <- upload_file(filepath)
url <- paste0("https://fernuni-hagen.sciebo.de/public.php/webdav/", filename)
PUT(url, authenticate(username, password), body = body)
})
)

ReferenceError: Can't find variable: Pickr when running shinytest::recordTest()

I am currently working on an RShiny App which became pretty big and hence I want to implement automated UI testing. I therefore tried to record my UI tests with recordTest() from the shinytest package. However, when I run shinytest::recordTest(), I get the error message "ReferenceError: Can't find variable: Pickr" (see attached image). I located the problem to be a colorPickr from the shinyWidgets package and it seems like it has something to do with a .js-File in the package, but I have no idea how to solve this problem.
Error Message
When running shinytest::recordTest(), I usually would expect that the app starts in a headless browser and I can record my tests. This works perfectly fine, when I disable the line of code where the colorPickr is defined. With the colorPickr, the above error occurs.
I tried to update my R version (unfortunately we are working with 3.6.0 currently) and updated all packages, which did not help. I also tried to install phantomJS and set my PATH variable to the phantomjs.exe. Did not help either (not sure if I did that correctly tbh).
The package versions I use are: shinytest_1.5.1, shinyWidgets_0.6.2, shiny_1.6.0
The error is reproducable with the following example app:
library(shiny)
library(shinyWidgets)
library(shinytest)
# Define UI for app that draws a histogram ----
ui <- fluidPage(
# App title ----
titlePanel("Hello Shiny!"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Slider for the number of bins ----
sliderInput(inputId = "bins",
label = "Number of bins:",
min = 1,
max = 50,
value = 30),
colorPickr(
inputId = "color",
label = "Pick a color",
selected = "blue" )
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Histogram ----
plotOutput(outputId = "distPlot")
)
)
)
# Define server logic required to draw a histogram ----
server <- function(input, output) {
output$distPlot <- renderPlot({
x <- faithful$waiting
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = "#007bc2", border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times")
})
}
shinyApp(ui, server)
To reproduce the error, run shinytest::recordTest().
Thanks in advance for any help!

shiny-server fails when trying to source an R file

I updated shiny server (open source) and now i cannot source an R file from within the app code. The .R file referenced is just a simple set of functions, all using base R. The file is also within the same dir as the app resides. I even chmod 777 everything to try to get it to run and it still returns the 'error has occurred' on port 3838. Yet it runs just fine locally (within RStudio). shiny-server.conf is unchanged, user runs as shiny, etc. I updated all packages and even uninstalled and reinstalled shiny and shiny-server with no luck. Its literally only failing on trying to source a file.
source('/srv/shiny-server/basicFls.R')
library(shiny)
ui <- fluidPage(
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(
sliderInput(inputId = "bins",
label = "Number of bins:",
min = 1,
max = 50,
value = 30)
),
mainPanel(
plotOutput(outputId = "distPlot")
)
)
)
server <- function(input, output) {
output$distPlot <- renderPlot({
x <- faithful$waiting
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = "#75AADB", border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times")
})
}
shinyApp(ui = ui, server = server)
You need to have the path of the source() function set relative to the working directory of the app. In this case, the app directory should be referenced as ".". So a file called code.R located in the app director is referenced as:
source("./code.R")
If the file is located in a directory called "srv" in the same directory as the app, you reference the file as:
source("./srv/code.R")
So, in the following example, the contents of code.R is below, and the file is stored in a directory called "srv" off the root of the app directory.
# code.R
makePlot <- function(x_name) {
ggplot(mtcars, aes_string(x=x_name, y="disp")) + geom_point()
}
Here's the app code that works for that:
library(shiny)
library(ggplot2)
source('./srv/code.R')
ui <- fluidPage(
titlePanel("Basic App"),
sidebarLayout(
sidebarPanel(
selectInput("xaxis", "X axis:", choices=c("mpg", "qsec"))
),
mainPanel(
plotOutput("plot")
)
)
)
server <- function(input, output) {
output$plot <- renderPlot(
# ggplot(mtcars, aes_string(x=input$xaxis, y="disp")) + geom_point()
makePlot(input$xaxis)
)
}
shinyApp(ui, server=server)

Changing CTRL+P of a web page using R shiny

I am currently looking for a method to change parameters of the print function of a normal web page. In fact, when you press CTRL+P, it opens a little window where you can print a file as PDF, and I am trying to change some informations on it. For example, the title of the first page and the name of the PDF file. If someone have an idea of how to do that in my R shiny code, let me know as soon as possible. Thank you :)
The dialog to print the webpage to a pdf file is a feature of the browser and can not be directly manipulated using shiny. Howeve, the browser uses HTML meta data from the head tag e.g. to print the title. You can add these tags to your shiny webpage:
library(shiny)
ui <- fluidPage(
tags$head(
# Define the title in both the browser tab name and pdf print header
tags$title("My fancy shiny title")
),
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(
sliderInput(
inputId = "bins",
label = "Number of bins:",
min = 1,
max = 50,
value = 30
)
),
mainPanel(
plotOutput(outputId = "distPlot")
)
)
)
server <- server <- function(input, output) {
output$distPlot <- renderPlot({
x <- faithful$waiting
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x,
breaks = bins, col = "#75AADB", border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times"
)
})
}
shinyApp(ui, server)
You might also want to add shiny::tags$style tags e.g. to add #media rules to customize the print layout e.g. to remove the sidebar or buttons.

R Function shinyApp will not run

I absolutely cannot figure out why my shiny app will not run. I'm fairly new to shiny I will mention. I'm running the following code locally, but when I run it, it never completes, just sits there. Any help is GREATLY appreciated. Code is below:
ui <- fluidPage(
# App title ----
titlePanel("Hello World!"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Slider for the number of bins ----
sliderInput(inputId = "bins",
label = "Number of bins:",
min = 5,
max = 50,
value = 30)
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Histogram ----
plotOutput(outputId = "distPlot")
)
)
)
# Define server logic required to draw a histogram ----
server <- function(input, output) {
# Histogram of the Old Faithful Geyser Data ----
# with requested number of bins
# This expression that generates a histogram is wrapped in a call
# to renderPlot to indicate that:
#
# 1. It is "reactive" and therefore should be automatically
# re-executed when inputs (input$bins) change
# 2. Its output type is a plot
output$distPlot <- renderPlot({
x <- faithful$waiting
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = "#75AADB", border = "orange",
xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times")
})
}
shinyApp(ui = ui, server = server)
Result:
It never completes, just sits there.
Ok, so I figured out the answer to this. It was a permission thing with my company. If anyone runs into this issue in the future, be sure to evaluate your level of internet permissions.

Resources