Reputation: 8404
Im trying to create the following shiny app below but when Im tryin to subset based on my widget I get: Error in $: object of type 'closure' is not subsettable
## app.R ##
#Libraries needed fot the app
library(shiny)
library(shinydashboard)
library(ggplot2)
library(plotly)
library(tidyr)
library(hrbrthemes)
Targets2<-structure(list(Week = c(1, 1, 7, 7, 16, 16, 14, 14, 20, 20, 15,
15, 7, 7, 2, 2, 10, 10, 15, 15), Type = c("Cumilative target",
"Actual target", "Cumilative target", "Actual target", "Cumilative target",
"Actual target", "Cumilative target", "Actual target", "Cumilative target",
"Actual target", "Cumilative target", "Actual target", "Cumilative target",
"Actual target", "Cumilative target", "Actual target", "Cumilative target",
"Actual target", "Cumilative target", "Actual target"), Count = c(5,
7, 123, 18, 348, 33, 298, 37, 448, 52, 323, 29, 123, 18, 13,
12, 198, 8, 323, 29)), row.names = c(NA, -20L), class = c("tbl_df",
"tbl", "data.frame"))
#The ui part contains the user interface
ui <- dashboardPage(
#Contains the title
dashboardHeader(title = "Clinics Dashboard"),
dashboardSidebar(
#Contains the sidebar with the date range input
selectInput("weeks", "Select Week(s):",
choices=unique(Targets2$Week ),selected=unique(Targets2$Week ),
multiple=T)
),
#the body contains the 2 plots
dashboardBody(
#1st plot
plotlyOutput("plot")
)
)
#the server part contains all the background code that is displayed in the ui part
server <- function(input, output) {
Targets2<-reactive({
Targets2 <- subset(Targets2, Targets2$Week %in% input$weeks)
})
#the code for creating the 1st plot
output$plot<-renderPlotly({
p <-
ggplot(Targets2(), aes(x = Week, y = Count, fill = Type))+
geom_area(alpha = 0.6 , size = 0.5, colour = "white", stat = "identity", orientation = "x") +
theme_ipsum() +
theme(legend.position = "bottom")
p <- p+labs(title = "Figure 1: Weekly Cumulative Projected Enrollment vs Weekly Cumulative Actual Enrollment",
subtitle = "Cum Weekly Projected Enrollment/Cum Weekly Actual Enrollment")
ggplotly(p)
})
}
shinyApp(ui, server)
Upvotes: 0
Views: 708
Reputation: 388982
Change the name of the reactive
function or change the name of the dataset. They both share the same name hence, the error.
library(shiny)
library(shinydashboard)
library(ggplot2)
library(plotly)
library(tidyr)
library(hrbrthemes)
Targets<-structure(list(Week = c(1, 1, 7, 7, 16, 16, 14, 14, 20, 20, 15,
15, 7, 7, 2, 2, 10, 10, 15, 15), Type = c("Cumilative target",
"Actual target", "Cumilative target", "Actual target", "Cumilative target",
"Actual target", "Cumilative target", "Actual target", "Cumilative target",
"Actual target", "Cumilative target", "Actual target", "Cumilative target",
"Actual target", "Cumilative target", "Actual target", "Cumilative target",
"Actual target", "Cumilative target", "Actual target"), Count = c(5,
7, 123, 18, 348, 33, 298, 37, 448, 52, 323, 29, 123, 18, 13,
12, 198, 8, 323, 29)), row.names = c(NA, -20L), class = c("tbl_df",
"tbl", "data.frame"))
#The ui part contains the user interface
ui <- dashboardPage(
#Contains the title
dashboardHeader(title = "Clinics Dashboard"),
dashboardSidebar(
#Contains the sidebar with the date range input
selectInput("weeks", "Select Week(s):",
choices=unique(Targets2$Week ),selected=unique(Targets2$Week ),
multiple=T)
),
#the body contains the 2 plots
dashboardBody(
#1st plot
plotlyOutput("plot")
)
)
#the server part contains all the background code that is displayed in the ui part
server <- function(input, output) {
Targets2<-reactive({
Targets2 <- subset(Targets, Targets$Week %in% input$weeks)
})
#the code for creating the 1st plot
output$plot<-renderPlotly({
p <-
ggplot(Targets2(), aes(x = Week, y = Count, fill = Type))+
geom_area(alpha = 0.6 , size = 0.5, colour = "white", stat = "identity", orientation = "x") +
theme_ipsum() +
theme(legend.position = "bottom")
p <- p+labs(title = "Figure 1: Weekly Cumulative Projected Enrollment vs Weekly Cumulative Actual Enrollment",
subtitle = "Cum Weekly Projected Enrollment/Cum Weekly Actual Enrollment")
ggplotly(p)
})
}
shinyApp(ui, server)
Upvotes: 2