【问题标题】:update table in rshiny (using dttable) and save the new information into a variable更新 r shiny 中的表(使用数据表)并将新信息保存到变量中
【发布时间】:2022-10-17 03:19:29
【问题描述】:

我有一张表,用户可以在其中更改数据,更新后的数据将用于将来的计算。 这是一个表格的例子,我想要它,以便在修改表格时,主面板上的必要信息将相应更新。 这是我的代码:

library(ggplot2)
library(DT)
library(shiny)

ui <- fluidPage(
  sidebarLayout(sidebarPanel(
    DTOutput("mytable"),
    actionButton("update", "Update")
  ),
                mainPanel(
                  plotOutput("plot"),
                  verbatimTextOutput("text")
                  )
  )
)

server <- function(input, output, session) {
  
  tab <- reactiveValues(df = {data.frame(
    num = 1:5, 
    x = LETTERS[1:5],
    y = c(14,5,8,9,13)
  )})
  
  output$mytable <- renderDT({
    DT::datatable(tab$df, editable = T, selection = "none")
  })
  
  observeEvent(input$update,{
    output$plot <- renderPlot({
      tab$df %>% ggplot(aes(x,y)) + geom_point()
      
    })
    
    output$text <- renderPrint({
      tab$df$x
    })
    
  })
  
  
}

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny shiny-reactivity


    【解决方案1】:

    用你的server 方法试试这个方法。

    首先,添加observeEvent 以检测对数据表的编辑/更改。如果有,更改将存储在您的 tab 中,这是反应式的。

    其次,如果你想要一个actionbutton 来重做绘图和文本,那么还需要第二个reactiveValues rvobserveEvent 在按下按钮时存储和更新它们。

    server <- function(input, output, session) {
      
      tab <- reactiveValues(df = {data.frame(
        num = 1:5, 
        x = LETTERS[1:5],
        y = c(14,5,8,9,13)
      )})
      
      rv <- reactiveValues(
        plot = NULL,
        text = NULL
      )
      
      output$mytable <- renderDT({
        DT::datatable(tab$df, editable = T, selection = "none")
      })
      
      observeEvent(input$mytable_cell_edit, {
        row <- input$mytable_cell_edit$row
        clmn <- input$mytable_cell_edit$col
        tab$df[row, clmn] <- input$mytable_cell_edit$value
      })
      
      observeEvent(input$update,{
        rv$text <- tab$df$x
        rv$plot <- tab$df %>% 
          ggplot(aes(x,y)) + 
            geom_point()
      })
      
      output$plot <- renderPlot({
        rv$plot
      })
        
      output$text <- renderPrint({
        rv$text
      })
      
    }
    

    【讨论】:

      猜你喜欢
      • 2021-10-06
      • 2021-04-05
      • 1970-01-01
      • 2021-09-27
      • 2019-06-04
      • 2021-09-16
      • 1970-01-01
      • 2022-01-11
      • 1970-01-01
      相关资源
      最近更新 更多