0

When I run this code, the ggplot 'ventoutcomes' will not show up, it comes up as a blank graph. When I run the separate code for ventoutcomes in RStudio, it successfully creates the graph. Ideas for troubleshooting? I cannot post the datasets due to patient confidentiality. Thank you

library(shiny)
library(semantic.dashboard)
library(ggplot2)
library(plotly)
library(DT)
##devtools::install_github("ropensci/plotly")
##packages needed to create the dashboard


setwd ("/Users/k/Desktop/HMHCOVID")
## set working directory. You can also do it in R Studio under Session...Set Working Directory. If you do that, delete this line. 


library(dplyr)
library(ggplot2)
library(tidyr)
##load the packages 

cols<-c("AmbientAir"="lightblue","LowFlow"="blue","HighFlow"="orange","NIPPV"="purple","MechanicalVentilation"="red","ECMO"="black")
##this is setting the colors

Figure<-read.csv("Figure.csv",header=TRUE)
Figure_Event<-read.csv("Figure_Event.csv",header=TRUE)
##load files

Figure_Event %>% filter(Event=="Discharged" | Event=="Death") %>% filter(Patient<26)-> Figure_Event
##filter the Figure_Event file to relevant rows

##

##Figure <- Figure[!apply(is.na(Figure) | Figure == "", 1, all),]
##remove blank and NA data

##above is the code for ventilation outcomes, to prepare the data



##setwd("/Users/k/Desktop/HMHCOVID")
##set the working directory

library(readr)
library(dplyr)
library(ggplot2)
library(scales)
library(shiny)
library(grid)
library(tidyverse)
library(stringr)
library (lubridate)
library (tinytex)
#install.packages("anytime")
#install.packages("devtools")
library(devtools)
#load the packages needed for donors_time graph


o2outcomes <- read.csv(
  file="patients_transfused.csv",
  stringsAsFactors = FALSE)

o2outcomes <- o2outcomes[!apply(is.na(o2outcomes) | o2outcomes == "", 1, all),]
##remove blank and NA data

view (o2outcomes)

(names(o2outcomes)[10] <- "transfusion_date")
##rename column 8 to see if it helps with transferring from 'character' to 
'date'


o2outcomes <- transform(o2outcomes,
                        td = anytime::anytime(gsub("/20 ","/2020 ",transfusion_date))
)

o2outcomes$transfusion_date <- parse_date_time(o2outcomes$transfusion_date,
                                               orders = "%m/%d/%y %H%M")
##convert data from character into date


o2outcomes <- o2outcomes[order(o2outcomes$transfusion_date),]
##sort by date


o2outcomes <- o2outcomes[order(o2outcomes$transfusion_date), ]
o2outcomes$ID <- seq_len(nrow(o2outcomes))
##create ID to track cumulative cases

##above is the code preparing the data for the donors_time graph




library(plotly)
fig <- plot_ly(midwest, x = ~percollege, color = ~state, type = "box")
fig

ui <- dashboardPage(
  dashboardHeader(color = "blue",title = "HMH Convalescent Plasma Transfusion Therapy", inverted = TRUE),
  dashboardSidebar(
    size = "thin", color = "teal",
    sidebarMenu(
      menuItem(tabName = "main", "Recipient Data", icon = icon("home")),
      menuItem(tabName = "extra", "Donor Data", icon = icon("table"))
    )
  ),
  dashboardBody(
    tabItems(
      selected = 1,
      tabItem(
        tabName = "main",
        fluidRow(
          box(width = 8,
              title = "Graph 1",
              color = "green", ribbon = TRUE, title_side = "top left",
              column(width = 8,
                     plotOutput("ventoutcomes")
              )
          ),
          box(width = 8,
              title = "Graph 2",
              color = "red", ribbon = TRUE, title_side = "top right",
              column(width = 8,
                     plotOutput("donors_time")
                    
              )
          )
        )
      ),
      tabItem(
        tabName = "extra",
        fluidRow(
          dataTableOutput("carstable")
        )
      )
    )
  ), theme = "cerulean"
)

