My app runs fine locally. I can interact with it, etc. But when deployed globally it immediately throws the "Disconnected from Server" error. I've been googling for days and have tried everything I know how to try.
First things first:
I tried uninstalling then reinstalling a bunch of packages
I updated all the packages I am using
There is no information at all in the shiny logs. "This application currently has no logs."
My data (csv) is only 17MB (3 columns, 1M-ish rows)
when I reduced it to only about 200 rows I continued having the same issue
I went through the settings, changed the startup timeout to 300 and systematically changed each of the other settings, one by one.
I've read the browser's JavaScript logs end to end. They didn't make a ton of sense (probably because I don't know JavaScript), but nothing obvious stood out.
I am not loading my data with the entire file path, just the end part
My "Deploy" log in RStudio indicates successful deployment:
Preparing to deploy application...DONE
Uploading bundle for application: 193997...DONE
Deploying bundle: 994423 for application: 193997 ...
Waiting for task: 489405538
building: Building image: 992534
building: Fetching packages
building: Installing packages
building: Installing files
building: Pushing image: 992534
deploying: Starting instances
rollforward: Activating new instances
terminating: Stopping old instances
Application successfully deployed to https://jesstme.shinyapps.io/shinynames/
Deployment completed: https://jesstme.shinyapps.io/shinynames/
Link to the app itself:
https://jesstme.shinyapps.io/shinynames/
Server code:
#set wd & environment----
setwd("/Users/OldJess/Dropbox/R Stuff (Home)/ShinyNames")
#load packages------
library(datasets)
library(ggplot2)
library(viridis)
library(ggthemes)
library(gridExtra)
library(dplyr)
library(rdrop2)
library(shiny)
library(devtools)
#base <- read.csv("data/NationalNamesBrief.csv", stringsAsFactors = FALSE, row.names = NULL, na.strings = c("NA","","#MULTIVALUE"))
#temporary df for demonstration purposes
base <- structure(list(Name = c("Ellie", "Ellie", "Ellie", "Ellie", "Ellie",
"Ellie"),
Year = c(1880L, 1881L, 1882L, 1883L, 1883L, 1884L),
Gender = c("F", "F", "F", "F", "M", "F"),
Count = c(17L, 27L, 37L, 24L, 7L, 28L)),
.Names = c("Name", "Year", "Gender", "Count"),
row.names = c(NA, 6L), class = "data.frame")
#clean data----
base$name <- tolower(base$Name)
base$MF <- as.factor(base$Gender)
#add ranking data by Year
base <- base %>%
group_by(Year) %>%
arrange(Year, desc(Count)) %>%
mutate(Rank = row_number())
#add ranking data by Year AND Gender
base <- base %>%
group_by(Year, Gender) %>%
arrange(Year, desc(Count)) %>%
mutate(GenderRank = row_number())
#create functions----
#function to create line & heat charts
lineHeatCharts <- function(pickaname){
pickanameLower <- tolower(pickaname)
subDf <- subset(base[base$name == pickanameLower,])
heat <- ggplot(subDf, aes(x = Year, y = MF, fill = Count)) +
scale_fill_viridis(name = "",
option = "B",
limits = c(0, max(subDf$Count))) +
geom_tile(color = "white", size = 0) +
theme_tufte() +
theme(axis.text.x = element_text(angle = 90, vjust = 1, hjust = 1),
axis.ticks.x = element_blank()) +
scale_x_continuous(breaks = seq(min(subDf$Year),
max(subDf$Year), by = 5)) +
labs(x = "Year", y = "")
line <- ggplot(subDf, aes(x = Year, y = Count, fill = MF)) +
geom_line(aes(colour = factor(subDf$Gender)), size = 1.5) +
theme_tufte() +
theme(axis.text.x = element_blank(),
axis.ticks.x = element_blank()) +
scale_x_continuous(breaks = seq(min(subDf$Year),
max(subDf$Year), by = 5)) +
labs(x = "", y = "", color = "")
return(grid.arrange(line, heat,
ncol = 1, nrow = 2,
heights = c(5, 2), top = max(subDf$Name)))
}
# Define server logic
function(input, output) {
output$view <- renderPlot({
lineHeatCharts(input$list)
})
}
UI code:
library(shiny)
library(shinythemes)
# Define UI for dataset viewer application
fluidPage(theme = shinytheme("flatly"),
# Application title
titlePanel("First Names on U.S. Social Security Applications, 1880 - 2014"),
sidebarLayout(
sidebarPanel(
textInput(inputId = "list", label = "Enter a name:", value = "Ellie"),
helpText("Note: This page will take about 30 seconds to load the first time you open it. Data are from US Social Security applications via data.gov. For privacy, only names with at least 5 babies per year are included. Errors in Social Security form submission, like incorrect sex, are not corrected. Names with special characters and spaces are not included."),
submitButton("Refresh View")
),
mainPanel(
h4(""),
plotOutput("view")
)
)
)
Try this:
#set wd & environment----
#setwd("/Users/OldJess/Dropbox/R Stuff (Home)/ShinyNames")
#load packages------
library(datasets)
library(ggplot2)
library(viridis)
library(ggthemes)
library(gridExtra)
library(dplyr)
library(shiny)
library(shinythemes)
#base <- read.csv("data/NationalNamesBrief.csv", stringsAsFactors = FALSE, row.names = NULL, na.strings = c("NA","","#MULTIVALUE"))
#temporary df for demonstration purposes
base <- structure(list(Name = c("Ellie", "Ellie", "Ellie", "Ellie", "Ellie", "Ellie"),
Year = c(1880L, 1881L, 1882L, 1883L, 1883L, 1884L),
Gender = c("F", "F", "F", "F", "M", "F"),
Count = c(17L, 27L, 37L, 24L, 7L, 28L)),
.Names = c("Name", "Year", "Gender", "Count"),
row.names = c(NA, 6L), class = "data.frame")
#clean data----
base$name <- tolower(base$Name)
base$MF <- as.factor(base$Gender)
#add ranking data by Year
base <- base %>%
group_by(Year) %>%
arrange(Year, desc(Count)) %>%
mutate(Rank = row_number())
#add ranking data by Year AND Gender
base <- base %>%
group_by(Year, Gender) %>%
arrange(Year, desc(Count)) %>%
mutate(GenderRank = row_number())
#create functions----
#function to create line & heat charts
lineHeatCharts <- function(pickaname){
pickanameLower <- tolower(pickaname)
if(!any(base$name %in% pickanameLower)){
return()
}
subDf <- subset(base[base$name == pickanameLower,])
heat <- ggplot(subDf, aes(x = Year, y = MF, fill = Count)) +
scale_fill_viridis(name = "",
option = "B",
limits = c(0, max(subDf$Count))) +
geom_tile(color = "white", size = 0) +
theme_tufte() +
theme(axis.text.x = element_text(angle = 90, vjust = 1, hjust = 1),
axis.ticks.x = element_blank()) +
scale_x_continuous(breaks = seq(min(subDf$Year), max(subDf$Year), by = 5)) +
labs(x = "Year", y = "")
line <- ggplot(subDf, aes(x = Year, y = Count, fill = MF)) +
geom_line(aes(colour = factor(subDf$Gender)), size = 1.5) +
theme_tufte() +
theme(axis.text.x = element_blank(),
axis.ticks.x = element_blank()) +
scale_x_continuous(breaks = seq(min(subDf$Year),
max(subDf$Year), by = 5)) +
labs(x = "", y = "", color = "")
return(grid.arrange(line, heat, ncol = 1, nrow = 2, heights = c(5, 2), top = max(subDf$Name)))
}
ui <- fluidPage(theme = shinytheme("flatly"),
# Application title
titlePanel("First Names on U.S. Social Security Applications, 1880 - 2014"),
sidebarLayout(
sidebarPanel(
textInput(inputId = "list", label = "Enter a name:", value = "Ellie"),
helpText("Note: This page will take about 30 seconds to load the first time you open it. Data are from US Social Security applications via data.gov. For privacy, only names with at least 5 babies per year are included. Errors in Social Security form submission, like incorrect sex, are not corrected. Names with special characters and spaces are not included."),
submitButton("Refresh View")
),
mainPanel(
h4(""),
plotOutput("view")
)
)
)
server <- function(input, output, session) {
output$view <- renderPlot({
lineHeatCharts(input$list)
})
}
shinyApp(ui, server)
As it turns out there were two problems:
1) I needed to remove the setwd() 2nd line of my code
2) Shiny logs weren't working.
I posted on Google's Shiny forum, and a RStudio person fixed the issue with the logs. Once the logs were working I saw that the error pointed to my attempts to setwd. Deleted that and the problem was fixed. Keeping this question up because I'm sure this issue will plague someone else at some point.
Related
I am fairly new to R Shiny and I've been working on an app with an interactive world map which shows each country's performance at the Olympics, using scale_fill_gradient. The app user gets to choose the performance indicator (total medals won, gold only, weighted score) and the year (1996 to 2020).
The problem is there's no more error shown, but the plot doesn't show either! I have tried to run the functions as normal R script and they worked fine there (the plot showed up in the viewer pane). I found a few others who have also run into problems with no plot or error showing, but their cases are different to mine (e.g. mismatch in Output and Render) so the resolutions don't work for me.
It's a massive dataset so I've not included it here, I thought I might check first if the error could be spotted from the code alone. Here's what I've used:
function
world_map1 <- function(WorldMap, year, performance) {
w_plot1 <- WorldMap %>%
filter(Year == year) %>%
select("long", "lat", "group", "region", all_of(performance)) %>%
replace(is.na(.), 0) %>%
rename_at(performance, ~ "Value") %>%
mutate(Value = as.numeric(as.character(Value)))
tooltip_css <- "background-color:#2E2E2E; font-family: Calibri; color:#F2F2F2;"
w_g1 <- ggplot() +
geom_polygon_interactive(data = subset(w_plot1, lat >= -60 & lat <= 90),
aes(x = long,
y = lat,
fill = Value,
group = group,
tooltip = sprintf("%s<br/>%s", region, Value))) +
scale_fill_gradient(name = "Medals /Score",
low = "lightgoldenrodyellow",
high = "goldenrod1",
na.value = "white")
return(
girafe(
ggobj = w_g1,
options = list(
opts_tooltip(
css = tooltip_css
)
))
)
}
ui
ui <- fluidPage(
titlePanel("Title"),
sidebarLayout(
sidebarPanel(
radioButtons(inputId = "performance", label = "Performance measure:",
choices = c("Total medals won" = "Total",
"Gold medals won" = "Gold",
"Weighted points system" = "Weighted"
)),
width = 3
),
mainPanel(
girafeOutput("mapPlot1"),
sliderInput(inputId = "year", label = "Year:",
min = 1996, max = 2020, step = 4, value = 1996, ticks = FALSE, sep = ""
)
)
)
)
server
server <- function(input, output) {
output$mapPlot1 <- renderGirafe({
ggiraph(code = print(world_map1(WorldMap, input$year, input$performance)))
}
)
}
run app
shinyApp(ui = ui, server = server)
Any help or insights appreciated!
I thought it was my theme() block so I removed that, as shown above. Also checked other cases on no plot showing here, couldn't find one with fixes that would work for me because it seems the underlying problem is different?
I'm fairly new to r and shiny, so bear with me - I have created a plot which shows the accumulated weekly distance covered by players in a sports team, where the user can select the player and the week range. Each player has an individual target distance they should meet and I want the data points in the plot to be green if they have met the target and red if they have not.
The data for weekly distance and target distance are located in different data frames (and they need to be) so I need that when a player is selected in selectInput(), the weekly distance is pulled from the first data frame and the target for the same player is pulled from the second data frame and used for conditional formatting.
EDIT - This is the gps2 data frame (though the PlayerName column lists the actual name which I've changed to initials here):
structure(list(Week = c(14, 14, 14, 14, 14, 15), PlayerName = c("CF",
"DR", "GB", "KB", "RA",
"AM"), Distance = c(3.8088, 2.1279, 2.4239, 1.3565,
4.5082, 4.4097), SprintDistance = c(291.473, 146.97, 11.071,
67.596, 252.787, 0), TopSpeed = c(22.6402, 21.3442, 20.5762,
21.6002, 20.5602, 18.6401)), row.names = c(NA, -6L), groups = structure(list(
Week = c(14, 15), .rows = structure(list(1:5, 6L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = 1:2, class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
And the targets data frame:
structure(list(PlayerName = c("AM", "AB", "AMc",
"BC", "CD", "CM"), Distance = c(28.2753333333333,
34.867, NA, 31.633, 34.6122, 32.1405), SprintDistance = c(1355.2,
1074.85, NA, 2426.55, 2430.54, 2447.9), TopSpeed = c(32.61, 30.3,
NA, 36.82, 42, 33.44)), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
I have been working on this for a few days now and can't wrap my head around how to do it or find a post which describes what I want to do. So far this is what I have:
# DEFINE UI ####
ui <- fluidPage(
titlePanel("GPS Monitoring Dashboard"),
sidebarLayout(
sidebarPanel(
#select player
selectInput(inputId = "name",
label = strong("Choose player"),
choices = unique(gps2$PlayerName),
selected = "AB"),
#select weeks
numericRangeInput(inputId = "week",
label = strong("Choose weeks"),
value = c(36, 37))),
# graphs and tables
mainPanel(
plotOutput(outputId = "TD"),
tableOutput(outputId = "TDsum"))
)
)
# DEFINE SERVER ####
server <- function(input, output) {
# Total Distance ----
# Data for distance plot
TD_plot <- reactive({
gps2 %>%
filter(PlayerName == input$name,
Week >= input$week [1] &
Week <= input$week [2],
) %>%
select(Distance)
})
# Build distance plot
output$TD <- renderPlot({
ggplot(TD_plot()) +
geom_point(aes(Week, Distance,
color = Distance > 5),
stat = "identity", size = 3) +
scale_color_manual(name = "Target met", values = set_names(c("green", "red"), c(TRUE, FALSE))) +
geom_line(aes(Week, Distance), size = 1) +
labs(title = "Weekly Total Distance", x = "Week", y = "Distance (km)")
})
# Data for distance table
TD_sum <- reactive({
gps2 %>%
filter(PlayerName == input$name,
Week >= input$week [1] &
Week <= input$week [2])%>%
select(Distance) %>%
pivot_wider(.,
names_from = Week,
values_from = Distance)
})
# Build distance table
output$TDsum <- renderTable(TD_sum())
}
shinyApp(ui = ui, server = server)
Right now the data points changes based on an arbitrary value (5) as I was trying to expand on that. I hope this explains in enough detail what I'm trying to do, thanks in advance for your help!
Here's a working example that may be helpful.
First, would left_join your actual distances by players, and their target distances. This will rename columns with "Actual" or "Target" as suffixes to keep them apart.
In geom_point you can use color = DistanceActual > DistanceTarget to have differential color based on whether a distance is greater or less than the target.
I simplified the other functions for demonstration.
library(shiny)
library(tidyverse)
full_data <- left_join(gps2, df_targets, by = "PlayerName", suffix = c("Actual", "Target"))
# DEFINE UI ####
ui <- fluidPage(
titlePanel("GPS Monitoring Dashboard"),
sidebarLayout(
sidebarPanel(
#select player
selectInput(inputId = "name",
label = strong("Choose player"),
choices = unique(full_data$PlayerName),
selected = "player1"),
#select weeks
numericRangeInput(inputId = "week",
label = strong("Choose weeks"),
value = c(36, 37))),
# graphs and tables
mainPanel(
plotOutput(outputId = "TD"),
tableOutput(outputId = "TDsum"))
)
)
# DEFINE SERVER ####
server <- function(input, output) {
# Filter by week and player name
TD_data <- reactive({
full_data %>%
filter(PlayerName == input$name,
Week >= input$week [1],
Week <= input$week [2])
})
# Build distance plot
output$TD <- renderPlot({
ggplot(TD_data()) +
geom_point(aes(Week, DistanceActual, color = DistanceActual > DistanceTarget), stat = "identity", size = 3) +
scale_color_manual(name = "Target met", values = set_names(c("green", "red"), c(TRUE, FALSE))) +
geom_line(aes(Week, DistanceActual), size = 1) +
labs(title = "Weekly Total Distance", x = "Week", y = "Distance (km)")
})
# Build distance table
output$TDsum <- renderTable(
TD_data() %>%
select(Week, DistanceActual)
)
}
shinyApp(ui = ui, server = server)
I'm working on a Shiny dashboard for a personal project with some football stats. Whenever I change the statistic to be graphed and/or the filter, I get the same players that were in the first dataset. For example, when I start the app, the app creates a graph of the top ten rushers in school history with a filter of rushing attempts >= 0. When I change the statistic selection to rushing average, however, those ten players are the ones shown, which is incorrect.
library(readxl)
library(tidyverse)
library(purrr)
library(shiny)
interface <- fluidPage(
titlePanel(" "),
sidebarLayout(
sidebarPanel(
h1("Stats!"),
selectInput("stat_selection",
label = "Select a season statistics",
choices = c("Rushing Yards",
"Rushing Touchdowns",
"Rushing Average",
"Reciving Yards",
"Receptions",
"Receiving Touchdowns",
"Receiving Average"),
selected = "Rushing Yards"),
selectInput("filter_input",
label = "Select a statistic to filter by",
choices = c("Rushing Yards",
"Rushing Touchdowns",
"Rushing Average",
"Rushing Attempts",
"Reciving Yards",
"Receptions",
"Receiving Touchdowns",
"Receiving Average"),
selected = "Rushing Attempts"),
numericInput("filter_number",
label = "Type a number for the filter (>=)",
value = 0, min = 0),
actionButton("button", "Graph")),
mainPanel(
plotOutput("plot_button"),
tableOutput("table_button")
)
)
)
server_osu <- function(input, output) {
dataInput <- reactive({
switch(input$stat_selection,
"Rushing Yards" = rush_yds,
"Rushing Touchdowns" = rush_tds,
"Rushing Average" = rush_avg,
"Reciving Yards" = rec_yds,
"Receptions" = rec_rec,
"Receiving Touchdowns" = rec_td,
"Receiving Average" = rec_avg)
})
filterInput <- reactive({
switch(input$filter_input,
"Rushing Yards" = rush_yds,
"Rushing Touchdowns" = rush_tds,
"Rushing Average" = rush_avg,
"Rushing Attempts" = rush_att,
"Reciving Yards" = rec_yds,
"Receptions" = rec_rec,
"Receiving Touchdowns" = rec_td,
"Receiving Average" = rec_avg)
})
filter_number <- reactive(as.double(input$filter_number))
table_button_react <- eventReactive(input$button, {
dataset <- dataInput()
val <- filter_number()
colnames(dataset)[1] = "Player and Season"
dataset_filter <- filterInput()
colnames(dataset_filter)[1] = "Player and Season"
dataset <- left_join(dataset, dataset_filter)
colnames(dataset)[1] = "Player and Season"
og <- colnames(dataset)[3]
colnames(dataset)[3] = "filter"
original <- colnames(dataset)[2]
colnames(dataset)[2] = 'selected'
dataset <- dataset %>%
filter(filter >= val)
dataset <- dataset %>%
top_n(10) %>%
arrange(-selected)
colnames(dataset)[2] = original
colnames(dataset)[3] = og
dataset
})
plot_button_react <- eventReactive(input$button, {
dataset <- dataInput()
val <- filter_number()
colnames(dataset)[1] = "Player and Season"
dataset_filter <- filterInput()
colnames(dataset_filter)[1] = "Player and Season"
dataset <- left_join(dataset, dataset_filter)
colnames(dataset)[1] = "Player and Season"
colnames(dataset)[2] = "selected"
colnames(dataset)[3] = "filter"
dataset <- dataset %>%
filter(filter >= val)
top_ten <- dataset %>% top_n(10)
min = min(top_ten$selected)
max = max(top_ten$selected)
ggplot(top_ten, aes(x = reorder(`Player and Season`, -selected), y = selected)) +
geom_bar(stat = 'identity') + theme_minimal() + xlab('SEASON') +
ylab(input$stat_selection) + theme(text=element_text(size=16)) +
scale_fill_manual(values = c('#BBBBBB', '#BB0000')) +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
theme(legend.position = 'none') +
coord_cartesian(ylim=c(min - 0.05*min, max + 0.05*max)) +
theme(axis.title.y = element_text(margin = margin(t = 0, r = 10, b = 0, l = 10))) +
theme(axis.title.x = element_text(margin = margin(t = 10, r = 0, b = 10, l = 0))) +
theme(axis.text.y = element_text(size=14),
axis.title=element_text(size=16,face='bold')) +
labs(caption = '')
})
output$plot_button <- renderPlot({
plot_button_react()
})
output$table_button <- renderTable({
table_button_react()
})
}
As I mentioned above, a reprex - including input data - would help us to help you. That said, I think the problem is that your XXX_button_reacts depend only on input$button. They don't depend on input$stat_selection, input$filter_number or input$filter_input. That's why they don't update as you want them to.
The fix is easy. Just add them (in a call to req() if you like) at the top of each XXXX_button_react, for example:
plot_button_react <- eventReactive(input$button, {
input$stat_selection
input$filter_number
input$filter_input
<your code here>
})
As a point of style, I feel it's better to separate data generation from data presentation. It makes the logic of your code more obvious, reduces the chance of errors, reduces the need for code duplication and makes your code more reusable.
In your case, I would create a reactive that holds the data you wish to tablulate and plot and then reference that reactive in each of your render_XXXX functions. That would also remove the need for your input$button: the plot and graph would each update automatically whenever you changed one of your other input widgets.
This is my first shiny app. I would like for the user to be able to update the number of facet columns and the dimensions of downloaded plot. readNWISuv, the function to download data can take a long time if multiple years are queried. Currently, the app downloads the data each time the user wants to change the plot format or plot dimensions. Not sure if I need to use reactiveValues, but I would assume that I want the data to be downloaded and manipulated outside of renderPlot. Thanks!
library(shiny)
library(dataRetrieval)
library(lubridate)
library(tidyverse)
library(plotly)
#flow wrecker
ui <- pageWithSidebar( #fluidPage(
# Application title
titlePanel("Flow Record"),
# Sidebar with a date input
#sidebarLayout
sidebarPanel(
dateRangeInput("daterange", "Date range: (yyyy-mm-dd)",
start = Sys.Date()-10,
min = "1980-10-01"),
textInput("gage", "USGS Gage #", "11532500"),
#actionButton("dload","Download data"),
selectInput("facet_x", "Facet Column #:", 2, choices =1:4),
submitButton("Update View", icon("refresh")),
helpText("When you click the button above, you should see",
"the output below update to reflect the values you",
"entered above:"),
#verbatimTextOutput("value"),
downloadButton('downloadImage', 'Download figure'),
numericInput("fig_x", "Fig. Dim. x:", 10, min = 3, max = 16),
numericInput("fig_y", "Fig. Dim. y:", 10, min = 3, max = 16),
width = 3
),
# Show a plot of the generated WY
mainPanel(
plotlyOutput("WYfacet")
)
)
# Define server draw WY facets
server <- function(input, output) {
parameterCd <- "00060" # discharge
#water year
wtr_yr <- function(dates, start_month=10) {
# Convert dates into POSIXlt
dates.posix = as.POSIXlt(dates)
# Year offset
offset = ifelse(dates.posix$mon >= start_month - 1, 1, 0)
# Water year
adj.year = dates.posix$year + 1900 + offset
# Return the water year
adj.year
}
output$WYfacet <- renderPlotly({
#progress bar
withProgress(readNWISuv(input$gage, parameterCd, input$daterange[1], input$daterange[2],tz="America/Los_Angeles") %>% addWaterYear,
message = 'Download in progress',
detail = 'This may take a while...', value = 1)
#download
temperatureAndFlow <- readNWISuv(input$gage, parameterCd, input$daterange[1], input$daterange[2],tz="America/Los_Angeles") %>% addWaterYear
names(temperatureAndFlow)<-c("agc","site","date","WY", "flow","a","tzone")
temperatureAndFlow$commonDate <- as.Date(format(temperatureAndFlow$date, format="2000-%m-%d"))
tf.df<-temperatureAndFlow %>%
filter(WY<=max(WY) & WY>=if_else(month(min(date))<10,min(WY)+1,min(WY)))
tf.df$date.d<-format(tf.df$date, format="%Y-%m-%d")
#mutate commonDate
df4 <- tf.df %>%
mutate(WY=factor(wtr_yr(date.d))) %>%
#seq along dates starting with the beginning of your water year
mutate(commonDate=as.Date(paste0(ifelse(month(date.d) < 10, "2001", "2000"),
"-", month(date.d), "-", day(date.d))), Date=date.d)
#plot
ploty<-ggplot(data = df4,mapping = aes(x = commonDate, y = flow,label=Date, colour = factor(WY))) +
geom_line() +
labs(x = " ", y = "Discharge (cfs)") +
facet_wrap(facets = ~WY,ncol=as.integer(input$facet_x)) +
scale_y_log_eng()+
annotation_logticks(sides = "l")+
theme_bw()+
theme(panel.grid.minor.x = element_blank())+
scale_x_date(labels = function(x) format(x, "%b"),date_breaks = "1 months")+
guides(colour=FALSE)
ggplotly(ploty, tooltip=c("flow","Date"))
})
#fig dimensions
output$fig_x <- renderText({ input$fig_x })
output$fig_y <- renderText({ input$fig_y })
#facet columns
output$facet_x <- renderText({ input$facet_x })
#download to computer
output$downloadImage <- downloadHandler(
filename = function(){paste("plot",'.png',sep='')},
content = function(file){
ggsave(file,width = input$fig_x,height = input$fig_y, dpi = 600, units = "in", device='png')
print(ggplot(data = df4,mapping = aes(x = commonDate, y = flow, colour = factor(WY))) +
geom_line() +
#geom_point()+
#geom_vline(data = trip,aes(xintercept=commonDate),trip_df,color="black")+
labs(x = " ", y = "Discharge (cfs)") +
facet_wrap(facets = ~WY,ncol=as.integer(input$facet_x)) +
scale_y_log_eng()+
annotation_logticks(sides = "l")+
theme_bw()+
theme(panel.grid.minor.x = element_blank())+
scale_x_date(labels = function(x) format(x, "%b"),date_breaks = "1 months")+
guides(colour=FALSE))
})
}
# Run the application
shinyApp(ui = ui, server = server)
There are a few changes to make to your sever section to make this work. Primarily:
splitting the creation of the dataframe into a new eventReactive function, dependent on an actionButton.
referring to the function inside the renderPlotly call
Try this:
## Within ui function call ############################################
# submitButton("Update View", icon("refresh")), # line to replace
actionButton(inputId = "update", "Update View", icon("refresh")),
## (if you want to keep a button to control when data is downloaded ##
server <- function(input, output) {
parameterCd <- "00060" # discharge
#water year
wtr_yr <- function(dates, start_month=10) {
# Convert dates into POSIXlt
dates.posix = as.POSIXlt(dates)
# Year offset
offset = ifelse(dates.posix$mon >= start_month - 1, 1, 0)
# Water year
adj.year = dates.posix$year + 1900 + offset
# Return the water year
adj.year
}
# New part here - use `reactive` to make df4 a new thing, which is processed separately. The `eventReactive` function waits till it sees the button pressed.
df4 <- eventReactive(input$update, ignoreNULL = FALSE, {
#progress bar
withProgress(readNWISuv(input$gage, parameterCd, input$daterange[1], input$daterange[2],tz="America/Los_Angeles") %>% addWaterYear,
message = 'Download in progress',
detail = 'This may take a while...', value = 1)
#download
temperatureAndFlow <- readNWISuv(input$gage, parameterCd, input$daterange[1], input$daterange[2],tz="America/Los_Angeles") %>% addWaterYear
names(temperatureAndFlow)<-c("agc","site","date","WY", "flow","a","tzone")
temperatureAndFlow$commonDate <- as.Date(format(temperatureAndFlow$date, format="2000-%m-%d"))
tf.df<-temperatureAndFlow %>%
filter(WY<=max(WY) & WY>=if_else(month(min(date))<10,min(WY)+1,min(WY)))
tf.df$date.d<-format(tf.df$date, format="%Y-%m-%d")
#mutate commonDate
tf.df %>%
mutate(WY=factor(wtr_yr(date.d))) %>%
#seq along dates starting with the beginning of your water year
mutate(commonDate=as.Date(paste0(ifelse(month(date.d) < 10, "2001", "2000"),
"-", month(date.d), "-", day(date.d))), Date=date.d)
})
output$WYfacet <- renderPlotly({
# req will pause plot loading till new data downloaded above, but changes to display will render without new download
req(df4())
#plot
ploty<-ggplot(data = df4(), # Put brackets here to refer to df4 as a reactive input!!!
mapping = aes(x = commonDate, y = flow, label=Date, colour = factor(WY))) +
geom_line() +
labs(x = " ", y = "Discharge (cfs)") +
facet_wrap(facets = ~WY,ncol=as.integer(input$facet_x)) +
scale_y_log10()+
# annotation_logticks(sides = "l")+
theme_bw()+
theme(panel.grid.minor.x = element_blank())+
scale_x_date(labels = function(x) format(x, "%b"),date_breaks = "1 months")+
guides(colour=FALSE)
ggplotly(ploty, tooltip=c("flow","Date"))
})
#fig dimensions
output$fig_x <- renderText({ input$fig_x })
output$fig_y <- renderText({ input$fig_y })
#facet columns
output$facet_x <- renderText({ input$facet_x })
#download to computer
output$downloadImage <- downloadHandler(
filename = function(){paste("plot",'.png',sep='')},
content = function(file){
ggsave(file,width = input$fig_x,height = input$fig_y, dpi = 600, units = "in", device='png')
print(ggplot(data = df4() ,mapping = aes(x = commonDate, y = flow, colour = factor(WY))) +
geom_line() +
#geom_point()+
#geom_vline(data = trip,aes(xintercept=commonDate),trip_df,color="black")+
labs(x = " ", y = "Discharge (cfs)") +
facet_wrap(facets = ~WY,ncol=as.integer(input$facet_x)) +
scale_y_log10()+
annotation_logticks(sides = "l")+
theme_bw()+
theme(panel.grid.minor.x = element_blank())+
scale_x_date(labels = function(x) format(x, "%b"),date_breaks = "1 months")+
guides(colour=FALSE))
})
}
EDITED to include full UI and sample data
I did read the other StackOverflow qs on this issue, but none seemed to address the cause of my error.
When the app loads, I get "error object [name of district I've selected] not found" for the District (inputID = "d"). I know it must be an issue with the subsetting reactive in the server, but I've tried everything (loading the data in the server, removing the vector from the filter function, changing the data type of the variables).
I also took this code from another Shiny App I built, which works. I can't see any differences between the two, besides that one is geom_point() and this is geom_col() so again, not sure what is going on.
Thanks!
Sample data:
sample <- sample_n(pop, 10)
dput(sample)
structure(list(GazID = c(NA, NA, "13872", NA, "13610", "13985",
"13984", "13434", "13428", "13631"), Province = c("Niolandskaia",
"Kaluzhskaia", "Iaroslavskaia", "Vyborgskaia", "Moskovskaia",
"Volynskaia", "Volynskaia", "Orenburgskaia", "Orenburgskaia",
"Arkhangel'skaia"), District = c(NA, "Suhinichinbezuezdniigorod",
"Romanov", NA, "Zvenigorod", "Kovel", "Lutsk", "Ufa", "Orenburg",
"Mezen"), TotalPop = c(NA, NA, 104104, NA, 71746, 103381, 102779,
93145, 62740, 26796), Male = c(NA, NA, 48604, NA, 36948, 52266,
50393, 46403, 32617, 13078), Female = c(NA, NA, 55500, NA, 34798,
51115, 52386, 46742, 30123, 13718), City = c(NA, 5552, NA, NA,
1253, 4254, 5552, 6682, 9533, NA), Rural = c(NA, NA, NA, NA,
70493, 99127, 97228, 86483, 53207, NA)), row.names = c(NA, -10L
), class = c("tbl_df", "tbl", "data.frame"))
Above the UI:
library(tidyverse)
library(readr)
library(shiny)
library(stringr)
library(rebus)
pop <- read_csv("pop.csv")
pop$TotalPop <- str_replace_all(pop$TotalPop, pattern = fixed(","), replacement = "")
pop$Male <- str_replace_all(pop$Male, pattern = fixed(","), replacement = "")
pop$Female <- str_replace_all(pop$Female, pattern = fixed(","), replacement = "")
pop$City <- str_replace_all(pop$City, pattern = fixed(","), replacement = "")
pop$Rural <- str_replace_all(pop$Rural, pattern = fixed(","), replacement = "")
pop$District <- str_remove_all(pop$District, pattern = "[^[:alnum:]]")
pop$TotalPop <- as.numeric(pop$TotalPop)
pop$Male <- as.numeric(pop$Male)
pop$Female <- as.numeric(pop$Female)
pop$City <- as.numeric(pop$City)
pop$Rural <- as.numeric(pop$Rural)
pop$GazID <- as.character(pop$GazID)
pop$District <- str_trim(pop$District)
The UI:
ui <- fluidPage(
titlePanel("Population Data from VSO"),
sidebarLayout(
sidebarPanel(
selectInput(inputId = "y", #internal label
label = "Population to map", #label that user sees
choices = c("Total population" = "TotalPop",
"Male population" = "Male",
"Female population" = "Female",
"Urban population" = "City",
"Rural population" = "Rural"),
selected = "TotalPop"),
selectizeInput(inputId = "d",
label = "Select district",
choices = c(pop$District),
multiple = TRUE, # can choose multiple
options = list(maxItems = 5))),
mainPanel(
plotOutput("plot")
)
)
)
The server:
server <- function(input, output) {
pop_subset <- reactive({
req(input$d)
filter(pop, District %in% c(input$d)
)})
output$plot <- renderPlot({
ggplot(data = pop_subset(), aes_string(x = pop_subset()$District, y = input$y)) +
geom_col(aes(fill = pop_subset()$District)) +
labs(x = "District", y = "Population") +
scale_fill_discrete(name = "Districts")
})}
shinyApp(ui = ui, server = server)
The problem is that you are using aes_string in your ggplot, but trying to pass District without quotes. I realize you need aes_string because you are using input$y, so just change your plot call to
output$plot <- renderPlot({
req(pop_subset())
ggplot(data = pop_subset(), aes_string(x = "District", y = input$y)) +
geom_col(aes(fill = District)) +
labs(x = "District", y = "Population") +
scale_fill_discrete(name = "Districts")
})
For reproducibility, packages and some sample data (no idea of its true representative nature, doesn't really matter I think).
library(dplyr)
library(shiny)
library(ggplot2)
set.seed(42)
n <- 50
pop <- data_frame(
TotalPop = sample(1e4, size=n, replace=TRUE)
) %>%
mutate(
Male = pmax(0, TotalPop - sample(1e4, size=n, replace=TRUE)),
Female = TotalPop - Male,
City = sample(LETTERS, size=n, replace=TRUE),
District = sample(letters, size=n, replace=TRUE)
)