在R Shiny中使用反应性的“ if”语句

时间:2018-12-13 06:47:05

标签: r loops shiny

下面是我的数据框(设备)的标题及其结构。

      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(设备)

   '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 ...

在$ Season中,我有四个季节,然后在$ countRun中,值是4,8,12。还粘贴了到目前为止我尝试过的R Shiny的UI和服务器。我遇到“ if”语句的问题。请参阅:

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)

我要达到的目的是根据我在sliderInput“ Day1C”和“ Timein24”列中选择的值来获得“运行”响应的值。例如如果我选择“要运行的第1天”,即,将“ Day1C”输入为2,则数据帧“ Timein24”列下的所有 2s的运行应为1它应该是0

我希望我有道理。如果您需要更多详细信息,请告诉我。

1 个答案:

答案 0 :(得分:1)

这是镜头,未经充分测试:

Billings