Raghavan vmvs
Raghavan vmvs

Reputation: 1265

Prevent R shiny handsontable from resetting to default value

I have created the following shiny App

library(shiny)
library(rhandsontable)

ui <- fluidPage(  
sidebarLayout(sidebarPanel = "Inputparameter", 
            selectInput(inputId = "Name", label = "Name", choices = c("A", "B", "C"))),                
            mainPanel (rHandsontableOutput(outputId = 'Adjusttable', width ='100%', height = 100%')))

server <- function(input, output, session) {  
output$Adjusttable<-renderRHandsontable({
DF = data.frame(ID = 1:7,'Column2' = 0, Start = "D",FM="",stringsAsFactors = FALSE)
names(DF)[names(DF)=='Column2']<- input$Name
names(DF)[names(DF)=='FM']<-'FM'
DF$ID<-NULL
rhandsontable(DF, width = 280, height = 677,stretchH = "all")  %>%
  hot_col(col = "Start", type = "dropdown", source = c("Fw", "Sw"), fillHandle = 
  list(direction='vertical', autoInsertRow=TRUE))%>%
  hot_context_menu(allowRowEdit = TRUE, allowColEdit = FALSE)
  }, quoted = FALSE )}

 shinyApp(ui, server)

The following results in an app with an editable table. When we fill values in the table,, and change the item in the name drop down, the values get reset to 0 and the table defaults to its default state. Is there a way to fill the table, change the number of rows, etc , change the name input and avoid resetting the table. I request someone to take a look.

Upvotes: 0

Views: 179

Answers (1)

YBS
YBS

Reputation: 21287

Try this

library(shiny)
library(rhandsontable)
library(DT)

DF <- data.frame(ID = 1:7,Column2 = 0, Start = "D",FM="",stringsAsFactors = FALSE)
names(DF)[names(DF)=='FM']<-'FM'
DF$ID<-NULL

ui <- fluidPage(  
  sidebarLayout(
    sidebarPanel( "Inputparameter", 
                selectInput(inputId = "Name", label = "Name", choices = c("A", "B", "C"))),                
    mainPanel( rHandsontableOutput(outputId = 'hot', width ='100%', height = '100%') 
               , DTOutput("t1")
               ) 
    )
)

server <- function(input, output, session) {

  DF1 <- reactiveValues(data=DF)
  observe({
    input$Name
    names(DF1$data)[1] <- input$Name
  })
  
  output$hot<-renderRHandsontable({
    
    rhandsontable(DF1$data, width = 280, height = 677,stretchH = "all")  %>%
      hot_col(col = "Start", type = "dropdown", source = c("Fw", "Sw"), fillHandle = 
                list(direction='vertical', autoInsertRow=TRUE)) %>%
      
      hot_context_menu(allowRowEdit = TRUE, allowColEdit = FALSE)
  }, quoted = FALSE )
  
  observe({
    if (!is.null(input$hot)){
      DF1$data <- (hot_to_r(input$hot))
    } 
  })
  
  output$t1 <- renderDT(DF1$data)

}

shinyApp(ui, server)

output

Upvotes: 1

Related Questions