Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require(rhandsontable)
- require(shiny)
- # Create some fake lists
- list_1 <- list()
- list_2 <- list()
- list_elements <- c('first','second')
- for (n in 1:length(list_elements)){
- list_1[[list_elements[n]]] <- data.frame(matrix(1:4,ncol=4))
- list_2[[list_elements[n]]] <- data.frame(matrix(7:9,ncol=3))
- if (n == 2){
- list_1[[list_elements[n]]] <- data.frame(matrix(1:2,ncol=2),bool=factor('a1',levels=c('a1','a2','a3')))
- list_2[[list_elements[n]]] <- data.frame(matrix(10:11,ncol=2),bool=factor('b1',levels=c('b1','b2')))
- }
- }
- # What the tables should look like
- rhandsontable(data.frame(values=as.numeric(list_1[['first']])), stretchH = "all", rowHeaderWidth = 300, width=600)
- rhandsontable(list_1[['second']], stretchH = "all", rowHeaderWidth = 50, height = 300,width=600) %>%
- hot_col("bool", allowInvalid = FALSE)
- rhandsontable(data.frame(values=as.numeric(list_2[['first']])), stretchH = "all", rowHeaderWidth = 300, width=600)
- rhandsontable(list_2[['second']], stretchH = "all", rowHeaderWidth = 50, height = 300,width=600) %>%
- hot_col("bool", allowInvalid = FALSE)
- server <- function(input, output) {
- values = reactiveValues()
- values[["list_1"]] <- list_1
- values[["list_2"]] <- list_2
- # Feed user input back to the list
- observe({
- if (!is.null(input$out)) {
- temp <- hot_to_r(input$out)
- if (isolate(input$list_selector) == "list_1") {
- assign(values[["list_1"]][[list_element()]],temp)
- } else {
- assign(values[["list_2"]][[list_element()]],temp)
- }
- }
- })
- list <- reactive({
- if (input$df_selector == "list_1") {
- list <- values[["list_1"]]
- } else {
- list <- values[["list_2"]]
- }
- list
- })
- list_element <- reactive({
- input$list_element_selector
- })
- # list() should give the state of the list element selector
- output$out <- renderRHandsontable({
- if (!is.null(list_element())){
- if (list_element() == 'first'){
- rhandsontable(data.frame(values=as.numeric(list()[['first']])), stretchH = "all", rowHeaderWidth = 300, width=600)
- } else {
- rhandsontable(list()[['second']], stretchH = "all", rowHeaderWidth = 50, height = 300,width=600) %>%
- hot_col("bool", allowInvalid = FALSE)
- }
- }
- })
- }
- ui <- fluidPage(sidebarLayout(sidebarPanel(
- selectInput(
- 'list_selector', 'Select list:',
- choices = c('list_1', 'list_2'),
- selected = 'list_1'
- ),
- selectInput(
- 'list_element_selector', 'Select element:',
- choices = c('first', 'second'),
- selected = 'first'
- )
- ),
- mainPanel(rHandsontableOutput("out"))))
- shinyApp(ui = ui, server = server)
Add Comment
Please, Sign In to add comment