Guest User

Untitled

a guest
Apr 23rd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. require(rhandsontable)
  2. require(shiny)
  3.  
  4. # Create some fake lists
  5. list_1 <- list()
  6. list_2 <- list()
  7. list_elements <- c('first','second')
  8. for (n in 1:length(list_elements)){
  9. list_1[[list_elements[n]]] <- data.frame(matrix(1:4,ncol=4))
  10. list_2[[list_elements[n]]] <- data.frame(matrix(7:9,ncol=3))
  11. if (n == 2){
  12. list_1[[list_elements[n]]] <- data.frame(matrix(1:2,ncol=2),bool=factor('a1',levels=c('a1','a2','a3')))
  13. list_2[[list_elements[n]]] <- data.frame(matrix(10:11,ncol=2),bool=factor('b1',levels=c('b1','b2')))
  14. }
  15. }
  16.  
  17. # What the tables should look like
  18. rhandsontable(data.frame(values=as.numeric(list_1[['first']])), stretchH = "all", rowHeaderWidth = 300, width=600)
  19. rhandsontable(list_1[['second']], stretchH = "all", rowHeaderWidth = 50, height = 300,width=600) %>%
  20. hot_col("bool", allowInvalid = FALSE)
  21. rhandsontable(data.frame(values=as.numeric(list_2[['first']])), stretchH = "all", rowHeaderWidth = 300, width=600)
  22. rhandsontable(list_2[['second']], stretchH = "all", rowHeaderWidth = 50, height = 300,width=600) %>%
  23. hot_col("bool", allowInvalid = FALSE)
  24.  
  25. server <- function(input, output) {
  26.  
  27. values = reactiveValues()
  28. values[["list_1"]] <- list_1
  29. values[["list_2"]] <- list_2
  30.  
  31. # Feed user input back to the list
  32. observe({
  33. if (!is.null(input$out)) {
  34. temp <- hot_to_r(input$out)
  35. if (isolate(input$list_selector) == "list_1") {
  36. assign(values[["list_1"]][[list_element()]],temp)
  37. } else {
  38. assign(values[["list_2"]][[list_element()]],temp)
  39. }
  40. }
  41. })
  42.  
  43. list <- reactive({
  44. if (input$df_selector == "list_1") {
  45. list <- values[["list_1"]]
  46. } else {
  47. list <- values[["list_2"]]
  48. }
  49. list
  50. })
  51.  
  52. list_element <- reactive({
  53. input$list_element_selector
  54. })
  55.  
  56. # list() should give the state of the list element selector
  57. output$out <- renderRHandsontable({
  58. if (!is.null(list_element())){
  59. if (list_element() == 'first'){
  60. rhandsontable(data.frame(values=as.numeric(list()[['first']])), stretchH = "all", rowHeaderWidth = 300, width=600)
  61. } else {
  62. rhandsontable(list()[['second']], stretchH = "all", rowHeaderWidth = 50, height = 300,width=600) %>%
  63. hot_col("bool", allowInvalid = FALSE)
  64. }
  65. }
  66. })
  67. }
  68.  
  69. ui <- fluidPage(sidebarLayout(sidebarPanel(
  70. selectInput(
  71. 'list_selector', 'Select list:',
  72. choices = c('list_1', 'list_2'),
  73. selected = 'list_1'
  74. ),
  75. selectInput(
  76. 'list_element_selector', 'Select element:',
  77. choices = c('first', 'second'),
  78. selected = 'first'
  79. )
  80. ),
  81. mainPanel(rHandsontableOutput("out"))))
  82.  
  83. shinyApp(ui = ui, server = server)
Add Comment
Please, Sign In to add comment