Hi,
I have a shiny problem I was hoping someone could help with
Its related to using lists in reactive values.
Im quite new to Shiny so any help that you can provide would be great.
The core questions is
How can I use my list of data frames within a shiny
reactiveValues
to pass to my charting function calledcreate_charts
In more detail, I have the code below which first creates a list of data frames. The data frames are from the famous iris
dataset where one is artificially smaller than the other
When you run it, you should get two different charts related to the two different data-frames contained within the list called startup_data
. One is a point chart and the other a bar chart. The results are created by the function create_charts
which returns a chart object created by the gridExtra
package
suppressMessages(library(tidyverse))
suppressMessages(library(gridExtra))
suppressMessages(library(shinydashboard))
suppressMessages(library(shiny))
# First im going to create two dataframes in a list
iris_short <- slice(iris, 1:70)
# Two Data sets are now contained in a list
startup_data <- list(iris = iris, iris_short = iris_short)
create_charts <- function(list){
p <- startup_data$iris %>%
ggplot(aes(x=Sepal.Length, y=Sepal.Width)) + geom_point()
q <- startup_data$iris_short %>% group_by(Species) %>% summarise(num_flowers = n()) %>%
ggplot(aes(x=Species, y=num_flowers)) +
geom_bar(stat = "identity")
# Create our two useful charts
z <- grid.arrange(p, q)
}
# Just demonstrates the function in action
# End result should be two gray plots (upper is point plot, lower is bar graph)
create_charts(startup_data)
So I tried then to create a shinydashboard which would look at the reactive values from the startup_data and then allow me to call my chart function. Unfortunately, I cant get it to work. Can anyone see the problem. You will have to put the proceeding code into your editor as well before you can run it.
Thank you for your time
# -----------------------------------------
# Shiny Bit
# -----------------------------------------
# First Up is the UI
header <- dashboardHeader(title = "Iris Demo")
sidebar <- dashboardSidebar(disable = TRUE)
body <- dashboardBody(
fluidRow(
tabBox(title = "Charting Information",width = 9,
tabPanel("Lovely Plot",
plotOutput(outputId = "iris_plot", height = "500px")))
)
)
server <- shinyServer(function(input, output, session){
# Kills the session once the user exits
session$onSessionEnded(stopApp)
# First up i want to declare my reactive values which is basically the two iris dataframe lists
values <- reactiveValues(details = startup_data)
# Now we need some charts to look at it
# Chart Produces the Running Total Chart
output$iris_plot <- renderPlot({
# basic error checking
# if the data frame is null or the number of rows is null return NUll
if (is.null(values$details()))
return(NULL)
if (nrow(values$details_uv()) == 0)
return(NULL)
# This is the problem
mychart <- values$details() %>%
create_charts(.)
print(mychart)
})
})
# Pulling it all together
ui <- dashboardPage(
skin = "blue",
header,
sidebar,
body
)
shinyApp(ui, server)