【问题标题】:R Shiny: Updating proxy table column headers in ObserveEventR Shiny:在 ObserveEvent 中更新代理表列标题
【发布时间】:2021-12-26 21:38:32
【问题描述】:

我想更新 R Shiny 代理表中的列标题。应用应该:

  1. 使用原始列标题名称启动(例如“Do”、“Re”、“Mi”、“Fa”、“So”)
  2. 当用户单击操作按钮时,将代理表中的列标题更改为其他内容(例如“y1”、“y2”、“y3”、“y4”、“y5”)

Shiny 有一个方便的 updateCaption() 方法,它允许代理表标题的类似行为。我想对代理表的表列标题做类似的事情。这是我的尝试。

library(shiny)
library(DT)

ui <- fluidPage(

  fluidRow(
    actionButton(
      "updatebutton",
      label = "Update Table",
      style = "margin-right: 5px;"
    ),
    DT::dataTableOutput("myplot")
  ),
)

server <- function(input, output) {

  mycolumnnames <-c("Do","Re","Mi","Fa","So")
  myothercolumnnames <- c("y1","y2","y3","y4","y5")
  output$myplot <- DT::renderDataTable({
    DF <- data.frame(replicate(5, sample(rnorm(5), 10, rep = TRUE)))
    datatable(DF, colnames = mycolumnnames,
              caption="Original caption")
  })

  proxy <- DT::dataTableProxy("myplot")

  observeEvent(input$updatebutton, {
    updateCaption(proxy, caption="Look, I am a NEW caption!")
    DF <- data.frame(replicate(5, sample(rnorm(5), 10, rep = TRUE)))
    # names(DF) <- myothercolumnnames # This doesn't work
    proxy %>% replaceData(DF)
  })
}

shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: shiny reactive dt


    【解决方案1】:

    Edit1:现在使用dataTableProxy()

    我带走了所有与颜色背景相关的东西,以便我可以专注于您的问题。

    首先,我在 Shiny 之外声明了一些值:您的 data.frame 和列名的两个向量。然后我将列名指定为第一个向量。

    在应用程序中,我以reactiveVal() 检索数据,并在按下按钮时更新其colnames

    library(shiny)
    library(DT)
    
    mycolumnnames <-c("Do","Re","Mi","Fa","So")
    myothercolumnnames <- c("y1","y2","y3","y4","y5")
    DF <- data.frame(replicate(5, sample(rnorm(5), 10, rep = TRUE)))
    colnames(DF) <- mycolumnnames
    
    ui <- fluidPage(
      
      fluidRow(
        actionButton(
          "updatebutton",
          label = "Update Table",
          style = "margin-right: 5px;"
        ),
        DT::dataTableOutput("myplot")
      ),
    )
    
    server <- function(input, output) {
      
      df <- reactiveVal(DF)
      
      output$myplot <- DT::renderDataTable({
        datatable(df(), caption="Original caption")
      })
      
      observeEvent(input$updatebutton, {
        new_data <- data.frame(replicate(5, sample(rnorm(5), 10, rep = TRUE)))
        
        if(!input$updatebutton %% 2 == 0 ){
          colnames(new_data) <- myothercolumnnames
        } else {
          colnames(new_data) <- mycolumnnames 
        }
        df(new_data)
        proxy1 <- DT::dataTableProxy("myplot")
        updateCaption(proxy1, caption="Look, I am a NEW caption!")
        replaceData(proxy1, df())
      })
    }
    
    shinyApp(ui = ui, server = server)
    
    
    

    因此,每当您按下按钮时,两个向量之间的列名都会更改。

    【讨论】:

    • 感谢您的洞察力,帕博特。我按照您的指示修改了问题中的示例代码以专注于该问题。至于您的解决方案,它不适用于我的应用程序,因为我需要使用 replaceData() + 代理表替换数据。除非有办法使列标题具有响应性,以便代理表在调用 replaceData 时更新它们?
    • 我将答案更改为使用 replaceData() 和代理表。
    • 这成功了!非常感谢你的帮助,帕博特。我将您的回复标记为已接受的答案。我也尝试过投票,但我没有必要的 15 个“声誉点”来投票。
    猜你喜欢
    • 1970-01-01
    • 2017-11-06
    • 2021-02-13
    • 2022-01-19
    • 1970-01-01
    • 2018-12-05
    • 2018-12-14
    • 2019-08-01
    • 1970-01-01
    相关资源
    最近更新 更多