I would like to plot the ggplot objects (bar and line plots) in the main panel as per the user options from checkboxGroupInput. When I tried the following code with fileInput in reactive of server logic, it returns Error in .getReactiveEnvironment()$currentContext() Could you please help me to identify where the code turns into error and suggest me a fix. Thanks
library(shiny)
library(ggplot2)
library(easyGgplot2)
# Creating dummy data to write a file to be used in fileinput
patient <- cbind.data.frame(seq(1:14), matrix(sample(1:100, 84), ncol = 6))
colnames(patient) <- c("DAYS", "PHYSICAL_ACTIVITY", "SMOKING", "ALCOHOL_INTAKE",
"HYDRATION", "SLEEP", "Total_score")
write.csv(patient, file = "patient.csv")
# Shiny uicode starts
ui <- fluidPage(
titlePanel("Data Plot"),
sidebarLayout(
sidebarPanel(
fluidRow(
column(6,
checkboxGroupInput(
"checkGroup", "Parameters",
list("PHYSICAL ACTIVITY", "SLEEP"),
selected = "PHYSICAL ACTIVITY"))),
fluidRow(
fileInput(
"file1", "Choose Data sheet",
multiple = TRUE,
accept = c("text/csv", "text/comma-separated-values,text/plain", ".csv")))),
mainPanel(plotOutput("plots"))
))
server <- function(input, output) {
patient <- reactive({
req(input$file1)
read.csv(input$file1$datapath, header = T)
})
plot_one <- ggplot(data = patient(), aes(x = DAYS, y = PHYSICAL_ACTIVITY)) +
geom_bar(
stat = "identity",
aes(fill = PHYSICAL_ACTIVITY <= median(PHYSICAL_ACTIVITY)),
show.legend = F) +
scale_fill_manual(values = c("steelblue", "red")) +
labs(title = "PHYSICAL ACTIVITY (STEPS)", x = NULL, y = NULL) +
theme_minimal()
plot_two <- ggplot(data = patient(), aes(x = DAYS, y = SLEEP)) +
geom_line(colour = "black", size = 1) +
geom_point(
size = 3,
aes(colour = cut(SLEEP, c(-Inf, summary(SLEEP)[[2]], summary(SLEEP)[[5]], Inf))),
show.legend = F) +
scale_color_manual(values = c("red", "orange", "green")) +
labs(title = "SLEEP (hrs)", x = NULL, y = NULL) +
theme_minimal()
list.of.plots <- list(`PHYSICAL ACTIVITY` = plot_one, SLEEP = plot_two)
output$plots <- renderPlot({
do.call(ggplot2.multiplot, c(list.of.plots[input$checkGroup], cols = 1))
})
}
shinyApp(ui, server)