I have a simple shiny app that has lists loaded into it. I am trying to use some user inputs to call the data needed. Although when I use a paste0
to create the name of the list and dataframe it is not being called.
As you can see below: I have two valueboxes. The first one I am just calling the full list/dataframe which is Store1_2021$Bakery %>% nrow()
.
The second 1 I am using a paste0() function to try to replicate but its printing as text instead of calling the dataframe.
I'm not sure if this is best practice but if anyone has any solutions I would appreciate it.
## app.R ##
library(shiny)
library(shinydashboard)
Store1_2021 <- list(Bakery = structure(list(Sales = c(23, 33, 124, 133), Item = c("Bread",
"Bread", "Bread", "Torillas"), Units = c(1, 3, 2, 4)), row.names = c(NA,
-4L), class = "data.frame"), Electronics = structure(list(Sales = c(23,
33, 124, 133), Item = c("Tv", "Tv", "Tv", "Speaker"), Units = c(1,
3, 2, 4)), row.names = c(NA, -4L), class = "data.frame"))
Store2_2021 <- list(Bakery = structure(list(Sales = c(23, 133, 124, 23), Item = c("Bread",
"Torillas", "Bread", "Bread"), Units = c(1, 3, 2, 4)), row.names = c(NA,
-4L), class = "data.frame"), Electronics = structure(list(Sales = c(23,
33, 124, 12), Item = c("Speaker", "Tv", "Tv", "Speaker"), Units = c(1,
3, 2, 4)), row.names = c(NA, -4L), class = "data.frame"))
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
selectInput("store","Select Store:", choices = c(1,2)),
selectInput("department", "Select Department:", choices = c("Bakery","Electronics"))
),
dashboardBody(
fluidRow(
valueBoxOutput("box1"),
valueBoxOutput("box2")
)
)
)
server <- function(input, output) {
output$box1 <- renderValueBox({
valueBox(
Store1_2021$Bakery %>% nrow()
,
"No of Transactions (rows)",
icon = icon("users"),
color = "blue"
)
})
output$box2 <- renderValueBox({
valueBox(
paste0("Store",input$store,"_2021","$",input$department)
,
"No of Transactions (rows)",
icon = icon("users"),
color = "blue"
)
})
}
shinyApp(ui, server)
CodePudding user response:
Try this
## app.R ##
library(shiny)
library(shinydashboard)
Store1_2021 <- list(Bakery = structure(list(Sales = c(23, 33, 124, 133), Item = c("Bread",
"Bread", "Bread", "Torillas"), Units = c(1, 3, 2, 4)), row.names = c(NA,
-4L), class = "data.frame"), Electronics = structure(list(Sales = c(23,
33, 124, 133), Item = c("Tv", "Tv", "Tv", "Speaker"), Units = c(1,
3, 2, 4)), row.names = c(NA, -4L), class = "data.frame"))
Store2_2021 <- list(Bakery = structure(list(Sales = c(23, 133, 124, 23), Item = c("Bread",
"Torillas", "Bread", "Bread"), Units = c(1, 3, 2, 4)), row.names = c(NA,
-4L), class = "data.frame"), Electronics = structure(list(Sales = c(23,
33, 124, 12), Item = c("Speaker", "Tv", "Tv", "Speaker"), Units = c(1,
3, 2, 4)), row.names = c(NA, -4L), class = "data.frame"))
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
selectInput("store","Select Store:", choices = c(1,2)),
selectInput("department", "Select Department:", choices = c("Bakery","Electronics"))
),
dashboardBody(
fluidRow(
valueBoxOutput("box1"),
valueBoxOutput("box2")
)
)
)
server <- function(input, output) {
output$box1 <- renderValueBox({
valueBox(
Store1_2021$Bakery %>% nrow()
,
"No of Transactions (rows)",
icon = icon("users"),
color = "blue"
)
})
mystore <- reactive({get(sprintf("Store%s_2021",input$store))})
mydept <- eventReactive(c(mystore(),input$department), {
mystore()[[input$department]]
})
output$box2 <- renderValueBox({
valueBox(
nrow(mydept())
,
"No of Transactions (rows)",
icon = icon("users"),
color = "red"
)
})
}
shinyApp(ui, server)
CodePudding user response:
Another alterntive could be
output$box2 <- renderValueBox({
valueBox(
.GlobalEnv[[glue("Store{input$store}_2021")]][[input$department]] %>%
nrow(),
"No of Transactions (rows)",
icon = icon("users"),
color = "blue"
)
})