Error in filter statement - Shiny Application











up vote
0
down vote

favorite












I've added a date filter to my Shiny App. Now I want to filter the data frame dynamically according to the date input.



I tried this as follows in ui.R:



fluidPage(
selectInput(
"analysis_period",
"analysie period:",
c(
"Last Year" = "LY",
"Past 90 days" = "ND",
"Past 30 days" = "TD",
"Last Week" = "LW"
)
)
)


In my server section I have:



date_input <- reactive({
if (input$analysis_period== "NT") {
as.Date(Sys.time()) - 90
}

else if (input$analysis_periodm == "DT") {
as.Date(Sys.time()) - 30
}

else if (input$analysis_periodum == "LW") {
as.Date(Sys.time()) - 7
}

else if (input$analysis_period == "LY") {
m = as.POSIXlt(as.Date(Sys.time()))
m$year = m$year - 1
m
}
})


Then I want to load the data frame and filter it using the filter:



data2 = data[, c('A', 'B', 'C')]
data2 <- filter(data2, date_input())


But when I start the app I always get the error:




Error in filter_impl(.data, quo) : 
Evaluation error: 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.).



What am I doing wrong here?










share|improve this question
























  • else if should start at the same row after closing bracket, i.e.: if(){}else if(){}else if....
    – zx8754
    Nov 19 at 10:25















up vote
0
down vote

favorite












I've added a date filter to my Shiny App. Now I want to filter the data frame dynamically according to the date input.



I tried this as follows in ui.R:



fluidPage(
selectInput(
"analysis_period",
"analysie period:",
c(
"Last Year" = "LY",
"Past 90 days" = "ND",
"Past 30 days" = "TD",
"Last Week" = "LW"
)
)
)


In my server section I have:



date_input <- reactive({
if (input$analysis_period== "NT") {
as.Date(Sys.time()) - 90
}

else if (input$analysis_periodm == "DT") {
as.Date(Sys.time()) - 30
}

else if (input$analysis_periodum == "LW") {
as.Date(Sys.time()) - 7
}

else if (input$analysis_period == "LY") {
m = as.POSIXlt(as.Date(Sys.time()))
m$year = m$year - 1
m
}
})


Then I want to load the data frame and filter it using the filter:



data2 = data[, c('A', 'B', 'C')]
data2 <- filter(data2, date_input())


But when I start the app I always get the error:




Error in filter_impl(.data, quo) : 
Evaluation error: 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.).



What am I doing wrong here?










share|improve this question
























  • else if should start at the same row after closing bracket, i.e.: if(){}else if(){}else if....
    – zx8754
    Nov 19 at 10:25













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I've added a date filter to my Shiny App. Now I want to filter the data frame dynamically according to the date input.



I tried this as follows in ui.R:



fluidPage(
selectInput(
"analysis_period",
"analysie period:",
c(
"Last Year" = "LY",
"Past 90 days" = "ND",
"Past 30 days" = "TD",
"Last Week" = "LW"
)
)
)


In my server section I have:



date_input <- reactive({
if (input$analysis_period== "NT") {
as.Date(Sys.time()) - 90
}

else if (input$analysis_periodm == "DT") {
as.Date(Sys.time()) - 30
}

else if (input$analysis_periodum == "LW") {
as.Date(Sys.time()) - 7
}

else if (input$analysis_period == "LY") {
m = as.POSIXlt(as.Date(Sys.time()))
m$year = m$year - 1
m
}
})


Then I want to load the data frame and filter it using the filter:



data2 = data[, c('A', 'B', 'C')]
data2 <- filter(data2, date_input())


But when I start the app I always get the error:




Error in filter_impl(.data, quo) : 
Evaluation error: 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.).



What am I doing wrong here?










share|improve this question















I've added a date filter to my Shiny App. Now I want to filter the data frame dynamically according to the date input.



I tried this as follows in ui.R:



fluidPage(
selectInput(
"analysis_period",
"analysie period:",
c(
"Last Year" = "LY",
"Past 90 days" = "ND",
"Past 30 days" = "TD",
"Last Week" = "LW"
)
)
)


In my server section I have:



date_input <- reactive({
if (input$analysis_period== "NT") {
as.Date(Sys.time()) - 90
}

else if (input$analysis_periodm == "DT") {
as.Date(Sys.time()) - 30
}

else if (input$analysis_periodum == "LW") {
as.Date(Sys.time()) - 7
}

else if (input$analysis_period == "LY") {
m = as.POSIXlt(as.Date(Sys.time()))
m$year = m$year - 1
m
}
})


Then I want to load the data frame and filter it using the filter:



data2 = data[, c('A', 'B', 'C')]
data2 <- filter(data2, date_input())


But when I start the app I always get the error:




Error in filter_impl(.data, quo) : 
Evaluation error: 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.).



What am I doing wrong here?







r shiny






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 20:11









