0

I want to plot with R shiny the variables contained in a data frame. I would hence have several plots, that is several renderPlot functions, but I want to create that data frame only once. I therefore look for a way to do something like

library(shiny)

server <- shinyServer(function(input, output, session) {

## Creating and plotting the dataframe

## Calling renderPlot
output$plotxy <- renderPlot({
x = c(1,2,3)
y = c(4,5,6)
z = c(7,8,9)
d = data.frame( x, y, z )
plot( d$x, d$y )
})

output$plotxz <- renderPlot({
x = c(1,2,3)
y = c(4,5,6)
z = c(7,8,9)
d = data.frame( x, y, z )
plot( d$x, d$z )
})

output$plotzy <- renderPlot({
x = c(1,2,3)
y = c(4,5,6)
z = c(7,8,9)
d = data.frame( x, y, z )
plot( d$z, d$y )
})

})




ui <- shinyUI(
fluidPage(
plotOutput("plotxy"),
plotOutput("plotxz"),
plotOutput("plotzy")
)
)

shinyApp(ui = ui, server = server)

Would create the dataframe "d" as a global variable be a good idea? Any tip?

Mark Peterson
  • 9,370
  • 2
  • 25
  • 48
Xavier Prudent
  • 1,570
  • 3
  • 25
  • 54
  • Why is a global variable un-acceptable? I would recommend including it in `global.R` rather than in `server.R`, but the effect is the same. Is there anything about the current behavior that you actually dislike? If so, provide a full [MWE](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), along with a description of what is wrong with the outcome. – Mark Peterson Oct 03 '16 at 15:48
  • I will both read and write to this dataframe, and would like to avoid "data collisions" between different versions of this dataframe. That is why I prefer to have a clear control on what is fed to the plot – Xavier Prudent Oct 03 '16 at 16:00
  • Without knowing your use case, I have no way of suggesting what approaches could be best. Can you post an MWE showing an example of the changes to the data you are intending? It is not at all clear to me how you intend to avoid "data collisions" if you are potentially writing over the data as well. – Mark Peterson Oct 03 '16 at 16:03
  • I understand, i just edited and made a full working example. At first the most important issue is how to externalise the creation of the dataframe – Xavier Prudent Oct 03 '16 at 16:20
  • None of the examples in the MWE suggest that a single global variable `d` would be unaccpetable. (Note: edit is to make the app self contained and easier for others to run.) – Mark Peterson Oct 03 '16 at 16:42

1 Answers1

2

Here is an example using a reactive that would allow you to change the data in a single place. However, this is no different than loading the data.frame globally, unless/until you add responsive portions to the function that defines d() (e.g., changes due to inputs or re-reading from disk in response to a button click).

library(shiny)

server <- shinyServer(function(input, output, session) {

  ## Creating the dataframe
  d <- reactive({
    data.frame(
      x = 1:3
      , y = 4:6
      , z = 7:9 
    )
  })

  ## Calling renderPlot
  output$plotxy <- renderPlot({
    plot( d()$x, d()$y )
  })

  output$plotxz <- renderPlot({
    plot( d()$x, d()$z )
  })

  output$plotzy <- renderPlot({
    plot( d()$z, d()$y )
  })

})




ui <- shinyUI(
  fluidPage(
    plotOutput("plotxy"),
    plotOutput("plotxz"),
    plotOutput("plotzy")
  )
)

shinyApp(ui = ui, server = server)
Mark Peterson
  • 9,370
  • 2
  • 25
  • 48