简体   繁体   中英

R Shiny not plotting some ggplot2 layers

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)

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 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM