Different behavior of deployed app on shinyapps.io

TL;DR: When running my R shiny app online, the dates are sorted correctly. When deploying the same app to shinyapps.io, the sorting of the date gets messed up. I have compared package versions between my local and online R instance, and they seem to be identical. Why are the two instances different?

Data ordering is done by creating a separate factor. Specifically, it was performed as follows:

df_sorted$Date <- factor(df_sorted$Date, levels = unique(df_sorted$Date), ordered = T)

I then create a boxplot with the following code (snippet shown):

ggplot(df_sorted, aes(x = Date, y = Hours_total, fill = State)) +
    geom_boxplot(outlier.shape = NA) +
    coord_cartesian(ylim = c(0, 2500))

Here is the resulting plot on my local computer (correct sorting!):


Here is the same plot on shinyapps.io (incorrect sorting!):

The last 4 entries are clearly incorrectly sorted, the rest is equal.

What I tried already, without any success:

  • Archiving the app
  • Deleting the app
  • Disabling the Package Cache

Any help is greatly appreciated!

I know it's not ideal, but one way to troubleshoot this is to print off some information about df_sorted prior to turning it into a factor variable, uploading your app, and then looking at what it prints out in the shinyapps.io logs. Comparing the logs to what the app prints off locally should give you an idea of what's causing (or not causing) your issues.

There's probably an easier way but that's how I'd start debugging this. You could print(str(df_sorted)) and print(unique(df_sorted$Date)) to start. Also, print(levels(df_sorted$Date)) after turning it into a factor variable might be helpful.

Great idea, unfortunately I am unsure what to do with the new information. Let me explain.

First, here is some more code. The original dataframe has the Date saved in the following format:


[1] "Jul 21" "Jul 21" "Jul 21" "Jul 21" "Jul 21" "Jul 21"

Afterwards, I do a custom sort before turning the Date into a factor (see first post):

df_sorted <- df[order(as.Date(
  paste(df_sorted$Date, " 01", sep = ""), format = "%B %y %d"
  )), ]


[1] "Jul 14" "Jul 14" "Jul 14" "Jul 14" "Jul 14" "Jul 14"

When running the print statements, I learned that already the first one, print(str(df_sorted)), leads to a different sorting between the web application and my own.

Any ideas why this could be? I don't see any problem with my first statement.

It's hard to know for sure without a reproducible example. Can you create a reprex from the data and processing portion of the script?

Are you running the app locally from a clean environment?

It could be that the local app is reading df_sorted$Date into ggplot as an ordered factor because it exists in that format in your local environment. The app on shinyapps.io is running from a clean environment, so df_sorted$Date might be being treated as a character vector - hence, why it's not being ordered in the figure. This implies that your code to process the character vector into an ordered factor isn't working, but also isn't throwing errors. You could test this by writing print(class(df_sorted$Date)) into the script right before the ggplot function runs.

That's my best guess without a reprex to work with!

1 Like