简体   繁体   中英

How to make “height” argument dynamic in renderPlot() in Shiny R package

I'm trying to figure out how to make "height" variable in renderPlot function in Shiny R package receive a variable from inside renderPlot(). This is my code in server.R:

shinyServer(function(input, output) { 
output$contents <- renderPlot({
file<-input$file #a CSV file is uploaded

file<-with(file,
                     aggregate(file[, input$metricIn],
                     by=list(period=day,
                     dimension1=file[, input$dimension1In], 
                     dimension2=file[, input$dimension2In]),
                     FUN = input$funIn, na.rm=TRUE))
#input$dimension1In is column name from file
#input$dimension2In is column name from file

#count of facets in the plot to calculate height in renderPlot height argument
facetCount<<-as.numeric(length(unique(file[, input$dimension1In]))) * 100


plot<-reactive({      
  g<-ggplot(data = file, aes_string(x=input$dimension2In,
                                    y=input$metricIn, fill=period))
  plot<-g + geom_bar(stat="identity", position=position_dodge()) +
  facet_wrap(as.formula(paste("~", input$dimension1In)),
               scales = "free", ncol=1)
}) #end of reactive
  print(plot())
}, width=1000,
height = facetCount)

}) #end of shinyServer

so my problem is that height argument is renderPlot doesn't see the facetCount variable even when I use <<- assignment inside renderPlot.

I want to make height dynamic because if there're a lot of facets to plot I want plot height to adjust accordingly.

When the height of your plot depends only on some input value then please follow the answer to this question . The main change is to use uiOutput instead of plotOutput in ui.R file and therefore also wrap plotOutput in renderUI in server.R file.

When the height of your plot is calculated on the server side, like in your example above, you additionally need to add a function call to the height parameter in plotOutput . I think this is required, because the height parameter is used by Shiny before making the actual plot. The example below worked for me.

ui.R:

shinyUI(fluidPage(
    titlePanel("The plot height depends on a calculated value"),
    # Sidebar with a slider input for a number
    sidebarLayout(
        sidebarPanel(
            sliderInput("dimension1In",
                        "Choose a number:",
                        min = 20,
                        max = 50,
                        value = 30)
            ),
        # use uiOutput instead of plotOutput directly
        mainPanel(
            uiOutput('ui_plot')
        )
    )
))

server.R:

shinyServer(function(input, output) {
    # instead of global assignment use reactive values
    values <- reactiveValues()
    # function where you do all the processing, e.g. read file, get data from it
    file_processing <- function() {
        # imagine this coefficient comes from your file
        coefficient <- 10
        return(coefficient)
    }
    # this function defines a height of your plot
    plot_height <- function() {
        # calculate values$facetCount
        values$facetCount <- as.numeric(input$dimension1In) * file_processing()
        return(values$facetCount)
    }
    # this function defines your plot, which depends on input$dimension1In
    output$contents <- renderPlot({
        plot(1:input$dimension1In)
    })
    # wrap plotOutput in renderUI
    output$ui_plot <- renderUI({
        plotOutput("contents", height = plot_height(), width = "100%")
    })

})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM