当我运行此代码时,ggplot'ventoutcomes'将不会显示,它会显示为空白图表。当我在RStudio中为ventoutcomes运行单独的代码时,它成功创建了图形。有疑难解答的想法吗?由于患者的机密性,我无法发布数据集。谢谢
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)
我想我在这行上遇到了麻烦:
Figure %>% drop_na(Requirement) %>%
我认为您对导致问题的原因是正确的,而且我很确定这是环境问题。就是说,我的意思是说我看到Figure
是通过脚本中的read.csv()
分配的,但它在应用程序的server
部分之外。这实际上意味着它将在运行时从server
中隐藏。
尝试将这些行移动到server
内:
Figure<-read.csv("Figure.csv",header=TRUE)
Figure_Event<-read.csv("Figure_Event.csv",header=TRUE)
这会将它们加载到应用程序的环境中,并使其可访问。如此说来,请确保要加载的任何其他数据集在server
部分中也具有该代码。
EDIT:我还注意到您有两个呼叫server<- shinyserver(function(input,output) {
。 See here提供了一些示例和基础知识,但您只应具有一个server
函数,并将所有output$
对象放入其中。这是一个伪代码示例,它可能为您显示:
# 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({...
})