【问题标题】:add text comments to a datatable in shiny在闪亮的数据表中添加文本注释
【发布时间】:2019-10-23 11:50:52
【问题描述】:

我正在尝试创建一个闪亮的应用程序,用户可以在其中向表格添加文本评论。

我创建了一个包含 3 列的数据框:numidval。我希望我的闪亮应用执行以下操作:

  1. 从 id 列中选择一个值 (selectInput)。
  2. 在文本框中添加文本注释(textInput)
  3. 点击操作按钮
  4. 在数据表中创建了一个名为comment 的新列,文本 cmets 将添加到 id 等于所选值的行的注释列中。

我闪亮的应用程序代码如下。当我从 selectinput 中选择一个值时,在文本框中添加一些评论并单击“添加评论”按钮,我闪亮的应用程序窗口会自行关闭。

有人知道为什么会这样吗?

提前非常感谢!

    library(shiny)
    library(DT)
    df = data.frame(num=1:10, id=LETTERS[1:10], val=rnorm(10)) 
    ui = fluidPage(
        fluidRow(
            column(2, selectInput(inputId = 'selectID',
                                  label = 'Select ID2',
                                  choices = LETTERS[1:10],
                                  selected='',
                                  multiple=TRUE)),
            column(6, textInput(inputId = 'comment', 
                                label ='Please add comment in the text box:', 
                                value = "", width = NULL,
                                placeholder = NULL)),
            column(2, actionButton(inputId = "button", 
                                   label = "Add Comment"))
        ),
        fluidRow (
            column(12, DT::dataTableOutput('data') ) 
        )           
    )

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

        observeEvent(input$button, {
            df[id==input$selectID, 'Comment']=input$comment
        })

        output$data <- DT::renderDataTable({
            DT::datatable(df, 
                          options = list(orderClasses = TRUE,
                          lengthMenu = c(5, 10, 20), pageLength = 5))
        })


    }

    shinyApp(ui=ui, server=server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:
    1. id 列未被识别为df[id == input$selectId, "Comment] 中的data.frame df 的列,将id 替换为df$id 可修复错误。

    2. 为了在更新df 后重新渲染数据表,df 应该是一个响应式对象。

    3. 要在 selectInput selectId 中处理多个选定的 id,您可能需要将 df$id == input$selectId 替换为 df$id %in% input$selectId

    这个更新的服务器功能应该可以帮助您解决这些问题:

    server <- function(input, output, session) {
    
      ## make df reactive
      df_current <- reactiveVal(df)
    
      observeEvent(input$button, {
    
          req(df_current())
    
          ## update df by adding comments
          df_new <- df_current()
          df_new[df_current()$id %in% input$selectID, "Comment"] <- input$comment
    
          df_current(df_new)
    
      })
    
      output$data <- DT::renderDataTable({
    
          req(df_current())
    
          DT::datatable(df_current(), 
              options = list(orderClasses = TRUE,
                  lengthMenu = c(5, 10, 20), pageLength = 5))
      })
    

    }

    【讨论】:

    • 非常感谢您的解决方案!是的,这适用于这个例子。然而,在我的实际应用程序中,df 实际上是来自dat &lt;- reactive(...) 的反应式数据帧。我用dat() 替换了df。但它没有用。你知道为什么它不起作用吗?对不起,我应该把整个实际案例作为例子。 @Joris Chau
    • 如果df 是根据df &lt;- reactive(...) 的反应对象,它的值不能像reactiveVal 对象那样用df(df_new) 修改。您可以改为定义 df &lt;- eventReactive(c(input$comments, other inputs), {...}),或使用上面的 reactiveVal/observeEvent 构造。也许您可以编辑您的问题以包含 df 的反应性定义?
    • 谢谢!我为此发布了另一个问题。这是链接:stackoverflow.com/questions/56515650/… 再次感谢您对此的帮助。 @Joris Chau
    猜你喜欢
    • 2015-12-20
    • 1970-01-01
    • 2017-12-23
    • 2021-03-08
    • 1970-01-01
    • 2019-10-25
    • 2014-06-07
    • 1970-01-01
    • 2022-08-03
    相关资源
    最近更新 更多