簡體   English   中英

向閃亮的數據表添加文本注釋

[英]add text comments to a datatable in shiny

我正在嘗試創建一個閃亮的應用程序,用戶可以在其中向表添加文本注釋。

我創建了一個包含3列的數據框: numidval 我希望我的閃亮應用程序執行以下操作:

  1. 從id列中選擇一個值(selectInput)。
  2. 在文本框中添加文本注釋(textInput)
  3. 點擊一個動作按鈕
  4. 數據表中將創建一個稱為comment的新列,文本注釋將添加到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)
  1. id不被識別為所述data.frame的列dfdf[id == input$selectId, "Comment] ,取代id通過df$id修正錯誤。

  2. 為了在更新df之后重新呈現數據表, df應該是一個反應對象。

  3. 要處理selectInput selectId中的多個選定id ,您可能希望將df$id == input$selectIddf$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))
  })

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM