Reputation: 341
Below are the headers of my dataframe (appliance) along with it's structure.
Home Season Day Timein24 countRun Run
19262 1 Autumn 1 1 8 NA
19263 1 Autumn 1 1 8 NA
19264 1 Autumn 1 1 8 NA
19265 1 Autumn 1 1 8 NA
19266 1 Autumn 1 2 8 NA
19267 1 Autumn 1 2 8 NA
19268 1 Autumn 1 2 8 NA
19293 1 Autumn 1 2 8 NA
19290 1 Autumn 1 3 8 NA
19295 1 Autumn 1 3 8 NA
19271 1 Autumn 1 3 8 NA
19294 1 Autumn 1 3 8 NA
19272 1 Autumn 1 4 8 NA
19273 1 Autumn 1 4 8 NA
19274 1 Autumn 1 4 8 NA
19275 1 Autumn 1 4 8 NA
19236 1 Autumn 1 5 8 NA
19237 1 Autumn 1 5 8 NA
19278 1 Autumn 1 5 8 NA
19279 1 Autumn 1 5 8 NA
str(appliance)
'data.frame': 51072 obs. of 6 variables:
$ Home : Factor w/ 19 levels "1","2","3","5",..: 1 1 1 1 1 1 1 1 1 1 ...
$ Season : chr "Autumn" "Autumn" "Autumn" "Autumn" ...
$ Day : Factor w/ 7 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...
$ Timein24: Factor w/ 12 levels "1","2","3","4",..: 1 1 1 1 2 2 2 2 3 3 ...
$ countRun: num 8 8 8 8 8 8 8 8 8 8 ...
$ Run : num NA NA NA NA NA NA NA NA NA NA ...
in $Season, I've four seasons and then in $countRun the values are 4,8,12. Also pasting the UI and Server of the R Shiny I've tried so far. I'm facing issues with the "if" statement. Please see:
ui = navbarPage("Flexible Demand",
tabPanel("Appliance1",
fixedRow(column(width = 12, offset = 5, titlePanel("Appliance1"))),
fixedRow(column(width = 6, selectizeInput("Home", label = "Select Home",
choices = c(sort(unique(appliance$Home))),
selected = c(sort(unique(appliance$Home)))[1],
multiple = F, width = "100%")),
column(width = 6, selectizeInput("Season", label = "Select Season",
choices = c(sort(unique(appliance$Season))),
selected = c(sort(unique(appliance$Season)))[1],
multiple = F, width = "100%"))),
fixedRow(column(width = 6, sliderInput("Day1C", label = "Day 1 - Hour to Run",
min = 1,max = 12, value = 2)),
column(width = 6, selectizeInput("Day1", label = "Day 1 - Duration (Hours)",
choices = c(sort(unique(appliance$countRun))),
selected = c(sort(unique(appliance$countRun)))[1],
multiple = F, width = "100%")),
mainPanel(DT::dataTableOutput("day1")))))
server = function(input, output, session){
output$day1 = DT::renderDataTable({
day1 = reactive({appliance[appliance$Home %in% input$Home &
appliance$Season %in% input$Season,]})
if (appliance$Timein24 != input$Day1C) {
appliance$Run=0
} else{appliance$Run=1}
day1()
})
}
shinyApp(ui = ui, server = server)
What I'm trying to achieve by this is to have values of Run reactive based on whatever I choose in sliderInput "Day1C" and column "Timein24". for e.g. if I choose my "Day 1- Hour to Run" i.e. sliderInput "Day1C" as 2 then the Run for all 2s under "Timein24" column of the dataframe should be 1
and for others it should be 0
.
I hope I'm making sense. Let me know if you want any further details.
Upvotes: 0
Views: 640
Reputation: 160982
This is a shot, not fully tested:
server = function(input, output, session){
day1 = reactive({
req(input$Home, input$Season)
appliance[appliance$Home %in% input$Home &
appliance$Season %in% input$Season,]
})
output$day1 = DT::renderDataTable({
req(day1(), input$Day1C)
x <- day1()
x$Run <- ifelse(x$Timein24 == input$Day1C, 1, 0)
x
})
}
Upvotes: 1