I already faced one really weird problem and I am facing another. I have already "solved" the first one but I mention it here as well because I do not mean it is "solved" in the right way.
By the way, just a note here: everything is working fine in console, just Shiny is somehow bugged or I dont know.
Problem nr. 1:
I styled my UI in a proper way, I won't post it here because it would be long and it is irrelevant. The important part of the UI is plotOutput('survival_curve', height = '750px'). Also there a few inputs as you will see in the code.
So I now I can work on the code content in shinyServer() function. So I do the following (ATTENTION - I had to translate some variable names in code from Czech to English. So I hope did not do any mistake nor I did not forgot to translate some variable names:
shinyServer(
...
...
...
output$survival_curve <- renderPlot(
curve_year_bottom <- input$survival_curve_input_years[1]
curve_year_top <- input$survival_curve_input_years[2]
curve_group_after_years <- input$survival_curve_input_group_after
data_survival_curve <- data[data$transplant_year %in% seq(curve_year_bottom, curve_year_top) &
!is.na(data$survival_time) &
data$survival_time >= 0,]
data_survival_curve$time_period <- cut(as.numeric(data_survival_curve$transplant_year),
seq(curve_year_bottom, curve_year_top, curve_group_after_years),
include.lowest = T)
surv_obj <- Surv(data_survival_curve$survival_time/365,data_survival_curve$patient_died)
fit <- survfit(surv_obj ~ time_period, data = data_survival_curve)
survival_curve_plt <- ggsurvplot(fit,
linetype = c('solid'),
ggtheme = theme_bw(),
surv.scale = 'percent',
xlab = 'Years',
ylab = '%',
censor = FALSE,
break.x.by = 1,
break.y.by = 0.1) +
geom_dl(aes(label = time_period), method = list("last.points"), cex = 0.8)
print(survival_curve_plt)
}
)
Ok now, when I start my server like this, I get an error saying: Error: object 'data_survival_curve' not found. It is completely weird right so I try to define a variable data_survival_curve before shinyServer() is called. So I do that in var.R script, source(var.R) and Voila!, it seems that the object was found but now I am getting another error: Error: object 'surv_obj' not found. This error comes from survfit() from the first argument. So I repeat the same - I predefine all the variables that are passed into ggsurvplot() and again - Voila! - it works! Can anyone tell me how to get rid of this? It seems like some functions can not find these temporary variables created in shinyServer().
Problem nr. 2
At first I thought it is the same kind of problem as the first one was. Nope, it is not.
So, I explain. Look at these lines of code - they are the same as in the first code snippet:
survival_curve_plt <- ggsurvplot(fit,
linetype = c('solid'),
ggtheme = theme_bw(),
surv.scale = 'percent',
xlab = 'Years',
ylab = '%',
censor = FALSE,
break.x.by = 1,
break.y.by = 0.1) +
geom_dl(aes(label = time_period), method = list("last.points"), cex = 0.8)
If I run this code with predefined variables in command line, graph is perfectly plotted and I am happy. It looks like this:
*Roky == Years
Now I come to a problem when I run this code in my shinyServer(). The layer created by geom_dl() (the labels at the end of the each line) is not plotted and I do not know what to do with that.
I think Shiny is somehow bugged. I do not really see problems in my code.
EDIT:
Test data:
data <- data.frame(id = c(1,2,3,4,5,6), patient_died = c(1,0,0,1,1,0), survival_time = c(21, 378, 3356, 7652, 3321, 324), transplant_year = c(2002, 2016, 2018, 2017, 2016, 2017))
This is how my graph for which was generated by the following lines of code for test data should look like on website created by Shiny:
data_survival_curve$time_period <- cut(as.numeric(data_survival_curve$transplant_year),
seq(2002, 2020, 2),
include.lowest = T)
surv_obj <- Surv(data_survival_curve$survival_time/365,data_survival_curve$patient_died)
fit <- survfit(surv_obj ~ time_period, data = data_survival_curve)
ggsurvplot(fit,
linetype = c('solid'),
ggtheme = theme_bw(),
surv.scale = 'percent',
xlab = 'Roky',
ylab = '%',
censor = FALSE,
break.x.by = 1,
break.y.by = 0.1) +
geom_dl(aes(label = time_period), method = list("last.points"), cex = 0.8)
And this is how it really looks like on my Shiny website:

I am using the following packages in my application:
library(shiny)
library(ggplot2)
library(dplyr)
library(survival)
library(rms)
library(survminer)
library(ggfortify)
library(directlabels)

ggsurvplotis of the classggsurvplot. You need to convert it to aggplotobject and then print the converted object to make it work withshiny. The reason this works in the command line is becauseprintis a generic functionprint()shinyServer()function. Or I see that incorrectly?print(survival_curve_plt)withprint(survival_curve_plt$plot)is what I meant.