首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R中rhandsontable内部模态的ID

R中rhandsontable内部模态的ID
EN

Stack Overflow用户
提问于 2022-04-05 10:08:28
回答 1查看 113关注 0票数 0

我的目标是通过单击an按钮在模态中显示(有条件的)可编辑rhandsontable。创建和显示表工作得很好,但是我很难在ui中为表正确分配ID,以便识别用户更改。

感谢您的帮助,谢谢:)

代码语言:javascript
复制
library(shiny)

ui <- fluidPage(
  actionButton('openModal', "Open Modal")
  # rHandsontableOutput("DataEditor") # doesn't work
)

server <- function(input, output, session) {
  
  observeEvent(input$openModal, {
    irrelevant_condition <- FALSE # include that rhandsontable doesn't have to appear always
    if (irrelevant_condition == TRUE) {
      return(showModal(modalDialog("Choose some variables to display first")))
    } else {
      # display rhandsontable if user made a valid choice
      showModal(modalDialog(  
        updDataEditor()
      ))
    }
  })
  
  updDataEditor <- function() {
    output$DataEditor <- renderRHandsontable({
      # in real app some conditional calculations leading to a DF called 'current.DF'
      # why function?: in real app with variation, depending on some inputs the user chose
      current.DF <- data.frame(Name = c("Name1", "Name2"), value1 = c(0,0), value2=c(0,0)) # example df
      rhandsontable(current.DF) 
    })
  }
    
  observeEvent(input$DataEditor, {
    # Here's the problem
    # won't get called when DataEditor is modified by the user
    browser() 
    return()
  })
  
}
shinyApp(ui,server)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-05 11:36:12

它的工作方式如下:

代码语言:javascript
复制
library(shiny)
library(rhandsontable)

ui <- fluidPage(
  actionButton('openModal', "Open Modal")
)

server <- function(input, output, session) {
  
  current.DF <- reactive(
    data.frame(Name = c("Name1", "Name2"), value1 = c(0,0), value2=c(0,0))
  )
  
  observeEvent(input$openModal, {
    irrelevant_condition <- FALSE # include that rhandsontable doesn't have to appear always
    if (irrelevant_condition == TRUE) {
      return(showModal(modalDialog("Choose some variables to display first")))
    } else {
      # display rhandsontable if user made a valid choice
      showModal(modalDialog(  
        rHandsontableOutput("DataEditor")
      ))
    }
  })
  
  output$DataEditor <- renderRHandsontable({
    rhandsontable(current.DF()) 
  })
  
  observeEvent(input$DataEditor, {
    print(input$DataEditor$changes)
  })
  
}
shinyApp(ui,server)

是你想要的吗?对于可以更改的dataframe,我将使用一个反应性值。如果只更改了一些单元格,您可以更好地使用set_data函数。

编辑解决注释中提出的问题:

代码语言:javascript
复制
  output$DataEditor <- renderRHandsontable({
    rhandsontable(current.DF()) %>% htmlwidgets::onRender(
      "function(el){var hot = this.hot; setTimeout(function(){hot.render();}, 1000)}"
    )
  })
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71749888

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档