2017-09-28 5 views
0

私は行または個々の値で反応性データフレームをサブセット化する方法をいくつか試してきましたが、これまで運がなかった。インターネット上のほとんどの例は、私のためにうまく動作する列サブセットや、ユーザー入力に基づくサブセット(列単位)に関連しています。 私の反応的なデータフレームがdemand()なら、私は試しました:demand()[1,]; demand()["2017",]; demand()[1,1] - >運がない。Shinyの反応性データフレームの行または個々の値のサブセット

私が手にエラーがWarning: Error in .getReactiveEnvironment()$currentContext: Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

あなたは、助けてくださいことはできますか?

以下のコードを参照してください。どうもありがとう、パベル

library(shiny) 
library(DT) 

ui <- fluidPage(
    fluidRow(
    column(width = 6, 
      headerPanel(title = h4(strong("Change the Demand"))), 
      wellPanel(
      navlistPanel(
       tabPanel(title = "Professional", 
         sliderInput(inputId = "dem_prof_2017", label = "Demand 2017 % increase", min = -100, max = 100, value = 0, step = 5), 
         sliderInput(inputId = "dem_prof_2018", label = "Demand 2018 % increase", min = -100, max = 100, value = 0, step = 5), 
         sliderInput(inputId = "dem_prof_2019", label = "Demand 2019 % increase", min = -100, max = 100, value = 0, step = 5), 
         sliderInput(inputId = "dem_prof_2020", label = "Demand 2020 % increase", min = -100, max = 100, value = 0, step = 5) 
       ), 
       tabPanel(title = "Full Size", 
         sliderInput(inputId = "dem_fs_2017", label = "Demand 2017 % increase", min = -100, max = 100, value = 0, step = 5), 
         sliderInput(inputId = "dem_fs_2018", label = "Demand 2018 % increase", min = -100, max = 100, value = 0, step = 5), 
         sliderInput(inputId = "dem_fs_2019", label = "Demand 2019 % increase", min = -100, max = 100, value = 0, step = 5), 
         sliderInput(inputId = "dem_fs_2020", label = "Demand 2020 % increase", min = -100, max = 100, value = 0, step = 5) 
       ), 
       tabPanel(title = "Humidifier", 
         sliderInput(inputId = "dem_hum_2017", label = "Demand 2017 % increase", min = -100, max = 100, value = 0, step = 5), 
         sliderInput(inputId = "dem_hum_2018", label = "Demand 2018 % increase", min = -100, max = 100, value = 0, step = 5), 
         sliderInput(inputId = "dem_hum_2019", label = "Demand 2019 % increase", min = -100, max = 100, value = 0, step = 5), 
         sliderInput(inputId = "dem_hum_2020", label = "Demand 2020 % increase", min = -100, max = 100, value = 0, step = 5) 
       ), 
       tabPanel(title = "Hair Care", 
         sliderInput(inputId = "dem_hc_2017", label = "Demand 2017 % increase", min = -100, max = 100, value = 0, step = 5), 
         sliderInput(inputId = "dem_hc_2018", label = "Demand 2018 % increase", min = -100, max = 100, value = 0, step = 5), 
         sliderInput(inputId = "dem_hc_2019", label = "Demand 2019 % increase", min = -100, max = 100, value = 0, step = 5), 
         sliderInput(inputId = "dem_hc_2020", label = "Demand 2020 % increase", min = -100, max = 100, value = 0, step = 5) 
       ), 
       tabPanel(title = "New Category", 
         sliderInput(inputId = "dem_nc_2017", label = "Demand 2017 % increase", min = -100, max = 100, value = 0, step = 5), 
         sliderInput(inputId = "dem_nc_2018", label = "Demand 2018 % increase", min = -100, max = 100, value = 0, step = 5), 
         sliderInput(inputId = "dem_nc_2019", label = "Demand 2019 % increase", min = -100, max = 100, value = 0, step = 5), 
         sliderInput(inputId = "dem_nc_2020", label = "Demand 2020 % increase", min = -100, max = 100, value = 0, step = 5)) 
      ) #end of wellpanel 
      )), # end of column 
    column(width = 4, offset = 1, 
      fluidRow(dataTableOutput(outputId = "demand_table")), 
      fluidRow(verbatimTextOutput(outputId = "demand_2017")) 
    ) # end of column 
) # end of fluid row 
) # end of fluidPage 


server <- function(input, output) { 

    demand_actual <- 
    data.frame(
     year = c("2017", "2018", "2019", "2020"), 
     professional = round(runif(n = 4, min = 1000000, 10000000), 0), 
     full_size = round(runif(n = 4, min = 1000000, 10000000), 0), 
     humidifier = round(runif(n = 4, min = 1000000, 10000000), 0), 
     hair_care = round(runif(n = 4, min = 1000000, 10000000), 0), 
     new_category = round(runif(n = 4, min = 1000000, 10000000), 0) 
    ) 

    demand <- reactive({ 
    data.frame(
     year = c("2017", "2018", "2019", "2020"), 

     professional = demand_actual$professional * 
     (c(input$dem_prof_2017, input$dem_prof_2018, input$dem_prof_2019, input$dem_prof_2020)/100 + 1), 

     full_size = demand_actual$full_size * 
     (c(input$dem_fs_2017, input$dem_fs_2018, input$dem_fs_2019, input$dem_fs_2020)/100 + 1), 

     humidifier = demand_actual$humidifier * 
     (c(input$dem_hum_2017, input$dem_hum_2018, input$dem_hum_2019, input$dem_hum_2020)/100 + 1), 

     hair_care = demand_actual$hair_care * 
     (c(input$dem_hc_2017, input$dem_hc_2018, input$dem_hc_2019, input$dem_hc_2020)/100 + 1), 

     new_category = demand_actual$new_category * 
     (c(input$dem_nc_2017, input$dem_nc_2018, input$dem_nc_2019, input$dem_nc_2020)/100 + 1) 
    ) 
    }) # end of demand 

    demand_2017 <- reactive({demand()["2017",]}) # OR subset each element individually: demand()[1,2] OR demand()["2017",] 

    output$demand_table <- renderDataTable({demand()}) 
    output$demand_2017 <- textOutput(demand_2017()) 

}# end of server 

    shinyApp(ui = ui, server = server)  

答えて

0

試してみてください。

demand_2017 <- reactive({  
tmp <- demand() 
tmp[ tmp$year == "2017",]}) 
output$demand_2017 <- renderDataTable(demand_2017()) 

または使用

library(tidyverse) 
demand_2017 <- reactive({  
    filter(demand(), year == "2017") 
         }) 
+0

おかげdplyr、@Jimbou。これはうまくいきましたが、あなたが呼ぶ/サブセットしたい変数がある場合は、それらを個別にリアクティブ値として定義する必要がありますか? 'value_1_1 < - 反応性({ TMP < - オンデマンド() TMP [1,1]})' – Pavel

+0

あなたが正確に何を意味するかわかりません。しかし、部分集合についての本質的な疑問であると思われる。 Plaeseの外観はこちら[http://www.statmethods.net/management/subset.html]または[ここ](http://adv-r.had.co.nz/Subsetting.html) – Jimbou

関連する問題