r - rShiny 中的空白 ggplot 图故障排除
问题描述
当我运行此代码时,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
。
编辑:我还注意到您有两次致电server<- shinyserver(function(input,output) {
. 有关一些示例和基础知识,请参见此处,但您应该只有一个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({...
})
推荐阅读
- c# - 在 Azure 认知搜索中使用正则表达式(lucene 语法)搜索单个字符
- javascript - Vue Nuxtjs 生成导入语法错误的 nuxt 文件,我该如何解决?
- oracle - Oracle中一个通知事件消耗多少内存?
- python - 请求帮助使子类访问python中的主类
- batch-file - 如何在目录中循环抛出文件名,然后提取它们的名称以获取 substirng 值
- c++ - 实现双向链表
- python - 为什么即使存在需求,从带有鸡蛋片段的 git 安装 pipenv 模块也会失败?
- vb6 - 拒绝访问。未授权使用此应用程序 - 尝试实例化 com+ 对象时出现间歇性错误
- python - 基于值的列计算
- python - 根据数据框中另一列的值添加一列