Ad
Conditional Values Using If Else Within Shiny App Using Tidyverse And Dplyr To Group And Filter A Dataset
I have a simple shiny that presents descriptive statistics using reactive. However, I would like to use ifelse
within tidyverse pipe (and not writing tons of codes). However, I´m not being able to do that. I checked previous post but it´s not working as well. I imagine this part is close to what I want:
students_results <- reactive({
ds %>%
if (input$all_quest == TRUE) { do nothing here!! } else {
filter(domain == input$domain) %>%
group_by(input$quest)
}
summarise(mean(test))
This code is 100% working,
library(shiny)
library(tidyverse)
library(DT)
ds <- data.frame(quest = c(2,4,6,8), domain = c("language", "motor"), test = rnorm(120, 10,1))
ui <- fluidPage(
sidebarLayout(
tabPanel("student",
sidebarPanel(
selectInput("domain", "domain", selected = "language", choices = c("language", "motor")),
selectInput("quest", "Questionnaire", selected = "2", choices = unique(ds$quest)),
checkboxInput("all_quest",
label = "Show all questionnaires",
value = FALSE)
)
),
mainPanel(
dataTableOutput("table")
)
)
)
server <- function(input, output) {
students_results <- reactive({
if (input$all_quest == TRUE) {
ds %>%
group_by(quest, domain) %>%
summarise(mean(test))
}
else {
ds %>%
filter(domain == input$domain) %>%
group_by(input$quest) %>%
summarise(mean(test))
}
})
output$table <- renderDataTable({
students_results()
}
)
}
shinyApp(ui = ui, server = server)
- Please check the akrun response below. Everything is working.
Ad
Answer
We may need to use {}
to block the code between the %>%
students_results <- reactive({
ds %>%
{
if (input$all_quest == TRUE) {
.
} else {
{.} %>%
filter(domain == input$domain) %>%
group_by(input$quest)
}
}%>%
summarise(mean(test))
})
Ad
source: stackoverflow.com
Related Questions
- → OctoberCMS Backend Loging Hash Error
- → "failed to open stream" error when executing "migrate:make"
- → OctoberCMS - How to make collapsible list default to active only on non-mobile
- → Create plugin that makes objects from model in back-end
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → How to install console for plugin development in October CMS
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → How to update data attribute on Ajax complete
- → October CMS - Conditionally Load a Different Page
Ad