I'm trying to combine a navlistPanel with sidebartLayout in a shiny web app. The idea is for the app to look like this:
But, when running the app, it´s not taking in the data and objects created in the server.R, nor the graph nor the table are shown in the app. Also, both the conditionalPanel and the actionButton does not work.
I have run the code without putting the sidebarLayout into the navlistePanel (only running the code with sidebarLayout) and it works perfectly. The problem comes when running the sidebarLayout inside the navlistPanel.
¿Any idea why this is happening?
Here is the code (I use it separated into 3 scripts: global, ui, and server).
# global.R <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
library(shiny)
library(tidyverse)
library(DT)
data <- airquality
meses <- unique(airquality$Month)
variables <- colnames(airquality)[-c(5,6)]
#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
shinyUI(
fluidPage(
navlistPanel("Panel de navegación",
tabPanel("General",
sidebarLayout(
sidebarPanel(
selectInput(inputId = "mes", label = "Selecciona un mes", choices = meses),
selectInput(inputId = "var", label = "Selecciona la variable Y", choices = variables),
checkboxInput(inputId = "cambiarTitulo", label = "Escribe un título", value = F),
conditionalPanel(condition = "input.cambiarTitulo == true",
textInput(inputId = "titulo", label = "Escribe un título para la agráfica",
placeholder = "Tu título va aquí")),
actionButton(inputId = "boton", label = "Refrescar")
),
mainPanel(
tabsetPanel(
tabPanel(title = "Gráfica", plotOutput("grafica")),
tabPanel(title = "Datos", dataTableOutput("tabla"))
)
)
)
),
tabPanel("Gráfica", plotOutput("grafica")),
tabPanel("Datos", DTOutput("tabla"))
)
)
)
#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
shinyServer(function(input, output) {
datosFiltrados <- reactive({
datosNuevos <- data %>% filter(Month == input$mes)
return(datosNuevos)
})
output$tabla <- renderDT(
datatable(datosFiltrados(),
options = list(pageLenth = 10,
lengthMenu = c(10,20)))
)
grafica <- eventReactive(input$boton, {
if (input$cambiarTitulo == T) {
ggplot(datosFiltrados()) +
geom_point(aes_string(x = "Day", y = input$var)) +
ggtitle(input$titulo)
} else {
ggplot(datosFiltrados()) +
geom_point(aes_string(x = "Day", y = input$var))
}
})
output$grafica <- renderPlot(grafica())
})
You have duplicated IDs in UI grafica
and tabla
. ID duplication is not allowed in shiny.
If you need to have 2 components render the same thing in shiny, you still need to have unique IDs to each and watch closely how I make the duplications in the server.
# global.R <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
library(shiny)
library(tidyverse)
library(DT)
data <- airquality
meses <- unique(airquality$Month)
variables <- colnames(airquality)[-c(5,6)]
#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
ui <- fluidPage(
navlistPanel("Panel de navegación",
tabPanel("General",
sidebarLayout(
sidebarPanel(
selectInput(inputId = "mes", label = "Selecciona un mes", choices = meses),
selectInput(inputId = "var", label = "Selecciona la variable Y", choices = variables),
checkboxInput(inputId = "cambiarTitulo", label = "Escribe un título", value = F),
conditionalPanel(condition = "input.cambiarTitulo === true",
textInput(inputId = "titulo", label = "Escribe un título para la agráfica",
placeholder = "Tu título va aquí")),
actionButton(inputId = "boton", label = "Refrescar")
),
mainPanel(
tabsetPanel(
tabPanel(title = "Gráfica", plotOutput("grafica")),
tabPanel(title = "Datos", dataTableOutput("tabla"))
)
)
)
),
tabPanel("Gráfica", plotOutput("grafica2")),
tabPanel("Datos", DTOutput("tabla2"))
)
)
#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
server <- function(input, output, session) {
datosFiltrados <- reactive({
datosNuevos <- data %>% filter(Month == input$mes)
return(datosNuevos)
})
output$tabla <- output$tabla2 <- renderDT(
datatable(datosFiltrados(),
options = list(pageLenth = 10,
lengthMenu = c(10,20)))
)
grafica <- eventReactive(input$boton, {
if (input$cambiarTitulo == T) {
ggplot(datosFiltrados()) +
geom_point(aes_string(x = "Day", y = input$var)) +
ggtitle(input$titulo)
} else {
ggplot(datosFiltrados()) +
geom_point(aes_string(x = "Day", y = input$var))
}
})
output$grafica <- output$grafica2 <- renderPlot(grafica())
}
shinyApp(ui, server)