R 将文本注释添加到数据库中的数据表中

R 将文本注释添加到数据库中的数据表中,r,shiny,R,Shiny,我正在尝试创建一个闪亮的应用程序,用户可以在其中向表中添加文本注释 我创建了一个包含3列的数据框:num、id和val。我希望我的闪亮应用程序执行以下操作: 从id列中选择一个值(选择输入) 在文本框中添加文本注释(文本输入) 点击一个动作按钮 在数据表中创建一个名为comment的新列,文本注释添加到行中的注释列中,其中id等于所选值 下面是我闪亮的应用程序代码。当我从selectinput中选择一个值,在文本框中添加一些注释并单击“添加注释”按钮时,我闪亮的应用程序窗口自动关闭 有人知道为什

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

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

  • 从id列中选择一个值(选择输入)
  • 在文本框中添加文本注释(文本输入)
  • 点击一个动作按钮
  • 在数据表中创建一个名为
    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)
    
    库(闪亮)
    图书馆(DT)
    df=data.frame(num=1:10,id=LETTERS[1:10],val=rnorm(10))
    ui=fluidPage(
    fluidRow(
    第(2)列,选择输入(inputId='selectID',
    标签='选择ID2',
    选择=字母[1:10],
    选定=“”,
    多重=真),
    第(6)列,textInput(inputId='comment',
    label='Please在文本框中添加注释:',
    值=”,宽度=空,
    占位符=空),
    列(2,actionButton)(inputId=“button”,
    label=“添加注释”))
    ),
    fluidRow(
    列(12,DT::dataTableOutput('data'))
    )           
    )
    服务器
    
  • id
    未被识别为数据列。在
    df[id==input$selectId,“Comment]
    中,将
    id
    替换为
    df$id
    修复了错误

  • 为了在更新
    df
    后重新提交数据表,
    df
    应该是一个反应对象

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

  • 此更新的服务器功能应可帮助您解决以下问题:

    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))
      })
    

    server非常感谢您的解决方案!是的,这个例子很有用。但是,在我的实际应用程序中,
    df
    实际上是一个反应数据帧,如果
    df
    是一个反应对象,根据
    df谢谢!我已经发布了另一个问题。这里是链接:再次感谢您的支持乔里斯·乔伊在网站上添加了一个回复