【问题标题】:R shiny: save data frames from multiple panelsR闪亮:从多个面板保存数据帧
【发布时间】:2021-02-13 14:46:31
【问题描述】:

在以下应用程序中,我想添加一个全局按钮,以同时将表格保存在 2 个面板中。 理想情况下,它们应该保存到一个 xlsx 文件中,在以相应选项卡命名的选项卡中。 请注意,这些选项卡是使用模块创建的。

非常感谢!!

library(shiny)
library(DT)

modDtUi <- function(id){ # UI module
  ns = NS(id)
  DT::dataTableOutput(ns('x1'))
}


modDt <-  function(input, output, session, data, globalSession){ # Server module
  
  x <- data
  output$x1 <- DT::renderDataTable(x, selection = 'none', editable = TRUE)
  
  proxy <- dataTableProxy('x1', session = globalSession)
  

  
}



ui <- fluidPage(
  mainPanel(
    tabsetPanel(
    
      tabPanel("Table1", modDtUi("editable")),
      tabPanel("Table2", modDtUi("editable2"))
    )
  )
)


server <- function(input, output, session) {
  callModule(modDt,"editable", data = head(iris,10), globalSession = session)
  
  callModule(modDt,"editable2", data = tail(iris,5), globalSession = session)
}

shinyApp(ui = ui, server = server)


【问题讨论】:

    标签: r shiny


    【解决方案1】:

    我相信这个演示有效。

    我使用reactiveValuesv$data将数据存储在模块内。该模块将returnv$data,因此当您想要将数据保存在闪亮的server 中时可以检索它。

    我还添加了一个observeEvent来检测数据的变化,并用replaceData更新数据表。

    excel 文件是使用writexl 库创建的,但您当然可以用其他文件替换。

    让我知道这是否适合您。我想这个答案有一些可以改进的元素 - 如果我们能识别它们,想进一步编辑。

    library(shiny)
    library(DT)
    library(writexl)
    
    modDtUi <- function(id){ # UI module
      ns = NS(id)
      DT::dataTableOutput(ns(id))
    }
    
    modDt <-  function(input, output, session, data, id, globalSession){ # Server module
      
      v <- reactiveValues(data = data)
      
      output[[id]] <- DT::renderDataTable(v$data, selection = 'none', editable = TRUE)
      
      proxy <- dataTableProxy(id, session = globalSession)
      
      id_input = paste(id, "cell_edit", sep = "_")
      
      # Could add observeEvent here to detect edit event
      observeEvent(input[[id_input]], {
        info = input[[id_input]]
        if (!is.null(info)) {
          v$data[info$row, info$col] <<- DT::coerceValue(info$value, v$data[info$row, info$col])
        }
        replaceData(proxy, v$data, resetPaging = FALSE)
      })
      
      return(data = reactive({v$data}))
    }
    
    ui <- fluidPage(
      sidebarLayout(
        sidebarPanel(
          width = 2,
          actionButton("btn", "Save Both")
        ),
        mainPanel(
          tabsetPanel(
            tabPanel("Table1", modDtUi("editable1")),
            tabPanel("Table2", modDtUi("editable2"))
          )
        )
      )
    )
    
    server <- function(input, output, session) {
      
      e1 <- callModule(modDt, "editable1", data = head(iris,10), id = "editable1", globalSession = session)
      e2 <- callModule(modDt, "editable2", data = tail(iris,5), id = "editable2", globalSession = session)
      
      observeEvent(input$btn, {
        print("Saving...")
        sheets <- list("e1" = e1(), "e2" = e2())
        write_xlsx(sheets, "test.xlsx")
      })
      
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 谢谢!它就像魅力一样,我可以很容易地把它改编成我的完整剧本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-14
    相关资源
    最近更新 更多