【问题标题】:Update shiny DT based on editable cells user input根据可编辑单元格用户输入更新闪亮的 DT
【发布时间】:2020-12-29 05:39:59
【问题描述】:

一个闪亮的应用小例子:

library(shiny)
library(tidyverse)
library(DT)

ui <- fluidPage(

    # Application title
    titlePanel("blah"),

    sidebarLayout(
        sidebarPanel(
            sliderInput("bins",
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30)
        ),

        # Show a plot of the generated distribution
        mainPanel(
           DT::DTOutput('ex_table')
        )
    )
)

server <- function(input, output) {

    output$ex_table <- DT::renderDataTable(mtcars %>% select(cyl) %>% mutate(blah = cyl + 2), 
                                           selection = 'none', editable = TRUE)
}

# Run the application 
shinyApp(ui = ui, server = server)

如果你运行它,它看起来像:

由于我在renderDataTable() 中添加了editable = TRUE,因此您可以编辑单元格。

提供数据表的表有以下行:

mtcars %>% select(cyl) %>% mutate(blah = cyl + 2)

所以特征 'blah' 应该始终是 cyl + 2 中的任何内容。在屏幕截图中,我添加了 10,000,因此所需的输出将是数据表在按 Enter 后更新以显示 10,002。

这可能吗?我该怎么做?

【问题讨论】:

    标签: r shiny dt


    【解决方案1】:

    您可以关注这些examples
    试试看:

    library(shiny)
    library(tidyverse)
    library(DT)
    
    ui <- fluidPage(
      
      # Application title
      titlePanel("blah"),
      
      sidebarLayout(
        sidebarPanel(
          sliderInput("bins",
                      "Number of bins:",
                      min = 1,
                      max = 50,
                      value = 30)
        ),
        
        # Show a plot of the generated distribution
        mainPanel(
          DT::DTOutput('modtable'),
        )
      )
    )
    
    server <- function(input, output,session) {
      data <- mtcars %>% select(cyl) %>% mutate(blah = cyl + 2)
      
      output$modtable <- DT::renderDT(data, selection = 'none', editable = TRUE)
      
      proxy = dataTableProxy('modtable')
    
      observeEvent(input$modtable_cell_edit, {
        info = input$modtable_cell_edit
        str(info)
        i = info$row
        j = info$col
        v = info$value
        data <<- editData(data, info)
        if(j==1){data[i,j+1]<<-as.numeric(data[i,j])+2}
        replaceData(proxy, data, resetPaging = FALSE) 
      })
    
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      猜你喜欢
      • 2018-04-30
      • 2020-10-06
      • 2019-10-25
      • 2020-11-05
      • 1970-01-01
      • 1970-01-01
      • 2018-08-04
      • 2019-07-15
      • 2021-02-15
      相关资源
      最近更新 更多