【发布时间】:2019-10-23 11:50:52
【问题描述】:
我正在尝试创建一个闪亮的应用程序,用户可以在其中向表格添加文本评论。
我创建了一个包含 3 列的数据框:num、id 和 val。我希望我的闪亮应用执行以下操作:
- 从 id 列中选择一个值 (selectInput)。
- 在文本框中添加文本注释(textInput)
- 点击操作按钮
- 在数据表中创建了一个名为
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)
【问题讨论】: