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?
r shiny
add a comment |
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?
r shiny
else if
should start at the same row after closing bracket, i.e.:if(){}else if(){}else if....
– zx8754
Nov 19 at 10:25
add a comment |
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?
r shiny
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
r shiny
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
add a comment |
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
add a comment |
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
})
add a comment |
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
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 needsisolate()
is only for particular purpose.
– phili_b
Nov 19 at 10:55
add a comment |
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
})
add a comment |
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
})
add a comment |
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
})
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
})
edited Nov 19 at 12:36
answered Nov 19 at 12:27
Tim1234
328
328
add a comment |
add a comment |
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
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 needsisolate()
is only for particular purpose.
– phili_b
Nov 19 at 10:55
add a comment |
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
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 needsisolate()
is only for particular purpose.
– phili_b
Nov 19 at 10:55
add a comment |
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
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
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 needsisolate()
is only for particular purpose.
– phili_b
Nov 19 at 10:55
add a comment |
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 needsisolate()
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
else if
should start at the same row after closing bracket, i.e.:if(){}else if(){}else if....
– zx8754
Nov 19 at 10:25