server <- shinyServer(function(input, output) {  
  output$ventoutcomes <- renderPlot({  
    Figure %>% drop_na(Requirement) %>% ggplot() + geom_line(aes(x=Day,y=Patient,group=Patient,col=Requirement),lwd=2.5) + scale_color_manual(values=cols) + scale_y_continuous(trans="reverse",breaks=seq(1,50,1))+scale_x_continuous(breaks=seq(-14,24,1))+theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank(),panel.background = element_blank()) + geom_vline(xintercept=0) + geom_point(data=Figure_Event, aes(x=Day,y=Patient,shape=Event),size=4) + scale_shape_manual(values=c(15,22)) + scale_color_discrete(breaks=c("AmbientAir","LowFlow","HighFlow", "NIPPV","MechanicalVentilation","ECMO"))
  })
})
  
server <- shinyServer(function(input, output) {  
  output$donors_time <- renderPlot({  
    ggplot(data=o2outcomes, aes(x=transfusion_date, y=ID, group=1)) +
      geom_line()+
      ylim(0, 100)+labs(y= "Number of Patients Transfused", x = "Transfusion Date")+ggtitle("Number of Patients Transfused Over Time") 
    ##create line graph 
  })
})

shinyApp(ui, server)

I think I'm having trouble with this line:

Figure %>% drop_na(Requirement) %>% 
Phil
  • 7,287
  • 3
  • 36
  • 66
  • 2
    Is Figure.csv in your shiny directory? Does it run if you make an app that only has that plot? We can’t run this app because we don’t have your folders or files. Are you able to make this reproducible? Explained here: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – bs93 May 21 '20 at 16:29

1 Answers1

1

I think you're right on what is causing the issue, and I'm pretty sure this is an environment problem. By that, I mean that I see Figure is assigned via read.csv() in your script, but it's "outside" the server part of your app. This effectively means it will be hidden from the server when running.

Try moving these lines to be within server:

Figure<-read.csv("Figure.csv",header=TRUE)
Figure_Event<-read.csv("Figure_Event.csv",header=TRUE)

That should load them into the environment of the app and make them accessible. With that being said, be sure any other datasets that you are loading have that code in the server part as well.

EDIT: I also noticed you have two calls to server<- shinyserver(function(input,output) {. See here for some examples and basics, but you should only have one server function and put all your output$ objects in there. Here's a pseudocode example of what it might look like for you:

# library calls
library(...)
library(...)

ui <- dashboardPage(...

    # ui stuff here already

)


server <- shinyserver(function(input,output) {

    # your prep code goes here.  That's EVERYTHING about preparation
    # of your data...load all datasets here or it will not be visible to the app.
    # almost consider this part your console commands to the app that should
    # be there before the program is run (or during).
    # so, any user-defined functions also go here, or reading of files...
    # if you have to add some columns to that data after reading.. .goes here too

    output$ventoutcomes <- renderPlot({...
        })

    output$donors_time <- renderPlot({...
        })
chemdork123
  • 12,369
  • 2
  • 16
  • 32
  • Would it be possible for you to show me how to execute that in my code? I'm new to RShiny and having trouble figuring out how to reorder it. Thanks for your help – htxepidemiologist May 22 '20 at 16:31
  • 1
    Literally take your code and if it's not a `library` call, it should either be in `server` or in `ui`. The `ui` stuff seems fine to me. All the code before the `ui` now (with the exception of `library` calls) should be moved to within `server <- shinyserver(function(input,output) {`. It should basically read: (1) `server<-shinyserver(...`, then (2) your prep code stuff, then (3) `output$ventoutcomes`, and (4) `output$donors_time`. Note that you only should have one `server<-shinyserver(...` call. – chemdork123 May 24 '20 at 05:35
  • You'll see some edits to the answer to see what I'm talking about – chemdork123 May 24 '20 at 05:42
  • thanks very much that all makes sense. appreciate your time – htxepidemiologist May 26 '20 at 14:17