I've created a Shiny app, which contains some Plotly objects. All works fine when previewing in RStudio. Though, when I deploy the app to Shiny server (on-premise) and open the app, the plots are not shown. When looking at the developer console in the browser (Chrome or Safari) I see the following error Can't find variable: Plotly which points to line 141 of plotly.js, which is var plot = Plotly.plot(graphDiv, x) in the following code:
//if no plot exists yet, create one with a particular configuration
if (!instance.plotly) {
var plot = Plotly.plot(graphDiv, x);
instance.plotly = true;
instance.autosize = x.layout.autosize || true;
instance.width = x.layout.width;
instance.height = x.layout.height;
I tested this in Chrome and Safari, and it happens in both browsers, though when I refresh the page in Chrome the plots sometimes work.
So to test this further, I took this code from the Plotly website, and deployed it to our Shiny server environment to see if the same behaviour occurs, which is the case. As such I don't think I made a coding mistake.
I'm using the following package versions:
plotly 4.7.1
shiny 1.0.5
htmlwidgets 1.0
Shiny Server v1.5.3.838
Does anyone know how to solve this? Am I missing some packages or does something need to be configured in Shiny server to make this work?
Thanks!
I think what you may need is renderPlotly instead of renderPlot in your server. If you do this, the plot will show up on the viewer, but not in the shiny popup (blank graph) and won't work when deployed. An example of this where renderPlot has been replaced by renderPlotly is:
ui <- dashboardPage(skin = "blue",
dashboardHeader(title= "Fig. 11: State Forecasting and Nowcasting", titleWidth = 450),
dashboardSidebar(disable=TRUE),
dashboardBody(
fillPage(padding = 0,
box(title="", id="normal", solidHeader = TRUE, status = "primary", width=7,
plotlyOutput("plot1", height = 250)),
box(title="State to Examine", background = "black", width=5,
selectInput("variable", "State:", choices= sort(unique(afteradd2$State)), selected="Ohio")),
box(title="Forecast Length", background = "black", width=5,
selectInput("variable.two", "Forecast:", choices= c("Nowcast", "Three Month Forecast",
"Six Month Forecast"), selected="Nowcast"))
)))
server <- function(input, output) {
output$plot1<- renderPlotly({
par(mfrow=c(1,1))
par(mar = c(4, 5, 4, 4))
fstate(input$variable, input$variable.two)})}
shinyApp(ui=ui, server=server)
Without seeing your code it's difficult to know for sure though. If this isn't your issue could you share your code so I can try to help?
Related
I work for a biodiversity monitoring project and I am trying to build a shiny app to better organize our fieldwork. I am basically trying to publish a map of our study area overlaid with a grid (our monitoring units). I am new to shiny apps and publishing stuff online but we really need that so I tried my hands at a tiny shiny app but failed:
library(shiny)
library(leaflet)
ui <- fluidPage(leafletOutput("mymap", height = 800))
server <- function(input, output, session) {
rtp <- readRDS("./Data/rtp.RDS")
m <- mapview(rtp,
method = "ngb",
na.color = rgb(0, 0, 255, max = 255, alpha = 0),
query.type = "click",
trim = TRUE,
legend = FALSE,
map.types = "Esri.WorldImagery",
alpha.regions = 0,
lwd=2,
color="red")
output$mymap <- renderLeaflet({m#map})
}
shinyApp(ui, server)
The code works locally, as in I can see the map with the grid and interact with it. However, when I publish it (the app successfully deploys), all I get is a grey background with which I cannot interact and a small "Disconnected from server" note in the bottom left corner. I have no idea what to do.
The rtp grid file I used (only 12KB) can be downloaded here
Later, I plan to link this map to an online sheet (filled by field workers) within the shiny app to display fielwork progress.
I'm having an issue with the running of a R shiny app. Here's what I do:
open RStudio
load the shiny code (e.g. app.R)
set the wd
library(shiny)
press on "Run App"
Then nothing happens.
And if I try to terminate the execution, R does not answer anymore and says to force the closing of RStudio.
Here's one of my codes (I tried some, so I don't think this is the issue, but I report one anyway):
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
titlePanel("Un'applicazione con uno slider"),
sidebarLayout(
h1("Sposta lo Slider!"),
sliderInput("slider1", "Spostami!", 0, 100, 0)
),
mainPanel(
h3("Valore dello slider:"),
textOutput("text")
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$text <- renderText(input$slider1)
}
# Run the application
shinyApp(ui = ui, server = server)
Do you have any advice to give me in order to make anything happen? I have no errors shown, so I don't know what to do...
I just see "runApp(...mypath.../app)" and blank space after it.
Thank you in advance.
Edit 1:
I also tried this, but nothing happened (as before):
library(shiny)
runExample("01_hello")
Edit 2: Copy-pasting the code directly in the console doen not work either
I was experiencing the same issue with R 4.1 on Windows 10. Updating all packages seems to have solved it:
update.packages(ask = FALSE, checkBuilt = TRUE)
I'm new to Shiny and have been developing a web app that displays a plotly chart; I'm still developing the app on my local machine. When I run the app for the first time after opening RGui, the web app runs but does not render the chart, even after I have clicked the "Draw" button. I have to refresh the webpage once or twice and then the chart will render. Once the chart has rendered, the problem is gone for the duration of that R session. Refreshing the page or restarting the shiny program will continue to render the chart, until RGui is closed. The problem reliably recurs the next time I open RGui and run the app.
All the existing questions and answers I have searched on shiny and failed rendering have not answered my question.
After several frustrated attempts at trying to find what was wrong in my program, I boiled it down to this code that looks (to me) like it should be functional, but still presents the issue:
library(shiny)
library(plotly)
ui = fluidPage(
plotlyOutput("Plot"),
actionButton("drawPlotButton", "Draw")
)
server = function(input, output)
{
output$Plot = renderPlotly({
input$drawPlotButton
return(plot_ly(mtcars, x = ~hp, y = ~mpg, type = "scatter", mode = "markers"))
})
}
shinyApp(ui = ui, server = server)
I can't tell if I am missing something simple or what. All help is appreciated.
Generally runs fine for me as in the plot renders at initialization.
However, if your objective is to plot the graph only when the Draw button is clicked then:
You need to add a eventReactive method
See r Shiny action button and data table output
library(shiny)
library(plotly)
ui = fluidPage(
plotlyOutput("Plot"),
actionButton("drawPlotButton", "Draw")
)
server = function(input, output)
{
plot_d <- eventReactive(input$drawPlotButton, {
plot_ly(mtcars, x = ~hp, y = ~mpg, type = "scatter", mode = "markers")
})
output$Plot = renderPlotly({
plot_d()
})
}
shinyApp(ui = ui, server = server)
I am trying to plot a simple histogram inside a shiny app. Outside the app (in R script), the graph gets plotted without any problems, (see here) but the same code produces an odd looking graph from inside the app (see the wrong graph here)
Could you help me figure out what's wrong? Link to dataset: https://drive.google.com/open?id=1ITK0lTWm_mkb9KonHLq4nuKDv-PBUDeR
library(ggplot2)
library(ggthemes)
library(scales)
library(shiny)
# read data
cso_corruption <- read.csv("cso_corruption.csv", header = T, sep = ",")
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
radioButtons(inputId = "x",label ="Measurement Type",choices =
c("freq_business", "freq_cso"), selected = NULL)
),
mainPanel(
plotOutput(outputId = "hist")
)
)
)
server <- function(input, output) {
output$hist <- renderPlot(ggplot(data = na.omit(cso_corruption), aes(x=input$x)) +
geom_histogram(fill="lightgreen", stat="count"))
}
shinyApp(ui = ui, server = server)
Possible reason is the data is not making it into the server.
I would put the csv into another file within the directory, so you can access it using
read.csv("./Data/projectdata.csv")
Therefore it does not get lost when you publish. Also make sure the data is checked when publishing. One last note to include the read.csv function in the server.
After many trial-and-errors, I have figured out a solution. The trick is, in the server, I used aes_string instead of aes. I haven't figured out why aes_string works here, since it is supposed to require the variables to be passed in quotes. But it works for some reason.
I'm looking at implementing 3D interactive plots in my shiny App, and so far I've been using plotly. However, plotly has one major disadvantage, it's extremely slow when rendering.
I've done checks, and the whole creation of updated outplot$plot <- renderPlotly ({.....}) and plotlyOutput("plot") takes less than 0.5 seconds, despite the large data set involved. This is a know issue for years but still seems to be current.
Hence, I'm looking to use a package called car, also because it has many options, some which I particularly want that are not available in other packages. Info on the car package is here: http://www.sthda.com/english/wiki/amazing-interactive-3d-scatter-plots-r-software-and-data-visualization
The problem is that it renders in a separate popup window rather than inside the shiny app, and I want to have it inside it, or even better, add a button that allow the user to have it as a popup, but only when asked. However, i can't figure out how to jam the bugger into the actual shiny page.
Here is my minimal example with a single text element and the graph code that (in my case) keeps appearing in a separate window rather than in the app.
install.packages(c("rgl", "car", "shiny"))
library("rgl")
library("car")
library(shiny)
cars$time <- cars$dist/cars$speed
ui <- fluidPage(
hr("how do we get the plot inside this app window rather than in a popup?"),
plotOutput("plot", width = 800, height = 600)
)
server <- (function(input, output) {
output$plot <- renderPlot({
scatter3d(x=cars$speed, y=cars$dist, z=cars$time, surface=FALSE, ellipsoid = TRUE)
})
})
shinyApp(ui = ui, server = server)
There is also this package, scatterplot3d but that's not interactive
http://www.sthda.com/english/wiki/scatterplot3d-3d-graphics-r-software-and-data-visualization
And there are some RGL packages but they have the same issue (seperate window) and don't offer the options I am lookign for.
You need to use an rglwidget which takes the last rgl plot and puts in in an htmlwidget. It used to be in a separate package, but it has recently been integrated into `rgl.
Here is the code to do this:
library(rgl)
library(car)
library(shiny)
cars$time <- cars$dist/cars$speed
ui <- fluidPage(
hr("how do we get the plot inside this app window rather than in a popup?"),
rglwidgetOutput("plot", width = 800, height = 600)
)
server <- (function(input, output) {
output$plot <- renderRglwidget({
rgl.open(useNULL=T)
scatter3d(x=cars$speed, y=cars$dist, z=cars$time, surface=FALSE, ellipsoid = TRUE)
rglwidget()
})
})
shinyApp(ui = ui, server = server)
Yielding this: