Shiny won't render my chart unless I resize the window

Hi everyone,

Word of warning. I am a complete noob when it comes to Shiny!

I'm currently trying to translate some code I've written into a Shiny app and am having some problems. I've set the app up so that you upload a file (mp3), this is then analysed in some way and chart should display that analysis at the end.

I have it working in a very basic fashion but for some reason it will only render the plot if I resize the Shiny app window?!

Any thoughts or help would be most appreciated.

# Define UI for data upload app ----
ui <- fluidPage(
    
    # App title ----
    titlePanel("Uploading Files"),
    
    # Sidebar layout with input and output definitions ----
    sidebarLayout(
        
        # Sidebar panel for inputs ----
        sidebarPanel(
            
            # Input: Select a file ----
            fileInput("file1", "Choose MP3 File",
                      multiple = TRUE,
                      accept = c("audio/mp3",
                                 ".mp3")),
            
            # Horizontal line ----
            tags$hr()
            
        ),
        
        # Main panel for displaying outputs ----
        mainPanel(
            
            # Output: Data file ----
            plotOutput("plot")
            
        )
        
    )
)

# Define server logic to read selected file ----
server <- function(input, output) {
    
    output$plot <- renderPlot({
        input$go
        # input$file1 will be NULL initially. After the user selects
        # and uploads a file, head of that data file by default,
        # or all rows if selected, will be shown.
        # load in the model
        model <<- load_model_hdf5("models/bob_cnn_dropout.h5")

        # get the album in the right order
        bands <<- c("band 1", "band 2")
        
        req(input$file1)
        
        whom <<- predict_band(input$file1$datapath)
        track_name <- input$file1$name

        whom %>% 
            ggplot(aes(pos, is_track, colour = winner)) +
            geom_line() +
            theme(legend.position = "none") +
            scale_colour_gradient(low = "deeppink2", high = "grey35") +
            labs(title = track_name)

    })
}

Feel free to mock the terrible code, it's all very hacky to get it to work.

Thanks, Chris

Here's a very basic app that does work:

library(shiny)
library(ggplot2)

ui <- fluidPage(
  fileInput("file1", "Upload"),
  plotOutput("plot")
)

server <- function(input, output) {
  df <- reactive({
    req(input$file1)
    data.frame(x = 1, y = 1, name = input$file1$name)
  })
  
  output$plot <- renderPlot({
    df() %>%
      ggplot(aes(x, y)) + 
      geom_text(aes(label = name))
  })
}
shinyApp(ui, server)

Things that I'd think about for your app:

  • It's probably unrelated, but I think you app will be easier to understand if you move the data frame generation into its own reactive.

  • You don't have an input control called "go" but you're referencing it.

  • The use of <<- is suspicious; I don't think you need it, and you don't need to be creating those objects every time that the input file changes (since they don't depend on the input you're just doing a lot of repeated work)

If that doesn't help, you'll need to create a more realistic reprex that other people can run on their computer.

Hi @hadley
Thanks very much for your help here and apologies for the pretty terrible reprex. As I said I was trying to circumvent actually learning Shiny and just shoving some bits in the hope I could put a front end on a presentation for a conference. My thoughts being maybe it's an option I've been unable to find and would be a simple "add x to your code".

In the future I'll be sure to actually learn the tool before leaping into it (and then complaining when I can't get it to work).

The input$go was left over from an old iteration I hadn't commented out and the <<- was more because I wanted to see what was coming out of things after the fact. But yes all very crappy.

Thanks again.

Oh and yes it worked, thank you.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.