how to get replacedata to work

490 views
Skip to first unread message

glennm...@me.com

unread,
Nov 13, 2016, 9:49:35 AM11/13/16
to Shiny - Web Framework for R
All,

I have a datatable and I would like to sequentially replace columns in the data based on the user's input - each user input choice will update a column from left to right.  I read the documentation which indicates the replacement table must have the same number of columns as the table.  I also looked at the example at the bottom of http://rstudio.github.io/DT/shiny.html - I have a minimal example below based on my understanding but the table data is not replaced.  In this example, I created two datatables one whose values are 2 and the other whose values are 4.  I would assume the datatable that is rendered would have values of 4 since the table whose values are 2 has been replaced.  What am I doing wrong?

Glenn

#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(
   
DT::dataTableOutput("YieldAnalysis")
)
# Define server logic required to draw a histogram
DataTableOptions <- list(autowidth = TRUE,
                         searching = FALSE,
                         info = FALSE,
                         ordering = FALSE,
                         paging = FALSE)

  
  YieldTable <- datatable(data = array(data = 2,
                                       dim = c(9,7),
                                       dimnames = list(c("Scenario",
                                                         "Life CPR",
                                                         "Yield",
                                                         "Avg. Life",
                                                         "Modified Duration",
                                                         "First Payment",
                                                         "Last Payment",
                                                         "Curve Spread",
                                                         "Zero Vol Spread"),
                                                       rep("Scenario",7)))
                          ,options = DataTableOptions
                          ,rownames = TRUE
                          ,colnames = ""
                          ,escape = TRUE)
   
  YieldTableReplace <- array(data = 4, dim = c(9,7),
                             dimnames = list(c("Scenario",
                                               "Life CPR",
                                               "Yield",
                                               "Avg. Life",
                                               "Modified Duration",
                                               "First Payment",
                                               "Last Payment",
                                               "Curve Spread",
                                               "Zero Vol Spread"),
                                             rep("Scenario",7)))
server <- function(input, output) { 
  
  output$YieldAnalysis <- renderDataTable({YieldTable})
  YieldAnalysisProxy <- dataTableProxy("YieldAnalysis")
  reactive({replaceData(YieldAnalysisProxy, YieldTableReplace)})

}

# Run the application 
shinyApp(ui = ui, server = server)

Joe Cheng

unread,
Nov 13, 2016, 10:41:58 AM11/13/16
to glennm...@me.com, Shiny - Web Framework for R
replaceData is a side-effecty operation and therefore belongs as part of an observer, not reactive expression. If you haven't watched my two videos on reactive programming, I'd definitely recommend doing that.
https://www.rstudio.com/resources/webinars/shiny-developer-conference/

In this toy example I suspect you still may not get what you want as the replaceData may be attempted before the data table is rendered for the first time. But if you tie the action to an actionButton or something you may get what you want.
--
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/0889808d-0bd5-4c28-b0b7-220f889b922e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

glennm...@me.com

unread,
Nov 13, 2016, 12:21:54 PM11/13/16
to Shiny - Web Framework for R, glennm...@me.com
Hi Joe,

Thanks, I did not know those videos were available - I will watch them this afternoon.  Since the action will ultimately be tied to a user input the second issue will go away.  
Reply all
Reply to author
Forward
0 new messages