eventReactive no longer responding to button cilck

127 views
Skip to first unread message

Just Rookie

unread,
Jul 11, 2015, 11:51:34 PM7/11/15
to shiny-...@googlegroups.com
I would like to load a data set from file and plot the data set when button was clicked. I used 
eventReactive()

 to trigger the ploting command with an action button, but it did not change anything. Any help? Thank you in advance!

ui.R

library(shiny)
library(shinyRGL)   #  for displaying 3D plot

shinyUI(fluidPage(
  
  titlePanel(h2("TEST")),
  
  sidebarLayout(
    
    sidebarPanel(
      fileInput("df", label = h3("Load .txt data"), accept=c('text/csv', 
                                                   'text/comma-separated-values,text/plain', 
                                                   '.csv')),
      
      actionButton("threeD", label = "3D")
    ),
    
    mainPanel(
      tabsetPanel(
        tabPanel("Data", tableOutput("cont")), 
        tabPanel("3D", webGLOutput("threeDPlot"))
      )
    )
  )
))


sever.R

options(rgl.useNULL=TRUE)
library(shiny)
library(shinyRGL)
library(rgl)

shinyServer(function(input, output) {
  
  dataInput <- reactive({
    validate(
      need(input$df != "", label = "Data set")
    )  
    
    inFile <- input$df
    dfTemp <- read.table(inFile$datapath, header=FALSE, sep="")
    colnames(dfTemp) <- c("x", "y", "z", "value")
    dfTemp
  })
  
  
  output$cont <- renderTable({
    dataInput()
  })
  
  eventReactive(input$threeD, {
    
    output$threeDPlot <- renderWebGL({
      
      clr <- dataInput()$value/max(dataInput()$value)
      f <- colorRamp(c("green", "yellow", "purple", "red"))
      
      for(i in 1:length(dataInput()$x)){
        shade3d(translate3d(scale3d(cube3d(col=rgb(f(clr[i])/255), alpha=0.15),
                                    10.0, 10.0, 2.5),dataInput()$x[i],
                            dataInput()$y[i],dataInput()$z[i]))
      }
    })
  })
})

text.txt

1 2 3 4
2 4 6 8
3 6 9 12
33 43 75 21

Joe Cheng

unread,
Jul 12, 2015, 7:15:35 PM7/12/15
to Just Rookie, shiny-...@googlegroups.com
It's never correct to assign output$xxx inside of a reactive or eventReactive.

Don't think of assigning output$xxx as redrawing the output, instead think of it as giving the recipe of how output$xxx should be drawn.

That being said, I removed the eventReactive from around the output$xxx, and it still fails with a JavaScript error:
"Uncaught Error: Could not find method of starting WebGL scene.(anonymous function) @ glbinding.js:33"

That seems like something specific to the output or the binding.

Once you figure that out though, here's how you can restrict the updating of the 3D plot to only happen on button press:

output$threeDPlot <- renderWebGL({
  validate(need(input$threeD, FALSE))
  isolate({
    # The rest of the code to do the 3D plot
  })
})

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/6ffcca08-70f3-45ae-b539-05a9318131a8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Pengcheng Hu

unread,
Jul 13, 2015, 5:28:16 AM7/13/15
to Joe Cheng, shiny-...@googlegroups.com
I found the reason related to the order of tabPanels. If the "3D" tabPanel was the first, the plot would be shown:
tabsetPanel(
            tabPanel("3D", webGLOutput("threeDPlot")), 
            tabPanel("Data", tableOutput("cont"))
          )

But if the "Data" tabPanel was the first, the plot would not be shown:

tabsetPanel(
            tabPanel("Data", tableOutput("cont")),
            tabPanel("3D", webGLOutput("threeDPlot"))                 
          )
I do not know the deep cause of this situation, but when I changed the order of tabPanels, it worked for me.


Best Regards,
Pengcheng Hu
Reply all
Reply to author
Forward
0 new messages