如何使用DTedit和Shiny :: uiOutput在列中引入新行?

时间:2019-06-14 02:06:59

标签: html shiny newline

我正在使用DTedit pckg在闪亮的应用程序中显示数据框(mydata),因为此简单的R pckg允许我以非常简单的方式添加/编辑行。到目前为止,一切都很好。但是,我想在Var2列中引入新行(或换行符),将FIRST和SECOND分开,将THIRD和FOURTH分开。

我已经能够使用DT :: dataTableOutput(下面的选项1)来做到这一点。但是,DTedit似乎仅适用于Shiny :: uiOutput,并且我一直无法在此处引入新行(选项2)。我已经读过有关div()的文章,但目前我仍然一无所知。

有人可以阐明我如何使用Dtedit在数据框的列中引入新行吗?因此闪亮:: uiOutput?

NB:我得出的结论是Shiny :: uiOutput是这里的问题,因为这是我可以看到的两个选项之间唯一的“明显”区别。但这就是我,可能我不太想念它。

PD:这是我的第一篇文章,所以如果可以做得更好,请告诉我。谢谢!

# OPTION 1: using DT (DT::dataTableOutput) (WORKING)

    ui = fluidPage(
      h3("New line works when using DT (DT::dataTableOutput)",
      mainPanel(
        DT::dataTableOutput("mytable")
        )
      )
    )

    server = function(input, output){

      #dataframe
      mydata <- data.frame(Var1 = c("a", "b"),
                           Var2 = c("FIRST LINE: first; SECOND LINE: second", 
                                    "THIRD LINE: third; FOUR LINE: four"))

      #Subtitute semicolon by break line based on 
      #https://stackoverflow.com/questions/26368192/how-to-insert-new-line-in-r-shiny-string
      mydata$Var2 <- gsub(pattern = "; ", replacement = "<br/>", mydata$Var2)

      #render table
      output$mytable = DT::renderDataTable(escape = F,
            mydata
        )
    }

shinyApp(ui = ui, server = server, options = list(height = 1080))

# OPTION 2: using DTedit, therefore shiny::uiOutput, (not working)

    ui = fluidPage(
      h3("New line does not work when using DTedit-shiny::uiOutput"),
      mainPanel(
        shiny::uiOutput("mytable")
      )
    )

    server = function(input, output){

      #dataframe
      mydata <- data.frame(Var1 = c("a", "b"),
                           Var2 = c("FIRST LINE: first; SECOND LINE: second", 
                                    "THIRD LINE: third; FOUR LINE: four"))

      #Subtitute semicolon by break line based on 
      #https://stackoverflow.com/questions/26368192/how-to-insert-new-line-in-r-shiny-string
      mydata$Var2 <- gsub(pattern = "; ", replacement = "<br/>", mydata$Var2)

      #render table
      output$mytable = DT::renderDataTable(escape = F,
                                           DTedit::dtedit(input, output,
                                                          name = 'mytable',
                                                          thedata = mydata)
      )

    }

shinyApp(ui = ui, server = server, options = list(height = 1080))

想要的结果:

Wanted outcome

到目前为止的实际结果:

Actual outcome

1 个答案:

答案 0 :(得分:1)

这可以通过在render函数中用JavaScript进行替换来实现:

server = function(input, output){

  #dataframe
  mydata <- data.frame(Var1 = c("a", "b"),
                       Var2 = c("FIRST LINE: first; SECOND LINE: second", 
                                "THIRD LINE: third; FOUR LINE: four"))

  #render table
  DTedit::dtedit(
    input, output,
    name = 'mytable',
    thedata = mydata, 
    datatable.options = list(
      columnDefs = list(
        list(targets=1, 
             render = JS("function(data){return data.replace(/;/g, '<br>');}"))
      )))

}

enter image description here