【问题标题】:Prevent R shiny handsontable from resetting to default value防止 R shiny handsontable 重置为默认值
【发布时间】:2021-06-26 09:43:42
【问题描述】:

我创建了以下闪亮的应用程序

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,并且表默认为默认状态。有没有办法填充表格,更改行数等,更改名称输入并避免重置表格。我请人看看。

【问题讨论】:

    标签: r shiny shiny-reactivity rhandsontable


    【解决方案1】:

    试试这个

    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)
    

    【讨论】:

    • 谢谢。将数据框创建步骤移到handson 表循环之外是一个重要步骤。应该可以动态改变侧边栏的列数
    猜你喜欢
    • 2016-04-11
    • 2017-05-20
    • 1970-01-01
    • 2018-04-12
    • 2020-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多