3

I have a Shiny application I'm working on. Part of it involves generating a plot using ggplot2. There are quite a lot of variables, so I'd like it to be able to scroll pretty widely so that it's readable. Originally I tried to adjust the size using plotOutput("plot1", width = X) (where x is some number), and I can make it work like this if I know exactly what my plot1 is going to look like. The thing is, there will eventually be filters that will affect how big plot1 is, so I can't predefine it.

What I'd like to be able to do is somehow tell R to say, "use ggplot2 to make a barchart where the bars are all x units wide, and make the plot as big as it needs to be."

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

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)

Specifically what's happening is that 'facet_grid' is splitting the plot up into different groups, and the user will have the option of deciding what/how many groups there are.

A possible workaround I thought of 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 regular R session this would be pretty easy, but in Shiny, I would need to be able to calculate a variable in server.r and use it in ui.r, and I'm a bit confused on how to do that.

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

2 Answers 2

4

I was able to do this by 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 function of the data) and in ui.r

mainPanel(
  uiOutput("testUI")
)

Edit: The above code was a lazy effort to get the work done without actually reproducing my full code. Here is the more full code, relevant 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")
    )
)
Sign up to request clarification or add additional context in comments.

2 Comments

I get the error: Error in tag("div", list(...)) : object 'plot1' not found. In addition your code has at least one error as you refer to variable "testUI" in ui.R and you use newUI in server.R. Can you confirm your code is correct? I have also tried accessing output$plot1 on the plotOutput code but then I get the error: eading objects from shinyoutput object not allowed. Thanks in advance
Yes, my code works, but it was a bit butchered because I just entered in some skeleton code to save time and didn't bother to double check the names of stuff. I edited my answer, so you can see the proper code (well, the relevant stuff anyway)
0

I use charts not ggplot2.

For those who come here with googleVis scaling issues, I would like to share that instead of setting say width = 200 in pixels you can use the word "automatic" which scales nicely and was not clear in documentation. So snippet 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 this helps others.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.