Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Problem with survival function in Shiny App

$
0
0

Recently I'm using the survival package in R, in order to be able to better measure the waiting time of patients in the Emergency Department of my Hospital, what I have achieved. However, it's complex to show the results and that can be understood by third parties from RStudio, so I'm developing a Shiny App to show the results without having to show the code and be able to modify certain elements quickly When I create the app, I have the problem that when I request that you build a dataframe with the quantiles, I get the error: "Error in rep: invalid 'times' argument" in R#200. I have reviewed the code on multiple occasions, but I still can't find a solution. I enclose the complete code Thank you


ui <- fluidPage(
    titlePanel("Prototipo Tiempos de Urgencia"),

    sidebarLayout(
                 sidebarPanel(
                   selectInput(inputId = "triage",
                               label = "Triage",
                               choices =c("Todos",listadotriage),
                               selectize = FALSE,
                               selected = "Todos"),
                   selectInput(inputId = "tiempo",
                               label = "Tiempo a analizar",
                               choices = c("Diferencia entre Hora de atención y hora de triage" = "60",
                                           "Diferencia ente hora de atencón y hora de admisión" = "61",
                                           "Diferencia ente hora de atencón y hora de alta" = "62",
                                           "Diferencia ente hora de admision y hora de alta" = "63",
                                           "Diferencia ente hora de admision y hora de triage" = "64"),
                               selectize = FALSE,
                               selected = "Diferencia entre Hora de atención y hora de triage"),
                  selectInput(inputId = "atencion",
                               label = "Área Atención",
                               choices = c("Todos",listadoatencion),
                               selectize = FALSE,
                               selected = "Todos"),
                   selectInput(inputId = "alta",
                               label = "Tipo de Alta",
                               choices = c("General" = "74",
                                           "Hospitalizado" = "72",
                                           "Altas" = "73"),
                               multiple = FALSE,
                               selected = "General"),
                   dateRangeInput(inputId = "fecha",
                                  label = "Intervalo de fechas",
                                  format = "dd-mm-yyyy",
                                  start = "2019-11-01",
                                  end = "2019-11-30",
                                  weekstart = 1,
                                  language= "es",
                                  separator = "a")
                 ),
                 mainPanel = dataTableOutput(outputId = "tabla1")

      ))


server <- function(input, output) {

    output$tabla1 <- renderDataTable({
      if (input$triage=="Todos") {
          dat1<-subset(dat1,ifelse(input$atencion!="Todos",dat1$AREA_ATENCION==input$atencion & dat1$fechanormal>=input$fecha[1] & dat1$fechanormal<=input$fecha[2],
                                   dat1$fechanormal>=input$fecha[1] & dat1$fechanormal<=input$fecha[2]))

          timesevent<-as.integer(input$tiempo)

          event<-as.integer(input$alta)

          dat3<-dat1[,c(30,timesevent,event)]

          gral<-Surv(dat3[[2]] ,event= as.numeric(dat3[[3]]))
          fit1a<-survfit(gral ~ Medico, data = dat3)
          d1<-as.data.frame(quantile(fit1a,c(0.25,0.5,0.75,0.9,1),conf.int = FALSE)) # estadísticas sobre tiempo de demora según modelo
          data1<-as.data.frame(fit1a$strata)
          setDT(data1,keep.rownames = TRUE)
          data1$rn<-gsub("Medico=","",data1$rn)
          colnames(data1)<-c("Médico","Consultas")
          setDT(d1,keep.rownames = TRUE)[]
          d1$rn<-gsub("Medico=","",d1$rn)
          colnames(d1)<-c("Médico","P.25","P.50","P.75","P.90","P.100")
          datatotal1<-merge(data1,d1,by="Médico")
          datatable(data=datatota1l,caption =paste("Tiempos de atención en Servicio de Urgencia por Médico, por Atención en Servicio",input$atencion,"durante","el período",format(input$fecha[1], format= "%d-%m-%Y"),"a",format(input$fecha[2], format= "%d-%m-%Y"), sep=""), 
                    rownames = FALSE,extensions= "Buttons",options=list(pageLength=40,dom="Bfrtip", buttons = c("print")) )
      } else {
          dat1<-subset(dat1,dat1$Triage==input$triage)
          dat1<-subset(dat1,ifelse(input$atencion!="Todos",dat1$AREA_ATENCION==input$atencion & dat1$fechanormal>=input$fecha[1] & dat1$fechanormal<=input$fecha[2],
                                   dat1$fechanormal>=input$fecha[1] & dat1$fechanormal<=input$fecha[2]))


         timesevent<-as.integer(input$tiempo)

         event<-as.integer(input$alta)

         dat3<-dat1[,c(30,timesevent,event)]

          gral<-Surv(dat3[[2]] ,event= as.numeric(dat3[[3]])) ~ Medico
          fit1b<-survfit(gral ~ Medico, data = dat3)
          d2<-as.data.frame(quantile(fit1b,c(0.25,0.5,0.75,0.9,1),conf.int = FALSE)) # estadísticas sobre tiempo de demora según modelo
          data2<-as.data.frame(fit1b$strata)
          setDT(data2,keep.rownames = TRUE)
          data2$rn<-gsub("Medico=","",data2$rn)
          colnames(data2)<-c("Médico","Consultas")
          setDT(d2,keep.rownames = TRUE)[]
          d2$rn<-gsub("Medico=","",d2$rn)
          colnames(d2)<-c("Médico","P.25","P.50","P.75","P.90","P.100")
          datatotal2<-merge(data2,d1,by="Médico")
          datatable(data=datatotal2,caption =paste("Tiempos de atención en Servicio de Urgencia por Médico, por Categorización",input$triage,"por Atención en Servicio",input$atencion,"durante","el período",format(input$fecha[1], format= "%d-%m-%Y"),"a",format(input$fecha[2], format= "%d-%m-%Y"), sep=""), 
                    rownames = FALSE,extensions= "Buttons",options=list(pageLength=40,dom="Bfrtip", buttons = c("print")) ) 
      }
    })
}

shinyApp(ui = ui, server = server)```

Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>