Change size of RStudio Window on start of Shiny app - r

I'm using the RStudio IDE to develop shiny apps. When starting an app I usually use the RunApp Button: Run in Window. This opens the app in a window with specific width and height.
Is there a way to change the width of this window, so every time I start the app will be shown in a wider window?

You can try the runGadget option:
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")
})
}
# Run in a dialog within R Studio
runGadget(ui, server, viewer = dialogViewer("Dialog Title", width = 1200, height = 600))
# Run in Viewer pane
runGadget(ui, server, viewer = paneViewer(minHeight = 500))
# Run in browser
runGadget(ui, server, viewer = browserViewer(browser = getOption("browser")))

Related

Audio files not loading properly in Shiny App

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)

Two boxes in splitlayout in R shiny app are misaligned. How do I fix this?

I have two numericInput boxes, allowing for the input of the min and max (range) of a numeric variable. I have tried using splitLayout, which works but the boxes are misaligned when I include a label for the boxes.
The code is below
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(
#fluidRow(
splitLayout(
variable <- faithful$waiting,
numericInput(paste("Min"),
#round = TRUE,
label = h5(c("test")),
min = round(min(variable, na.rm=TRUE)),
max = round(max(variable, na.rm=TRUE))-1,
value = round(min(variable, na.rm=TRUE))
), ## end slider input
numericInput(paste("Min"),
#round = TRUE,
label = h5(""),
min = round(min(variable, na.rm=TRUE))+1,
max = round(max(variable, na.rm=TRUE)),
value = round(max(variable, na.rm=TRUE))
)
)
)
))
# 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)
If you run the code, you will see that the boxes are misaligned.
How can I fix this?
The issue is that you have a label for one box, which pushes it down, with no equivalent label for the other box. To push the second box down, you need to include something that will read as label without showing anything. I used an HTML break:
numericInput(paste("Min"),
#round = TRUE,
label = h5(HTML("<br/>")),
min = round(min(variable, na.rm=TRUE))+1,
max = round(max(variable, na.rm=TRUE)),
value = round(max(variable, na.rm=TRUE))

Is there a way to erase contents of the bsModal() window when Close button is pushed?

I am using bsModal() window from the ShinyBS package in one of my Shiny Apps. Whenever the user opens the modal window for the second time, the contents of the window from the first opening remain on the screen until the new content loads. Which is not ideal. Is there a way to delete the contents of the bsModal() window once it is closed? I do not want the older content to appear in the window once it is reopened. Subsequently, maybe there is a way to clean up the bsModal() window before opening?
I wasn't able to find any solution for this
Here is some example (not my) code of an app that can be also accessed through:
library(shinyBS)
bsExample("Modals")
ui.R
library(shiny)
library(shinyBS)
fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30),
actionButton("tabBut", "View Table")
),
mainPanel(
plotOutput("distPlot"),
bsModal("modalExample", "Data Table", "tabBut", size = "large",
dataTableOutput("distTable"))
)
)
)
server.R
library(shiny)
library(shinyBS)
shinyServer(
function(input, output, session) {
output$distPlot <- renderPlot({
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$distTable <- renderDataTable({
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
tab <- hist(x, breaks = bins, plot = FALSE)
tab$breaks <- sapply(seq(length(tab$breaks) - 1), function(i) {
paste0(signif(tab$breaks[i], 3), "-", signif(tab$breaks[i+1], 3))
})
tab <- as.data.frame(do.call(cbind, tab))
colnames(tab) <- c("Bins", "Counts", "Density")
return(tab[, 1:3])
}, options = list(pageLength=10))
}
)

Shiny - bsModal all grayed out with shinythemes

I use the library shinythemes pretty extensively in apps that I build. I was trying to leverage a bsModal from the shinyBS package and noticed that the 'fade in' div never went away leaving me with an unusable web app since nothing was clickable.
The examples from shinyBS::bsModal all work fine (they are sans-shinythemes). How can I continue to use themes while also using modals?
Example App:
library(shiny)
library(shinyBS)
library(shinythemes)
app = shinyApp(
ui =
navbarPage(title=NULL,
id="navbar",
theme = shinytheme("journal"),
tabPanel("test",
column(1),
column(3,
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30),
actionButton("tabBut", "View Table")
),
column(7,
plotOutput("distPlot"),
bsModal("modalExample", "Data Table", "tabBut", size = "large",
dataTableOutput("distTable"))
)
)
),
server =
function(input, output, session) {
output$distPlot <- renderPlot({
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$distTable <- renderDataTable({
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
tab <- hist(x, breaks = bins, plot = FALSE)
tab$breaks <- sapply(seq(length(tab$breaks) - 1), function(i) {
paste0(signif(tab$breaks[i], 3), "-", signif(tab$breaks[i+1], 3))
})
tab <- as.data.frame(do.call(cbind, tab))
colnames(tab) <- c("Bins", "Counts", "Density")
return(tab[, 1:3])
}, options = list(pageLength=10))
}
)
runApp(app)
I don't know what causes the conflict, but the solution is to specify the link to the theme directly. Replace theme = shinytheme("journal") with theme = "http://bootswatch.com/journal/bootstrap.css" adjusting the name for the theme you're using.

Shiny R Moving Sidebar

I have a side bar as follows
sidebarPanel(
wellPanel(
list(tags$head(tags$style("body {background-color: #E0F2F7; }"))),
helpText("Choose a stock ticker to examine, For example
^HSI - Hang Seng,
^N225 - Nikkei 225 and
^FTSE - FTSE 100.
Information will be collected from",tags$a(href="https://uk.finance.yahoo.com/lookup", "yahoo finance"),"."),
textInput("symb", "Symbol", "^FTSE"),
bsAlert(inputId = "alert_anchor"),
dateRangeInput("dates",
"Date range",
start = "2015-01-01",
end = as.character(Sys.Date())),
textOutput("DateRange"),
div(style="display:inline-block",submitButton("Analysis")),
div(style="display:inline-block",downloadButton('downloadData', 'Download Data')),width=6
))
Which gives the following (side bar is on the left)
However I wanted it so that the sidebar follows the page as the user scrolls up and down instead of there being a blank blue space on the left as shown.
Is this possible to do on R Shiny? If so, how can it be done?
You can add a style argument to sidebarPanel, which sets the element position to fixed. Below is the Shiny default minimal example with minor modifications to illustrate the behavior.
library(shiny)
ui <- fluidPage(
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(
style = "position:fixed; width:33%;", # Add this line to your code!
sliderInput(inputId = "bins",
label = "Number of bins:",
min = 1,
max = 50,
value = 30)
),
mainPanel(
lapply(1:10, function(i) {
plotOutput(outputId = paste("distPlot", i, sep = "_"))
})
)
)
)
server <- function(input, output) {
lapply(1:10, function(i) {
output[[paste("distPlot", i, sep = "_")]] <- renderPlot({
set.seed(i)
x <- rnorm(n = 1000)
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)

Resources