1

Consider a complete app in Shiny. With multiple navbars, tabsets and plots.

What I need to build is a SEPARATE bottom info pane containing some information relevant to each of the rendered plots. This info depends on the actual data in the plotted chart.

So here is what I want to do Step1: user enters an input. Step2: Update the relevant plot Step3: compute relevant data needed for bottom pane and store in a global variable) Step4: once step3 IS COMPLETE, I want it to trigger an update of the bottom Pane

I tried to make the bottom pane AND the plot reactive to user input. BUT this may not guarantee that the plot rendering got a chance to update the data for info pane.

Any tips would be greatly appreciated.

thanks

Minimal EXAMPLE is here. NOTE - I know that I could make clusters a reactive "object". But the deal is that the myPlotInfo object is updated deep inside various functions. Hence it is not easily calculatable outside.


Server.R

source(util.R)
library(shiny)

myPlotInfo<-iris #setting inital value

shinyServer(function(input, output, session) {

 output$plot1 <- renderPlot({
 plotFunction(iris,input$xcol, input$ycol,input$clusters )
})

output$myTable<-renderDataTable({
  myPlotInfo    
})

})

ui.R

shinyUI(pageWithSidebar(
 headerPanel('Iris k-means clustering'),
 sidebarPanel(
  selectInput('xcol', 'X Variable', names(iris)),
  selectInput('ycol', 'Y Variable', names(iris),
            selected=names(iris)[[2]]),
  numericInput('clusters', 'Cluster count', 3,
             min = 1, max = 9)
 ),
mainPanel(
 plotOutput('plot1'),
 dataTableOutput("myTable")
)

))


util.R

plotFunction<-function(dat,x,y,n){
par(mar = c(5.1, 4.1, 0, 1))
selectedData<-dat[, c(x, y)]
clusters <- kmeans(selectedData,n)
myPlotInfo<<-selectedData
plot(selectedData,
     col = clusters$cluster,
     pch = 20, cex = 3)
    points(clusters$centers, pch = 4, cex = 4, lwd = 4)    
}
guna
  • 1,148
  • 1
  • 10
  • 18
  • Please provide a minimal reproducible example http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Pork Chop Jan 13 '15 at 10:03
  • are you looking for the 'clusters' about your kmeans or just the table that is used for computation? – Pork Chop Jan 13 '15 at 10:46
  • Actually My goal is that I want to be able to update a global variable myPlotInfo. Inside a plot function. And that needs to in turn trigger reDRawing of the output: output$myTable – guna Jan 13 '15 at 10:49
  • well you can write it to the global via <<- – Pork Chop Jan 13 '15 at 10:49
  • Yeah. I did that. I want that to trigger the redrawing of the output: output$myTable Thanks – guna Jan 13 '15 at 10:50

1 Answers1

2

OK, I've found the answer by looking at another post:

I added the below line to make the global variable reactive. And this works very well.

makeReactiveBinding("myPlotInfo")
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
guna
  • 1,148
  • 1
  • 10
  • 18