Reputation: 33
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) %>%
Upvotes: 0
Views: 336
Reputation: 13793
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({...
})
Upvotes: 1