我试图使用选择小部件绘制蜘蛛图,以便它更新已选择不同组的图表。 我能够毫无问题地生成情节并做出选择。但是,我不断收到警告:如果:参数长度为零,则错误。
我在下面粘贴可重现的代码 ui.R 和 server.R:
ui.R
library("dplyr")
library("haven")
library("tidyverse")
library("ggplot2")
library("plotly")
df <- data.frame(
SUBJID = c("101-001", "101-001", "101-001", "101-001", "101-001", "101-002", "101-002", "101-006", "101-006", "101-007", "101-007", "302-001", "302-001", "303-001", "303-001", "303-001"),
ADY = c(-4, 37, 78, 121, 163, -7, 41, -20, 38, -7, 42, -10, 42, -14, 41, 78),
PCHG = c(0, 0, -12.444523, -12.444322, -25.482626, 0, 15.789474, 0, -3.22666, 0, 7.55533, 0, 18.076923, 0, -2.53434, 22.666667),
TRTA = c("6mg", "6mg", "6mg", "6mg", "6mg", "12mg", "12mg", "24mg", "24mg", "24mg", "24mg", "40mg", "40mg", "40mg", "40mg", "40mg"),
TRTAN = c(11, 11, 11, 11, 11, 12, 12, 14, 14, 14, 14, 15, 15, 15, 15, 15),
BOR = c("PR", "PR", "PR", "PR", "PR", "PD", "PD", "PD", "PD", "SD", "SD", "PD", "PD", "SD", "SD", "SD")
)
shinyUI(navbarPage(
"Patient Profiles",
tabPanel(
"Spider Plot",
fluidPage(
uiOutput("selected_plot_trt"),
plotlyOutput("plot", height = 800, width = 1800)
)
),
collapsible = TRUE
))
服务器.R
library("dplyr")
library("haven")
library("tidyverse")
library("ggplot2")
library("plotly")
df <- data.frame(
SUBJID = c("101-001", "101-001", "101-001", "101-001", "101-001", "101-002", "101-002", "101-006", "101-006", "101-007", "101-007", "302-001", "302-001", "303-001", "303-001", "303-001"),
ADY = c(-4, 37, 78, 121, 163, -7, 41, -20, 38, -7, 42, -10, 42, -14, 41, 78),
PCHG = c(0, 0, -12.444523, -12.444322, -25.482626, 0, 15.789474, 0, -3.22666, 0, 7.55533, 0, 18.076923, 0, -2.53434, 22.666667),
TRTA = c("6mg", "6mg", "6mg", "6mg", "6mg", "12mg", "12mg", "24mg", "24mg", "24mg", "24mg", "40mg", "40mg", "40mg", "40mg", "40mg"),
TRTAN = c(11, 11, 11, 11, 11, 12, 12, 14, 14, 14, 14, 15, 15, 15, 15, 15),
BOR = c("PR", "PR", "PR", "PR", "PR", "PD", "PD", "PD", "PD", "SD", "SD", "PD", "PD", "SD", "SD", "SD")
)
shinyServer(function(input, output, session){
plot_data <- df %>%
select(SUBJID, ADY, PCHG, TRTA, TRTAN, BOR) %>%
arrange(TRTAN)
output$selected_plot_trt <- renderUI({
selectInput("selected_plot_trt",
"Treatment Group",
choices = c("All", unique(sort(c(as.character(plot_data$TRTA))))))
})
output$plot <- renderPlotly({
if (input$selected_plot_trt != "All") {
plot_data <- plot_data[plot_data$TRTA == input$selected_plot_trt, ]
}
ggplotly(
ggplot(plot_data, aes(x = ADY, y = PCHG, group = SUBJID, color=reorder(TRTA, TRTAN)
)) +
theme_bw(base_size=12) +
ggtitle("Percent Change in Tumor from Baseline over Time") +
theme(plot.title = element_text(hjust = 0.5)) +
xlab("Analysis Relative Day") +
ylab("Percent Change from Baseline") +
geom_line() +
geom_point(aes(shape = BOR, color=BOR), size = 2) +
scale_color_manual(name="",
values = c("6mg" = "#B03060", "12mg" = "#d62728",
"24mg" = "#2ca02c","40mg" = "#ffbb78"),
breaks = c("6mg", "12mg", "24mg",
"40mg"),
labels = c("6mg", "12mg", "24mg",
"40mg")) +
scale_shape_manual(name = "",
breaks = c("CR", "PR", "SD", "PD", "NA"),
values = c("SD"=16, "PD"=4, "PR"=18, "CR"=17, "NA"=4),
labels=c("CR"="Complete Response", "PR"="Partial Response",
"SD"="Stable Disease", "PD"="Progressive Disease", "NA"="Not Available"))
)
})
})
想知道是什么导致了警告以及如何消除它? 谢谢!!
使用
req()
消除警告。
output$plot <- renderPlotly({
req(input$selected_plot_trt)
if (input$selected_plot_trt != "All") {
plot_data <- plot_data[plot_data$TRTA == input$selected_plot_trt, ]
}
ggplotly(
ggplot(plot_data, aes(x = ADY, y = PCHG, group = SUBJID, color=reorder(TRTA, TRTAN)
)) +
theme_bw(base_size=12) +
ggtitle("Percent Change in Tumor from Baseline over Time") +
theme(plot.title = element_text(hjust = 0.5)) +
xlab("Analysis Relative Day") +
ylab("Percent Change from Baseline") +
geom_line() +
geom_point(aes(shape = BOR, color=BOR), size = 2) +
scale_color_manual(name="",
values = c("6mg" = "#B03060", "12mg" = "#d62728",
"24mg" = "#2ca02c","40mg" = "#ffbb78"),
breaks = c("6mg", "12mg", "24mg",
"40mg"),
labels = c("6mg", "12mg", "24mg",
"40mg")) +
scale_shape_manual(name = "",
breaks = c("CR", "PR", "SD", "PD", "NA"),
values = c("SD"=16, "PD"=4, "PR"=18, "CR"=17, "NA"=4),
labels=c("CR"="Complete Response", "PR"="Partial Response",
"SD"="Stable Disease", "PD"="Progressive Disease", "NA"="Not Available"))
)
})