I'm trying to get a function work within a shiny app, but it doesn't work as expected.
Outside of the app it works fine,

But within the app, it doesn't work:
Is it because the input$var isn't working as expected? (The checkbox also doesn't work and I'm still trying to figure that out.) My main question is about the function.
Code:
library(shiny)
if (interactive()) {
one <- function(.data, var, na = TRUE) {
if (na == FALSE)
return({
.data %>%
group_by({{var}}) %>%
drop_na() %>%
tally() %>%
mutate(`%` = 100*n/sum(n))
})
if (na == TRUE)
return({
.data %>%
group_by({{var}}) %>%
# drop_na() %>%
tally() %>%
mutate(`%` = 100*n/sum(n))
})
}
ui <- fluidPage(
selectInput("var", label = "Select Variable", choices = c(" ", names(mtcars))),
checkboxInput("check", "Display missing", FALSE),
tableOutput("value")
)
server <- function(input, output) {
output$value <- renderTable({
req(input$var)
if (input$check) ({
mtcars %>%
one(input$var, na = TRUE)
})
if(!input$check) ({
mtcars %>%
one(input$var, na = FALSE)
})
})
}
shinyApp(ui, server)
}
Dataset with missing values:
df <- data.frame(col1 = c(1:3, NA),
col2 = c("this", NA,"is", "text"),
col3 = c(TRUE, FALSE, TRUE, TRUE),
col4 = c(2.5, 4.2, 3.2, NA),
stringsAsFactors = FALSE)

