0

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)

enter image description here

And this is how it really looks like on my Shiny website: enter image description here

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)
7
  • The return value is of ggsurvplot is of the class ggsurvplot. You need to convert it to a ggplot object and then print the converted object to make it work with shiny. The reason this works in the command line is because print is a generic function Mar 12, 2018 at 23:00
  • I do the same print() shinyServer() function. Or I see that incorrectly?
    – scarface
    Mar 12, 2018 at 23:10
  • Try replacing print(survival_curve_plt) with print(survival_curve_plt$plot) is what I meant. Mar 12, 2018 at 23:16
  • I havent been at laptop yet so I could not try it yet but I still do not understand why shiny should accept this and it should work if my code does not work? Or is there written anywhere that shiny does not plot ggsurvplot objects? Can you explain me this, please? @Gregor de Cillia
    – scarface
    Mar 14, 2018 at 7:25
  • @GregordeCillia it does not work. The labels still does not get printed.
    – scarface
    Mar 14, 2018 at 16:22

1 Answer 1

0

I just tried your test data and didn't run into any issues if I convert the output of ggsurvplot into a ggplot object right away.

Can you pleaso tell me whether the labels are visible when you execute this code?

library(shiny)
library(survival)
library(survminer)
library(directlabels)

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)
)
data_survival_curve <- data
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)

ggsp <- 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) 
ggsp2 <- ggsp$plot + geom_dl(aes(label = time_period), method = list("last.points"), 
                             cex = 0.8)

shinyApp(
  fluidPage(plotOutput("plot")),
  function(input, output, session){
    output$plot <- renderPlot({ggsp2})
  }
)

However, If I try to execute the code the way you posted it (skipping the $plot), I get the following error.

ggsp2 <- ggsp$plot + geom_dl(aes(label = time_period), method = list("last.points"), 
                             cex = 0.8)

Error in ggsp + geom_dl(aes(label = time_period), method = list("last.points"), : non-numeric argument to binary operator In addition: Warning message: Incompatible methods ("+.ggsurv", "+.gg") for "+"

This makes sense to me since the output of ggsurvplot is in fact of class ggsurvplot

class(ggsp)
## [1] "ggsurvplot" "ggsurv"     "list"

To be honest, I'm quite puzzled by the fact that you didn't get similar error messages. I downloaded the latest CRAN versions of the packages above except ggplot2, where I use the development version (2.2.1.9000).

2
  • Now, the way I edited your code, it works. When I rewrote plot code in my application to this structure: ggsp <- 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) ggsp2 <- ggsp$plot + geom_dl(aes(label = time_period), method = list("last.points"), cex = 0.8) it still does not work anyway. I think that there are few other crucial lines of code in my application that differs from yours. Can we exchange any contact and discuss?
    – scarface
    Mar 15, 2018 at 9:50
  • Ye, I know what is the problem. You set data_survival_curve$time_period before shinyApp starts. I set this in shinyApp function and this causes problems. When you move this line data_survival_curve$time_period <- cut(as.numeric(data_survival_curve$transplant_year), seq(2002, 2020, 2), include.lowest = T) to the function in 2nd argument of shinyApp(), you will see that it does not work. I have to do it this way because I have to set time_period from user input - it is dynamically generated.
    – scarface
    Mar 15, 2018 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.