Frikster
Frikster

Reputation: 2905

R Shiny - plotOutput inline and size arguments prevents renderPlot from being entered

I've written an R shiny app before where a plot only displayed after a button was clicked. For some weird reason I cannot get the following code to enter the renderPlot function after a button click (i.e. browser() is never reached when clicking the button) and I'm stumped. Please be my second pair of eyes, I'm sure I'm missing something trivial.

The observe function works fine and updates the selectizeInput like I want. Importing of the csv also works fine and I can also use DT::renderDataTable to display it.

You can use this as example data, though it shouldn't matter since my code isn't plotting anything yet

Server

rm(list = ls())

library(shiny)
library(leaflet)
library(DT)
library(stringr)
library(zoo)
options(shiny.maxRequestSize=30*1024^2) 

TAG_COL <- 1
TIME_COL <- 2
DATE_COL <- 3
ACTION_COL <- 4

HEADFIX_STR <- 'reward0'

shinyServer(function(input, output, clientData, session) {    
  inFile<-reactive({
    input$file1
    if(is.null(input$file1)){
      return(NULL)
    }
    else{
      inF<-read.csv(input$file1$datapath, header=input$header, sep=input$sep, quote=input$quote) 
      inF
    }
  })

  observe({ 
    if(!is.null(inFile)){
      updateSelectizeInput(session, "tagChooser",
                           'Choose Tags to plot',
                           choices = unique(inFile()[[TAG_COL]]))
    }
  })

  # Subset between start and end with bin column and selected mice
  subsetTable<-reactive({
    browser()
    # More code to come
  })

  # Plot histrogram of times between headfixes 
  # TODO: assumes all mice in same cage. Fix to make it work for a selection of mice from seperate cages
  output$plotHist<-renderPlot({
    input$goButton
    browser() # <-- Why is this browser() never reached after clicking the button?
    isolate({
        subsetTable()
        subsetHeadfixes<-subsetTable()
        # More code to come
      })
  })
})

ui

rm(list = ls())
# Immediately enter the browser/some function when an error occurs
# options(error = some funcion)

library(shiny)
library(DT)

shinyUI(fluidPage(
  titlePanel("MurphyLab"),
  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Choose CSV File',
                accept=c('text/csv', 
                         'text/comma-separated-values,text/plain', 
                         '.csv')),
      checkboxInput('header', 'Header', TRUE),
      radioButtons('sep', 'Separator',
                   c(Comma=',',
                     Semicolon=';',
                     Tab='\t'),
                   ','),
      radioButtons('quote', 'Quote',
                   c(None='',
                     'Double Quote'='"',
                     'Single Quote'="'"),
                   '"'),
      selectizeInput('tagChooser', 'Choose Tags to plot', choices = c("data not loaded"), multiple = TRUE),
      textInput("abs_start","Insert start date and time. Format: %Y-%m-%d %H:%M:%S",value="2015-06-15 01:00:00"),
      textInput("abs_end","Insert end date and time. Format: %Y-%m-%d %H:%M:%S",value="2015-08-15 01:00:00"),
      textInput("binning", "Binning in seconds",value = 86400),
      actionButton("goButton", "Plot")
    ),

    mainPanel(
      plotOutput("plotHist", inline = TRUE,width='auto',height='auto')
    )
  )
)
)

UPDATE:

So when I change the plotOutput call in mainPanel to plotOutput("plotHist") everything works as expected. I'm changing my question instead to: Why does this fix things? Also, having only inline set to FALSE does not fix things.

Documentation for inline states:

use an inline (span()) or block container (div()) for the output

Upvotes: 2

Views: 1209

Answers (1)

Yihui Xie
Yihui Xie

Reputation: 30174

See the documentation of renderPlot() (I highlighted the relevent part below):

width,height The width/height of the rendered plot, in pixels; or 'auto' to use the offsetWidth/offsetHeight of the HTML element that is bound to this plot. You can also pass in a function that returns the width/height in pixels or 'auto'; in the body of the function you may reference reactive values and functions. When rendering an inline plot, you must provide numeric values (in pixels) to both width and height.

Upvotes: 1

Related Questions