防止 R 闪亮的handsontable 重置为默认值

问题描述

我创建了以下闪亮的应用

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)

以下结果是一个带有可编辑表格的应用。当我们在表中填充值并更改名称下拉列表中的项目时,这些值将重置为 0,并且表认为其认状态。有没有办法填充表格,更改行数等,更改名称输入并避免重置表格。我请人看一下。

解决方法

试试这个

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