I am trying to develop my first application in R Shiny, but so far the examples that I have found have been displaying how to make use of proprietary datasets (like carot or mtcars) rather than how to effectively load data into R Shiny for processing.
What I want to do is have R shiny create and load a different dataset depending on the input of the user and then display that dataset as a table accompanied by a visualization.
The approaches I have tried so far to solve this problem are listed in the code block below. CreateSnapshot and ProduceTable are user defined data preprocessing functions that are very long and are working properly.
CreateSnapshot is pulling raw data and saving it as a CSV. ProduceTable loads the CSVs created by CreateSnapshot and creates summary tables and a graphic that it then saves locally as a CSV and a WMF respectively.
When I attempt to run the application, numerous lines in the server trying to load the summary table and render it do not run. I also cannot seem to get R Shiny to load the WMF that is being generated by user input either. The code I've tried to use to render the images has since been deleted and does not show up in the code block below :
# Define UI for dataset viewer app ----
ui <- fluidPage(
# App title ----
titlePanel("FirstApp"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Text for providing a caption ----
# Note: Changes made to the caption in the textInput control
# are updated in the output area immediately as you type
# Input: Selector for choosing dataset ----
selectInput(inputId = "dis",
label = "Choose a discipline:",
choices = c("Choice 1", "Choice 2", "Choice 3")),
),
# Main panel for displaying outputs ----
mainPanel(
# Output: HTML table attempts ----
tableOutput("view"),
tableOutput("statstable"),
tableOutput("VisData"),
tableOutput("fileData"),
imageOutput("preImage")
)
)
)
# Define server logic to summarize and view selected dataset ----
server <- function(input, output) {
#Creating Base Dataset
Snapshot<-reactive({CreateSnapshot(input$dis)})
#Creating Summary Dataset
VisualizationTable<-reactive({ProduceTable(input$dis)})
#Attempting to create file from CSV files generated above.
fileData <- reactive({reactiveFileReader(1000, NULL, paste( input$dis, date.of.data,
"FilenameBoilerPlate.csv"), read.csv)})
VisData<-renderDataTable(read.csv(paste( input$dis, date.of.data,
"FileNameBoilerPlate.csv"), read.csv)))
#Attempting to create outputs for viewing. I have discovered that the first two allow for my user defined functions to run and create all of the files I need.
output$view <- renderTable({
head(Snapshot())
})
output$statstable <- renderTable({
head(VisualizationTable())
})
output$fileData <- renderTable({
fileData()
})
#Creating Image
output$preImage <- renderImage({
# When input$n is 3, filename is ./images/image3.jpeg
filename <- normalizePath(file.path('./images', paste(input$dis, date.of.data,
"Superbarplot.wmf", sep=' ')))
# Return a list containing the filename and alt text
list(src = filename,
alt = paste("Image Discipline", input$dis))
}, deleteFile = FALSE)
}
# Create Shiny app ----
shinyApp(ui, server)
Any thoughts about how to proceed would be greatly appreciated.
I would approach this by wrapping essentially all of what you're doing in an observeEvent function. I don't think reactive are the natural way to try to get your functions to run since they're more about returning an R object output that runs when required.
Something like:
observeEvent(input$dis, { Snapshot <- CreateSnapshot(input$dis)
VisualizationTable <- ProduceTable(input$dis)
fileData <- your read function of choice since it no longer needs to be reactive
output$fileData <- renderTable({ fileData })
etc..
} )
So every time the user chooses a discipline the entire thing will run, generating the outputs you want.
If there's no reason you want to produce a csv you could streamline this more by not saving the csv since you save it only to read the file to load it.