Variable plot size in shiny / ggplot2

I have a brilliant app that I'm working on. Part of it involves creating a plot using ggplot2. There are quite a few variables, so I would like it to be able to scroll quite wide so that it reads. I was originally trying to adjust the size with plotOutput("plot1", width = X)

(where x is some number) and I can get it to work like this if I know exactly what mine would look like plot1

. The point is that there will eventually be filters that will affect how large it is plot1

, so I cannot pre-define it.

What I would like to do is somehow tell R to say, "Use ggplot2 to make a barcard where the bars are only one unit wide, and make the plot as large as needed."

Here is the code in server.r that generates the graph:

output$plot1 <- renderPlot({
ggplot(data = dat, aes(x = foo)) + geom_bar(stat="bin")
+facet_grid(~bar, scales = "free_x", space = "free")

      

and then in ui.r I call it like I did above:

plotOutput("plot1", width = X)

      

In particular, what happens is that "facet_grid" breaks the graph into different groups, and the user will have the option to decide what / how many groups exist.

A possible workaround I was thinking would involve somehow trying to count the number of bars that would be present and then coming up with a function to figure out how big X would be. In a normal R session, this would be pretty easy, but in Shiny, I would need to compute a variable in server.r and use it in ui.r, and I am a little confused about how to do this.

EDIT: I may have figured out a workaround instead of doing the plot by casting the plot to the image and displaying the image. Now I just need to figure out how to make the image full size instead of having a shiny scale to fit the window. Any advice on this matter would be appreciated.

+3


source to share


2 answers


I was able to do this using renderUI. Something like the following in server.r

output$newUI <- renderUI({
   output$plot1 <- renderPlot({
   ggplot(data = dat, aes(x = foo)) + geom_bar(stat="bin")
   +facet_grid(~bar, scales = "free_x", space = "free")
   })
plotOutput(plot1, width = X)
})

      

(where X is some data function) and in ui.r

mainPanel(
  uiOutput("testUI")
)

      

Edit: The above code was a lazy effort to make this work without actually reproducing my complete code. Here's a more complete code, corresponding code:



in server.ui

output$ui <- renderUI({
    ...
    ##here, I did a query based on parameters passed in from the 
    ##shiny ui, so the data was variable.
    ##so I do some processing of the data, and then a function on the
    ##resulting data so I know how much to scale my plot 
    ##(something like nn = nrow(data))
    ##call that scaling factor "nn"
    ....
    output$plot1 <- renderPlot({
        ggplot(...)
        )}
    plotOutput("plot1", width = 30*nn, height = 500) ## nn is my scaling factor
)}

      

And in ui.R

ShinyUI(fluidPage(
    ...,
    mainPanel(
        uiOutput("ui")
    )
)

      

+4


source


I use charts are not ggplot2.

For those who come here with scaling issues googleVis

, I would like to share this, instead of setting say width = 200

in pixels, you can use the word "automatic" which scales well and was not clear in the documentation. So, an excerpt from one of my functions:



 # where plotdt has my data with columns px and py

 plot1 <- gvisBarChart(plotdt, 
                       xvar = px,
                       yvar = c(py),
                       options = list(width = "automatic",
                                      height = "automatic")

      

Hope it helps others.

0


source







All Articles