When I attempt to connect to any shiny application on my webserver I receive the following error:
ERROR: cannot open the connection
I am currently storing the application within the /srv/shiny-server folder on the server and the folder does currently have the correct read/write permissions. Earlier when I uploaded my application it ran without issue but I made several changes and when I updated the files I suddenly started getting this error. I tried rolling back all of the changes but the error persisted and so eventually I attempted uploading an example application from the Shiny Website and that also gets the same error.
Here is the code for the sample application I'm currently trying to get working but I do not think that it is the issue:
ui.R
library(shiny)
bootstrapPage(
selectInput(inputId = "n_breaks",
label = "Number of bins in histogram (approximate):",
choices = c(10, 20, 35, 50),
selected = 20),
checkboxInput(inputId = "individual_obs",
label = strong("Show individual observations"),
value = FALSE),
checkboxInput(inputId = "density",
label = strong("Show density estimate"),
value = FALSE),
plotOutput(outputId = "main_plot", height = "300px"),
# Display this only if the density is shown
conditionalPanel(condition = "input.density == true",
sliderInput(inputId = "bw_adjust",
label = "Bandwidth adjustment:",
min = 0.2, max = 2, value = 1, step = 0.2))
)
server.R
library(shiny)
function(input, output) {
output$main_plot <- renderPlot({
hist(faithful$eruptions,
probability = TRUE,
breaks = as.numeric(input$n_breaks),
xlab = "Duration (minutes)",
main = "Geyser eruption duration")
if (input$individual_obs) {
rug(faithful$eruptions)
}
if (input$density) {
dens <- density(faithful$eruptions,
adjust = input$bw_adjust)
lines(dens, col = "blue")
}
})
}
I opened up the logs for the application located in /var/log/shiny-server and it turned out that permission was being denied to the folder. After googling the problem I found this question which helped me solve the issue
Related
I've seen many people with issues like mine, but no answers have been helpful for my case.
I am using seurat for single cell data analysis and I'm trying to build a very simple web page for other individuals to query their gene expression.
I get a "subscript out of bounds" error while trying to run my Shiny app immediately on launch, despite it working in R.
In the console, it's immediately displaying -- Warning: Error in [[: subscript out of bounds. What am I doing wrong here?
library(shiny)
library(Seurat)
WholeEye <- readRDS("WholeEye.rds")
MG <- readRDS("MG.rds")
CMZ <- readRDS("CMZ.rds")
RPE <- readRDS("CMZ.rds")
ui <- fluidPage(
headerPanel('McFarlane scRNAseq'),
sidebarPanel(
textInput(inputId = 'celltype', label = "Choose a cell type. Eg. WholeEye, MG, RPE, CMZ", value = "MG", width = NULL, placeholder = NULL),
textInput(inputId = 'gene', label = "Choose a gene", value = "", width = NULL, placeholder = NULL)
),
mainPanel(
plotOutput(outputId = "FeaturePlot" )
)
)
server <- function(input, output) {
output$FeaturePlot <- renderPlot({
FeaturePlot(object = input$celltype, reduction = "umap", label = TRUE, min.cutoff = 0, features = input$gene)
})
}
shinyApp(ui = ui, server = server)
Try
output$FeaturePlot <- renderPlot({
req(input$celltype,input$gene)
FeaturePlot(object = get(input$celltype), reduction = "umap", label = TRUE, min.cutoff = 0, features = get(input$gene))
})
When I deploy an app using Shiny io, the URL (e.g., 'https://account.shinyapps.io/AppName/') does not work on other devices (sometimes the browser shows the error 'browser cannot connect to the server'). However, if the app is opened in shiny apps io dashboard then the full URL is copied from the address bar, the resulting URL will work on other devices (e.g., 'https://account.shinyapps.io/AppName/?_ga=2.202210006.61460828.1641729163-631729455.1649829163').
What would cause this?
Example of code used to create app:
library(shiny)
library(leaflet)
ui <- fluidPage(
# Application title
titlePanel("Radius"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
h4("Origin"),
numericInput(inputId = "originLat", label = "Latitude", value = 46.0),
numericInput(inputId = "originLong", label = "Longitude", value = -112.5),
h4("Radius"),
sliderInput(inputId = "RadiusMeters", label = "Meters", min = 0, max = 8000, value = 400, step = 400),
sliderInput(inputId = "RadiusMiles", label = "Miles", min = 0.00, max = 5, value = 400/1600, step = 0.25),
selectInput("View",
h4("Layer"),
choices = list("Aerial View" = 'Esri.WorldImagery',
"Topography" = 'OpenTopoMap',
"Street View" = 'OpenStreetMap.Mapnik'),
selected = 'OpenStreetMap.Mapnik'),
),
# Show a plot of the generated distribution
mainPanel(
leafletOutput("Area"),
)))
server <- function(input, output, session) {
observeEvent(input$RadiusMeters, {
updateSliderInput(session = session, inputId = "RadiusMiles", value = input$RadiusMeters / 1600)
})
observeEvent(input$RadiusMiles, {
updateSliderInput(session = session, inputId = "RadiusMeters", value = input$RadiusMiles * 1600)
})
output$Area <- renderLeaflet({
leaflet() %>%
addProviderTiles(input$View) %>%
setView(lng = (input$originLong), lat =(input$originLat), zoom = 13) %>%
addMarkers(lng=(input$originLong), lat=(input$originLat)) %>%
addCircles(lng=(input$originLong), lat=(input$originLat), radius = input$RadiusMeters)
})
session$onSessionEnded(function() {
stopApp()
})
}
UPDATE;
I think what was happening was some sort of glitch. I published essentially the same thing as a new app and it works. I will leave this up for the time being in case some one has an explanation for this.
I'm trying to deploye my shiny app on shinyapps.io and I get this message :
"An error has occurred
The application failed to start (exited with code 1)."
I tried to commit setwd line and other stuff but yet I didn't find solution.
The issue might be a wrong file path ? Should I put the "read.csv" line into my server or ui function ?
Here is my code :
#setwd(dir = "/media/miles/MILES/Projets & Cours/Master_1/Semestre 2/lardjane/Shiny_app/Projet Shiny")
matches <- read.csv('./matches.csv', stringsAsFactors=FALSE, sep=",", header=TRUE)
matches <- matches[,c(3,6)]
#summary(matches)
matches$platformid <- as.factor(matches$platformid)
#levels(matches$platformid)
#install.packages('shiny')
library(shiny)
#install.packages('rsconnect')
library(rsconnect)
ui <- shinyUI(fluidPage(
# Give the page a title
titlePanel("Game time by server"),
# Generate a row with a sidebar
sidebarLayout(
# Define the sidebar with one input
sidebarPanel(
selectInput("region", "Server:",
choices=levels(matches$platformid)),
hr(),
selectInput(inputId = "n_breaks",
label = "Number of bins in histogram (approximate):",
choices = c(10, 20, 35, 50),
selected = 20),
hr(),
checkboxInput(inputId = "individual_obs",
label = strong("Show individual observations"),
value = FALSE),
checkboxInput(inputId = "density",
label = strong("Show density estimate"),
value = FALSE),
conditionalPanel(condition = "input.density == true",
sliderInput(inputId = "bw_adjust",
label = "Bandwidth adjustment:",
min = 0.2, max = 2, value = 1, step = 0.2)),
hr(),
helpText("Data from Kaggle (2014-2018) League of Legends Ranked Matches.")
),
# Create a spot for the barplot
mainPanel(
plotOutput("timePlot")
)
)
)
)
server <- function(input, output) {
# Fill in the spot we created for a plot
output$timePlot <- renderPlot({
# Render a histogramme
hist(matches[matches$platformid==input$region,2],
probability = TRUE,
breaks = as.numeric(input$n_breaks),
main = "Game Time",
ylab="",
xlab="Duration (seconds)")
if (input$individual_obs) {
rug(matches[matches$platformid==input$region,2])
}
if (input$density) {
dens <- density(matches[matches$platformid==input$region,2],
adjust = input$bw_adjust)
lines(dens, col = "blue")
}
})
}
shinyApp(ui = ui, server = server)
I would like to add one last request. I would like to display R code just below the plot. That can anyone can get access to both (app result and R code). Is that possible ?
Thank you in advance.
swd is not the way to solve this because of how the environments in Shiny (and R in general) work. When you launch Shiny you actually don't know what physical server your Shiny server is running on. So you need to use a generic solution.
Try this:
matches <- read.csv('./matches.csv',
stringsAsFactors=FALSE, sep=",", header=TRUE)
Per https://docs.rstudio.com/shinyapps.io/Storage.html, if the csv file is in the same location as the app, try :
matches <- read.csv('matches.csv', stringsAsFactors=FALSE, sep=",", header=TRUE)
However, I don't think this is your issue; I think the issue is in your rendering of the plot. You use the input$region to generate your histogram, but you don't provide a default value, so it starts as NULL, which causes an issue when you try to construct your histogram. You have 2 options to solve this.
Option 1 is to set a default value for input$region with:
selectInput("region", "Server:",
choices=levels(matches$platformid),
selected = levels(matches$platformid)[1]),
Option 2 is to use req() so that the histogram will not run if any of its required values are not truthy:
server <- function(input, output) {
# Fill in the spot we created for a plot
output$timePlot <- renderPlot({
req(input$region, input$n_breaks)
# Render a histogramme
hist(matches[matches$platformid==input$region,2],
probability = TRUE,
breaks = as.numeric(input$n_breaks),
main = "Game Time",
ylab="",
xlab="Duration (seconds)")
if (input$individual_obs) {
rug(matches[matches$platformid==input$region,2])
}
if (input$density) {
dens <- density(matches[matches$platformid==input$region,2],
adjust = input$bw_adjust)
lines(dens, col = "blue")
}
})
}
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")))
BACKGOUND:
You can "ask" RStudio to generate an example R Markdown Shiny document, which contains this sample code:
## Inputs and Outputs
You can embed Shiny inputs and outputs in your document.
Outputs are automatically updated whenever inputs
change. This demonstrates how a standard R plot can be
made interactive by wrapping it in the Shiny
`renderPlot` function. The `selectInput` and
`sliderInput` functions create the input widgets used to
drive the plot.
```{r, echo=FALSE}
inputPanel(
selectInput("n_breaks", label = "Number of bins:",
choices = c(10, 20, 35, 50), selected = 20),
sliderInput("bw_adjust", label = "Bandwidth adjustment:",
min = 0.2, max = 2, value = 1, step = 0.2)
)
renderPlot({
hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks),
xlab = "Duration (minutes)", main = "Geyser eruption duration")
dens <- density(faithful$eruptions, adjust = input$bw_adjust)
lines(dens, col = "blue")
})
```
Note that this example does not make use of a folder containing ui.R and server.R.
PROBLEM:
If you copy this multiple times, the first one works as expected, and the later ones get displayed as well, but do not react to changes in the input parameters.
QUESTION:
How can you create an R Markdown document with multiple embedded plots like the above (without using external folders with ui.R and server.R), but ensuring that each one works interactively?
You must give different ids to your input elements, something like that :
First embedded shiny plot :
```{r}
inputPanel(
selectInput("n_breaks", label = "Number of bins:",
choices = c(10, 20, 35, 50), selected = 20),
sliderInput("bw_adjust", label = "Bandwidth adjustment:",
min = 0.2, max = 2, value = 1, step = 0.2)
)
renderPlot({
hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks),
xlab = "Duration (minutes)", main = "Geyser eruption duration")
dens <- density(faithful$eruptions, adjust = input$bw_adjust)
lines(dens, col = "blue")
})
```
Second embedded shiny plot :
```{r}
inputPanel(
selectInput("n_breaks2", label = "Number of bins:",
choices = c(10, 20, 35, 50), selected = 20),
sliderInput("bw_adjust2", label = "Bandwidth adjustment:",
min = 0.2, max = 2, value = 1, step = 0.2)
)
renderPlot({
hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks2),
xlab = "Duration (minutes)", main = "Geyser eruption duration")
dens <- density(faithful$eruptions, adjust = input$bw_adjust2)
lines(dens, col = "blue")
})
```
As described for example in the RStudio Shiny Tutorial, the first parameter to widget functions is the widget name, which identifies the widget. Multiple widgets with the same name will not each be usable, which is why simply creating two copies of the example does not create two working copies.
To make it work, you must make the widget names unique in each inputPanel call, and then use this names in the renderPlot calls.