404 not found when saving base R plot in Shiny - r

I'm making a Shiny app that involves making plots from a function from a bioinformatics package that uses base R graphics. I'm building the Shiny app in RStudio Server. Since the plot can only be made with that function in that package, using ggplot2 or highcharter instead is not an option. I have no problem with saving plots made by ggplot2, but got into trouble when trying to save plots made with base R graphics. I used the answer in Downloadhander (save plot) for basic plot in shiny, but when I clicked the download button, I got "404 not found" and download was not initiated, even though that plot displayed properly within the Shiny app. Here's a modified version of RStudio's default Faithful Geyser app that can reproduce this problem:
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30),
downloadButton("download", "Download plot")
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
p <- reactive({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
output$distPlot <- renderPlot(p())
output$download <- downloadHandler("foo.png",
content = function(file) {
png(file = file)
p()
dev.off()
})
}
# Run the application
shinyApp(ui = ui, server = server)
It seems that the problem is related to
png(file = file)
p()
dev.off()
not working within Shiny, since when I tried to save ggplot this way, it also gave "404 not found", while ggsave worked fine to download ggplot (though not base R plots) inside the Shiny app. Outside Shiny, the base R way to save plots works properly.

Changing p() from a reactive to a standard function solved the issue for me.
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30),
downloadButton("download", "Download plot")
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
p <- function() {
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
}
output$distPlot <- renderPlot(p())
output$download <- downloadHandler(filename = "foo.png",
content = function(file) {
png(filename = file)
p()
dev.off()
},
contentType = "image/png")
}
# Run the application
shinyApp(ui = ui, server = server)

Related

image titles do not render properly

I am trying to run the example shiny app (I added the options(encoding = 'UTF-8')):
options(encoding = 'UTF-8')
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 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)
and the image has not encoded the names (titles).
UPDATE
I tried to use par(family ="Ubuntu Mono") in the R script and it works. But, it doesn't work in the shiny app (tried it inside render plot).
This looks like a font conflict.
You could circumvent it by setting a custom font available on your system that does get rendered correctly.
For base graphics, use par(family = "serif") or
renderPlot(..., family = "serif") in Shiny.
For ggplot2 graphics, set theme(text = element_text(family = "serif"))
for an individual plot, or use base_family in supported themes, e.g.
theme_gray(base_family = "serif").
In all of the above, replace "serif"
with the font you want to use. For example, you specifically mentioned
"Ubuntu Mono" rendering correctly in your case.

Increasing margins for base plot in RShiny (arguments to par() are not respected)

I am having issues with a Shiny App such that the text labels are being cut off. If I were performing this plotting in R, I would adjust the margins of the plotting area using par(mar = c(10,10,10,10)), or other adjustments.
However, when I attempt to adjust the margins using par() within a shiny app no change takes effect. Here is a sample shiny app where I attempt to adjust the margins without success:
server.R
library(shiny)
shinyServer(function(input, output) {
output$distPlot <- renderPlot({
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
par(mar = c(10,10,10,10))
hist(x, breaks = bins, col = 'darkgray', border = 'white', main = "Obsequious people are usually not being genuine; they resort to flattery and other fawning ways to stay in the good graces of authority figures. An obsequious person can be called a bootlicker, a brownnoser or a toady.")
})
})
ui.R
library(shiny)
# Define UI for application that draws a histogram
shinyUI(fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
))
Is it possible to use par() to define a plotting area for a shiny app?

Read R code from a file (i.e. source) in a shiny App

I want to modularise my shiny app code, by moving parts of the code in separate files. I then include the content of the file with a call to the source function: source("./www/some_code.R", local = TRUE)
It works well except for an undesired effect: the word TRUE is added just below the insert.
Could you help me understand why this happen and how I can remove this undesired text?
For a reproducible example,
create app.R:
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
source("./www/slider.R", local = TRUE)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 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)
and in the www folder the slider.R:
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
Just when I posted this, I saw a link to this which answer the question: displaying TRUE when shiny files are split into different folders
I thought that I did my research though... Should I delete the whole thread?

Make Shinyapp use new input across sessions

I´m using a shinyapp on an opensource shiny-server to display a dashboard on multiple devices. I want to give the opportunity to change the plots on all dashboards from a local PC.
If the input is altered in any session, all sessions should update their plots to this new input. How do i do this? Can i save the input in a global variable?
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
observe({
invalidateLater(10000)
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
p<<- hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
output$distPlot <- renderPlot({print(p)})
}
# Run the application
shinyApp(ui = ui, server = server)
You can initiate an reactiveValue in the global file and use this instead up the original input
In principal all objects initiated in the global file are shared over sessions.
Example
**UI **
library(shiny)
# Define UI for application that plots random distributions
shinyUI(pageWithSidebar(
# Application title
headerPanel("It's Alive!"),
# Sidebar with a slider input for number of observations
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot", height=250)
)
))
**Server **
library(shiny)
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
# Expression that generates a histogram. 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
observe({
RV$bins = input$bins
})
output$distPlot <- renderPlot({
x <- faithful[, 2] # Old Faithful Geyser data
bins <- seq(min(x), max(x), length.out = RV$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
})
** Global **
RV <- reactiveValues(bins = 10)
When a user changes the number of bins the histogram will changes for all users but not the slider.
Hope this helps!!

Error in y axis label in shiny app using cairo

so I wrote a shiny app for importing data and plotting it using ggplot2. If I use cairo for plotting
options(shiny.usecairo=T)
(should be active by default) plots are prettier but y axis label "Oxygen consumption" miss some parts.
Plot without cairo:
Plot with cairo:
Can someone point me towards a solution or have ideas what the mistake is?
Edit: here is a minimal example:
library(shiny)
library(ggplot2)
options(shiny.usecairo=T)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30),
sliderInput("size",
"size:",
min = 0.1,
max = 40,
value = 15)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
res<- reactive({input$resolution})
output$distPlot <- renderPlot({
#refresh on
print(input$resolution)
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
qplot(x, breaks = bins, col = 'darkgray', ylab = "Some long string to test for errors and see what is affected")+theme_classic(base_size=input$size)
})
}
# Run the application
shinyApp(ui = ui, server = server)
So, for me it looks like some characters get cut on the right.
Edit: I narrowed this down to my windows machine, can't see it on shinyapps.io and local linux server anymore. Probably not so relevant then, since the error is only happening during development.

Resources