I was trying to draw a spider plot with select widget so it updates the graph with different group has been chosen.
I was able to generate the plot and make selections with no issue. However I kept receiving the Warning: Error in if: argument is of length zero.
I am pasting the reproducible codes ui.R and server.R below:
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
))
server.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"))
)
})
})
wondering what’s causing the warning and how to get rid of it?
Thanks!!
New contributor
BingZhizhi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.