[英]add text comments to a datatable in shiny
我正在嘗試創建一個閃亮的應用程序,用戶可以在其中向表添加文本注釋。
我創建了一個包含3列的數據框: num
, id
和val
。 我希望我的閃亮應用程序執行以下操作:
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)
列id
不被識別為所述data.frame的列df
在df[id == input$selectId, "Comment]
,取代id
通過df$id
修正錯誤。
為了在更新df
之后重新呈現數據表, df
應該是一個反應對象。
要處理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))
})
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.