Masoud

6,48241442




6,48241442










asked Nov 19 at 10:20









Tim1234

328




328












  • else if should start at the same row after closing bracket, i.e.: if(){}else if(){}else if....
    – zx8754
    Nov 19 at 10:25


















  • else if should start at the same row after closing bracket, i.e.: if(){}else if(){}else if....
    – zx8754
    Nov 19 at 10:25
















else if should start at the same row after closing bracket, i.e.: if(){}else if(){}else if....
– zx8754
Nov 19 at 10:25




else if should start at the same row after closing bracket, i.e.: if(){}else if(){}else if....
– zx8754
Nov 19 at 10:25












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










Ok I got it. When I executed the reactive inside a reactive expression or observer it works. Thank you guys for your help.



I did it like this:



  output$x1 <- DT::renderDataTable({
df <- data2
df<- filter(df, df$date >= dateinput())
df
})





share|improve this answer






























    up vote
    0
    down vote













    You have to use date_input in a reactive function, but if necessary you can get value outside by using isolate().



    But the goal of reactive function in Shiny it's to use isolate() at the minimum.



    I don't know if your data2 = data[, c('A', 'B', 'C')] is outside a reactive function.



    links:




    • https://shiny.rstudio.com/articles/isolation.html

    • https://shiny.rstudio.com/reference/shiny/1.0.1/isolate.html






    share|improve this answer





















    • Thanks phili_b. Yes, it's outside of the reactive function. I'm not quite sure yet what I need to adapt here. Did I understand correctly that I first have to isolate the query and that it is only used during a user interaction.
      – Tim1234
      Nov 19 at 10:36












    • yes, it's that :) " I first have to isolate the query and that it is only used during a user interaction"
      – phili_b
      Nov 19 at 10:43










    • Okay, and how can I use this? With a reactive function around the data query? I've never used isolation before. Sorry for asking :)
      – Tim1234
      Nov 19 at 10:48










    • I 've made a mistake you have to NOT isolate the query and that it is only used during a user interaction , except particular needs isolate() is only for particular purpose.
      – phili_b
      Nov 19 at 10:55











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372511%2ferror-in-filter-statement-shiny-application%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    Ok I got it. When I executed the reactive inside a reactive expression or observer it works. Thank you guys for your help.



    I did it like this:



      output$x1 <- DT::renderDataTable({
    df <- data2
    df<- filter(df, df$date >= dateinput())
    df
    })





    share|improve this answer



























      up vote
      1
      down vote



      accepted










      Ok I got it. When I executed the reactive inside a reactive expression or observer it works. Thank you guys for your help.



      I did it like this:



        output$x1 <- DT::renderDataTable({
      df <- data2
      df<- filter(df, df$date >= dateinput())
      df
      })





      share|improve this answer

























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        Ok I got it. When I executed the reactive inside a reactive expression or observer it works. Thank you guys for your help.



        I did it like this:



          output$x1 <- DT::renderDataTable({
        df <- data2
        df<- filter(df, df$date >= dateinput())
        df
        })





        share|improve this answer














        Ok I got it. When I executed the reactive inside a reactive expression or observer it works. Thank you guys for your help.



        I did it like this:



          output$x1 <- DT::renderDataTable({
        df <- data2
        df<- filter(df, df$date >= dateinput())
        df
        })






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 19 at 12:36

























        answered Nov 19 at 12:27









        Tim1234

        328




        328
























            up vote
            0
            down vote













            You have to use date_input in a reactive function, but if necessary you can get value outside by using isolate().



            But the goal of reactive function in Shiny it's to use isolate() at the minimum.



            I don't know if your data2 = data[, c('A', 'B', 'C')] is outside a reactive function.



            links:




            • https://shiny.rstudio.com/articles/isolation.html

            • https://shiny.rstudio.com/reference/shiny/1.0.1/isolate.html






            share|improve this answer





















            • Thanks phili_b. Yes, it's outside of the reactive function. I'm not quite sure yet what I need to adapt here. Did I understand correctly that I first have to isolate the query and that it is only used during a user interaction.
              – Tim1234
              Nov 19 at 10:36












            • yes, it's that :) " I first have to isolate the query and that it is only used during a user interaction"
              – phili_b
              Nov 19 at 10:43










            • Okay, and how can I use this? With a reactive function around the data query? I've never used isolation before. Sorry for asking :)
              – Tim1234
              Nov 19 at 10:48










            • I 've made a mistake you have to NOT isolate the query and that it is only used during a user interaction , except particular needs isolate() is only for particular purpose.
              – phili_b
              Nov 19 at 10:55















            up vote
            0
            down vote













            You have to use date_input in a reactive function, but if necessary you can get value outside by using isolate().



            But the goal of reactive function in Shiny it's to use isolate() at the minimum.



            I don't know if your data2 = data[, c('A', 'B', 'C')] is outside a reactive function.



            links:




            • https://shiny.rstudio.com/articles/isolation.html

            • https://shiny.rstudio.com/reference/shiny/1.0.1/isolate.html






            share|improve this answer





















            • Thanks phili_b. Yes, it's outside of the reactive function. I'm not quite sure yet what I need to adapt here. Did I understand correctly that I first have to isolate the query and that it is only used during a user interaction.
              – Tim1234
              Nov 19 at 10:36












            • yes, it's that :) " I first have to isolate the query and that it is only used during a user interaction"
              – phili_b
              Nov 19 at 10:43










            • Okay, and how can I use this? With a reactive function around the data query? I've never used isolation before. Sorry for asking :)
              – Tim1234
              Nov 19 at 10:48










            • I 've made a mistake you have to NOT isolate the query and that it is only used during a user interaction , except particular needs isolate() is only for particular purpose.
              – phili_b
              Nov 19 at 10:55













            up vote
            0
            down vote










            up vote
            0
            down vote









            You have to use date_input in a reactive function, but if necessary you can get value outside by using isolate().



            But the goal of reactive function in Shiny it's to use isolate() at the minimum.



            I don't know if your data2 = data[, c('A', 'B', 'C')] is outside a reactive function.



            links:




            • https://shiny.rstudio.com/articles/isolation.html

            • https://shiny.rstudio.com/reference/shiny/1.0.1/isolate.html






            share|improve this answer












            You have to use date_input in a reactive function, but if necessary you can get value outside by using isolate().



            But the goal of reactive function in Shiny it's to use isolate() at the minimum.



            I don't know if your data2 = data[, c('A', 'B', 'C')] is outside a reactive function.



            links:




            • https://shiny.rstudio.com/articles/isolation.html

            • https://shiny.rstudio.com/reference/shiny/1.0.1/isolate.html







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 19 at 10:30









            phili_b

            1199




            1199












            • Thanks phili_b. Yes, it's outside of the reactive function. I'm not quite sure yet what I need to adapt here. Did I understand correctly that I first have to isolate the query and that it is only used during a user interaction.
              – Tim1234
              Nov 19 at 10:36












            • yes, it's that :) " I first have to isolate the query and that it is only used during a user interaction"
              – phili_b
              Nov 19 at 10:43










            • Okay, and how can I use this? With a reactive function around the data query? I've never used isolation before. Sorry for asking :)
              – Tim1234
              Nov 19 at 10:48










            • I 've made a mistake you have to NOT isolate the query and that it is only used during a user interaction , except particular needs isolate() is only for particular purpose.
              – phili_b
              Nov 19 at 10:55


















            • Thanks phili_b. Yes, it's outside of the reactive function. I'm not quite sure yet what I need to adapt here. Did I understand correctly that I first have to isolate the query and that it is only used during a user interaction.
              – Tim1234
              Nov 19 at 10:36












            • yes, it's that :) " I first have to isolate the query and that it is only used during a user interaction"
              – phili_b
              Nov 19 at 10:43










            • Okay, and how can I use this? With a reactive function around the data query? I've never used isolation before. Sorry for asking :)
              – Tim1234
              Nov 19 at 10:48










            • I 've made a mistake you have to NOT isolate the query and that it is only used during a user interaction , except particular needs isolate() is only for particular purpose.
              – phili_b
              Nov 19 at 10:55
















            Thanks phili_b. Yes, it's outside of the reactive function. I'm not quite sure yet what I need to adapt here. Did I understand correctly that I first have to isolate the query and that it is only used during a user interaction.
            – Tim1234
            Nov 19 at 10:36






            Thanks phili_b. Yes, it's outside of the reactive function. I'm not quite sure yet what I need to adapt here. Did I understand correctly that I first have to isolate the query and that it is only used during a user interaction.
            – Tim1234
            Nov 19 at 10:36














            yes, it's that :) " I first have to isolate the query and that it is only used during a user interaction"
            – phili_b
            Nov 19 at 10:43




            yes, it's that :) " I first have to isolate the query and that it is only used during a user interaction"
            – phili_b
            Nov 19 at 10:43












            Okay, and how can I use this? With a reactive function around the data query? I've never used isolation before. Sorry for asking :)
            – Tim1234
            Nov 19 at 10:48




            Okay, and how can I use this? With a reactive function around the data query? I've never used isolation before. Sorry for asking :)
            – Tim1234
            Nov 19 at 10:48












            I 've made a mistake you have to NOT isolate the query and that it is only used during a user interaction , except particular needs isolate() is only for particular purpose.
            – phili_b
            Nov 19 at 10:55




            I 've made a mistake you have to NOT isolate the query and that it is only used during a user interaction , except particular needs isolate() is only for particular purpose.
            – phili_b
            Nov 19 at 10:55


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372511%2ferror-in-filter-statement-shiny-application%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin