invalidate/reset an input value

2,031 views
Skip to first unread message

msquatrito

unread,
Feb 3, 2015, 11:23:18 AM2/3/15
to shiny-...@googlegroups.com
Hi All,

In my shiny app I'm generating an input value (input$rows) by clicking on a row of a dataTable. I then use this value as an input to create a scatterplot. Please see the code at the end of this message.
After the first time the data table is generated everything works fine: I can select a specific row and then generate the scatterplot.

However, when a new dataTable is rendered after changing the input dataset, the previous input$rows is not invalidated/reset. I would like input$rows to be `NULL` when a new dataset is selected and a new datatable is rendered.

I guess I'm missing an `observe` call somewhere, but unfortunately I cannot figure out how to fix it.

Thanks in advance for your help.

Massimo



#' Select a dataset for the correlation
df <- reactive({
      switch(input$datasetCor, 
               ………
    })

#' Generate a reactive element of the correlation data. This changes based on some input parameters: df() (a gene expression dataset) and input$geneCor 
corrData <- reactive({
               ………
    })

#' Generate an HTML table view of the correlation data 

output$corrData
<- renderDataTable({
  corr
.table <- corrData()
}, options = list(orderClasses = TRUE), callback = "function(table) {
      table.on('click.dt', 'tr', function() {
            table.$('tr.selected').removeClass('selected');
            $(this).toggleClass('selected');            
        Shiny.onInputChange('rows',
                            table.rows('.selected').data()[0][0]);
      });
    }"
)

# The call back function return the first value of the selected row as a new input called 'input$rows"

#' Generate the scatter plot
output$corrDataPlot
<- renderPlot({
    input$rows 
    validate(
        need(input$geneCor != "" & input$rows!= "","")
      )
  scatter
<- ggplot(df(),mapping = aes_string(x = input$gene, y = input$rows)) + geom_point(alpha=.5)
 
print(scatter)
})

msquatrito

unread,
Feb 10, 2015, 12:24:12 PM2/10/15
to shiny-...@googlegroups.com
I have not yet figured it out how to fix it.
You can find below a reproducible example.  I would like input$rows to be `NULL` when a new dataset is selected and a new datatable is rendered.
Thanks

dataset1 <- data.frame(a=rnorm(50),b=rnorm(50),c=rnorm(50), d=rnorm(50))
dataset2
<- data.frame(a=rnorm(50),b=rnorm(50),c=rnorm(50), d=rnorm(50))


server
<- function(input, output) {
  datasetInput
<- reactive({
   
switch(input$dataset,
           
"dataset1" = dataset1,
           
"dataset2" = dataset2)
 
})
 
 
#' Generate a reactive element of the the correlation data
  corrData
<- reactive({
    data
<- datasetInput()
    values
<- data[ ,input$gene, drop = F]
    r
<- apply(values, 2, function(x) { apply(data, 2, function(y) { cor(x,y, method = "pearson") })})
    corr
<- data.frame(Gene = row.names(r),r = round(r,3))
    corr
 
})
 
 
#' Generate an HTML table view of the correlation table
  output$corrData
<- renderDataTable({  
    corrData
()

 
}, options = list(orderClasses = TRUE), callback = "function(table) {
      table.on('click.dt', 'tr', function() {
            table.$('tr.selected').removeClass('selected');
            $(this).toggleClass('selected');            
        Shiny.onInputChange('rows',
                            table.rows('.selected').data()[0][0]);
      });
    }"

 
)

 
 
#' Generate the correlation plot

  output$corrDataPlot
<- renderPlot({
    input$rows
    validate
(

      need
(input$gene != "" & input$rows!= "","Click on a row to see the corresponding correlation plot."))
    df
<- datasetInput()
    aes_scatter
<- aes_string(x = input$gene, y = input$rows)
    ggplot
(df,mapping = aes_scatter) +geom_point(alpha=.5)
 
})
}


ui
<- shinyUI(fluidPage(
  sidebarLayout
(
    sidebarPanel
(
      selectInput
(inputId = "dataset", label = "Dataset", choices = c("dataset1","dataset2")),
      selectInput
(inputId = "gene", label = "Gene", choices = c("a","b","c"))
   
),
    mainPanel
(
      dataTableOutput
(outputId = "corrData"),
      plotOutput
("corrDataPlot"))
 
)
))


shinyApp
(ui = ui, server = server)

Joe Cheng

unread,
Feb 11, 2015, 12:14:00 AM2/11/15
to msquatrito, shiny-...@googlegroups.com
I'd introduce a reactive variable, and two observers that set it. Like this:

server <- function(input, output, session) {
  v <- reactiveValues(rows = NULL)
  observeEvent(input$rows, {
    v$rows <- input$rows
  })
  observeEvent(datasetInput(), {
    v$rows <- NULL
  })

(observeEvent is newly introduced in Shiny 0.11 and is mostly just a shortcut for doing observe({ x; isolate({y}) }), plus some other convenience features)

Then everywhere you currently refer to input$rows, refer to v$rows instead. v$rows will be set to input$rows or NULL, depending on which observeEvent triggered more recently.


--
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/e0503e6a-bc41-4ce0-a45a-bf8e84a5657c%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

msquatrito

unread,
Feb 11, 2015, 4:10:19 AM2/11/15
to shiny-...@googlegroups.com
Thank you so much Joe, it works exactly as I needed. 
Best,
Massimo

Carlos Martín

unread,
Jun 3, 2015, 6:44:42 AM6/3/15
to shiny-...@googlegroups.com, squa...@gmail.com
Hi Joe,

I am working on a similar issue but using two selectInputs, two googleBubbleCharts and a googleLineChart where each googleBubbleChart is dependent of the corresponding selectInput values and the googleLineChart is dependent of the selected bubbles in the googleBubbleCharts (please see attachment). I agree that your solution solves the issue when we select a new value in the selectInput components, however both googleBubbleCharts still keep the index of the bubbles selected (input$chart_selection and input$chart1_selection, as we don't modify the selection in the charts). Therefore, if you click in a new bubble with the same index as the one kept in input$chart_selection or input$chart1_selection, depending on the chart you are clicking on, nothing is triggered and nothing is shown in the lineChart for that bubble. Is there any way to solve this issue?. Can we modify the input$chart_selection value from R?.

Hope my explanation is clear.

Thanks.

Best,
Carlos
picture.png
Reply all
Reply to author
Forward
0 new messages