I am new with Shiny App and I wanted to try something very simple but I'm having a hard time with it.
I have a data that looks like this:
Proyecto <- c("ECO","ECO", "ECO", "ECO", "FECHIC", "FECHIC")
Encuesta <- c(1, 2, 1,1, 1, 1)
Visita <- c(1,2,3, 1, 1, 1)
acep <- (1,1,1,1,1,1)
covid_date <- c("2020-06-20", "2020-12-21", "2021-05-02", "2020-06-22", "2020-06-20", "2020-12-21")
covid_fechic <- c(2,1,2,2,2,NA)
BASE_COMPLETA <- tibble(Proyecto, Encuesta, Visita, acep, covid_date, covid_fechic)
I want to create an App that could have as an Input:
- Proyecto (selectInput)
- Encuesta (selectInput)
- Visita (selectInput)
- Any variable from the dataset, such as covid_date or covid_fechic (textInput)
With these input I would like to create a table that first, filter by Proyecto, Encuesta and Visita, and later select only the Variable that I chose, arrange it, group it and summarise in a way that could show me the different options of the variable and how many people choose any of those options.
Without the Shiny App is something like this:
BASE_COMPLETA %>%
filter(Proyecto == "FECHIC") %>%
filter(Encuesta == 1) %>%
filter(Visita == 1) %>%
filter(acep == 1) %>%
select(covid_fechic) %>%
arrange(covid_fechic) %>%
group_by(covid_fechic) %>%
summarise(n = n()) %>%
print()
But when I try this in my Shiny App, it seems like is only working until the select part, I can't get the arrange, group_by or summarise.
Is it because I can't use the same input variable many times?
Here is my very simple Shiny Code:
lista_proyecto <- c("FECHIC", "ECO")
lista_encuestas <- c(1, 2)
lista_visitas <- c(1, 2, 3)
ui <- fluidPage(
theme = shinytheme("flatly"),
h1(id="big-heading", "Encuesta COVID"),
tags$style(HTML("#big-heading{color: #092F87;}")),
sidebarLayout(
sidebarPanel(
width = 12,
selectInput("Proyecto",
label = h4("Proyecto:"),
choices = lista_proyecto),
selectInput("Encuesta",
label = h4("Encuesta:"),
choices = lista_encuestas),
selectInput("Visita",
label = h4("Ronda:"),
choices = lista_visitas),
textInput("Variable",
label = h4("Variable de interés"),
value = "covid_date")
),
mainPanel(
fluidRow(DTOutput("Tabla3"))
)
)
)
server <- function(input, output) {
output$Tabla3 <- renderDT({
BASE_COMPLETA %>%
filter(Proyecto == input$Proyecto) %>%
filter(Encuesta == input$Encuesta) %>%
filter(Visita == input$Visita) %>%
filter(acep == 1) %>%
select(input$Variable) %>%
arrange(input$Variable) %>%
group_by(input$Variable) %>%
summarise(n = n()) %>%
print()
}, options = list(bFilter=0))
}
shinyApp(ui = ui, server = server)
Can anybody help me to undestand where is the problem and what can I do to make this works? Thanks in advance!.
CodePudding user response:
knowing that the value from the inputs are characters, we can use the special construct .data[[]]
to select a variable using a string.
output$Tabla3 <- renderDT({
BASE_COMPLETA %>%
filter(Proyecto == input$Proyecto) %>%
filter(Encuesta == input$Encuesta) %>%
filter(Visita == input$Visita) %>%
filter(acep == 1) %>%
select(.data[[input$Variable]]) %>%
arrange(.data[[input$Variable]]) %>%
group_by(.data[[input$Variable]]) %>%
summarise(n = n()) %>%
print()
}, options = list(bFilter=0))
alternatively we could also used function get()
.
For more information see Programming with dplyr.
app code:
library(shiny)
library(DT)
library(shinythemes)
Proyecto <- c("ECO", "ECO", "ECO", "ECO", "FECHIC", "FECHIC")
Encuesta <- c(1, 2, 1, 1, 1, 1)
Visita <- c(1, 2, 3, 1, 1, 1)
acep <- c(1, 1, 1, 1, 1, 1)
covid_date <- c("2020-06-20", "2020-12-21", "2021-05-02", "2020-06-22", "2020-06-20", "2020-12-21")
covid_fechic <- c(2, 1, 2, 2, 2, NA)
BASE_COMPLETA <- tibble(Proyecto, Encuesta, Visita, acep, covid_date, covid_fechic)
lista_proyecto <- c("FECHIC", "ECO")
lista_encuestas <- c(1, 2)
lista_visitas <- c(1, 2, 3)
ui <- fluidPage(
theme = shinytheme("flatly"),
h1(id = "big-heading", "Encuesta COVID"),
tags$style(HTML("#big-heading{color: #092F87;}")),
sidebarLayout(
sidebarPanel(
width = 12,
selectInput("Proyecto",
label = h4("Proyecto:"),
choices = lista_proyecto
),
selectInput("Encuesta",
label = h4("Encuesta:"),
choices = lista_encuestas
),
selectInput("Visita",
label = h4("Ronda:"),
choices = lista_visitas
),
textInput("Variable",
label = h4("Variable de interés"),
value = "covid_date"
)
),
mainPanel(
fluidRow(DTOutput("Tabla3"))
)
)
)
server <- function(input, output) {
output$Tabla3 <- renderDT(
{
BASE_COMPLETA %>%
filter(Proyecto == input$Proyecto) %>%
filter(Encuesta == input$Encuesta) %>%
filter(Visita == input$Visita) %>%
filter(acep == 1) %>%
select(.data[[input$Variable]]) %>%
arrange(.data[[input$Variable]]) %>%
group_by(.data[[input$Variable]]) %>%
summarise(n = n()) %>%
print()
},
options = list(bFilter = 0)
)
}
shinyApp(ui = ui, server = server)