繁体   English   中英

R shiny 带保存按钮的模块

[英]R shiny module with save button

我正在设置一个模块,目的是在同一个应用程序中的不同选项卡中有两个 data.tables。 我希望能够分别编辑和保存每个表。 在我的代码中,只有第一个“保存”按钮有效,它保存了两个 data.tables。

理想情况下,每个保存按钮都应该起作用并且只保存相应的表。

重要提示:我使用DTedit的修改版本:

devtools::install_github('DavidPatShuiFong/DTedit@2.2.1')

这是我有问题的代码:

library(shiny)
library(DTedit)

myModuleUI <- function(id,nam) {
  ns <- shiny::NS(id)
  shiny::tagList(
    
    br(),
    
    ##### needs corrections!!
    
    tabsetPanel(tabPanel("XXX",  dteditmodUI(ns(nam)),actionButton(ns("reset"), "Reset to Saved", styleclass = "warning"),  actionButton(ns("saveBtn"), label = "save"),  br(),
                         id=ns('tabset'), type = 'tabs')
    )
  )
  ####
}

myModule <- function(input, output, session,df,nam,taby,wb) {
  
  dfr=reactiveVal()
  dfr(df)
  
  
  Grocery_List_Results <- shiny::callModule(
    dteditmod,
    id = nam,
    thedata =dfr)
    
   

  # ### save  part
  
  savd = data.frame(isolate(dfr()))
  
  observeEvent(input$saveBtn, {
    
    print("Q")
    ## Add worksheets
    st = paste(taby,as.character(unclass(Sys.time())),sep="_")
    addWorksheet(wb, st)
    
    writeData(x = Grocery_List_Results$thedata,
              wb = wb,
              sheet = st)
    
    
    saveWorkbook(wb, "wb.xlsx", overwrite = T)
    
    savd <<- Grocery_List_Results$thedata
    
    
    shinyalert(title = "Saved!", type = "success")
  })
  
  observeEvent(input$reset, {
    dfr(savd)
    print(dfr)
    
    shinyalert(title = "Reset to saved data!", type = "info")
  })
  
  
}






########

ui <- fluidPage(
  h3('Grocery List'),
  myModuleUI('myModule1',nam="groc"),br(),
  myModuleUI('myModule1',nam="groc2")
)

server <- function(input, output, session) {
  
  df= data.frame(
    Buy = c('Tea', 'Biscuits', 'Apples',"Tea","Apples"),
    Quantity = c(7, 2, 5,9,44),
    stringsAsFactors = FALSE
  )
  
  file = "AICs.xlsx"
  wb <- loadWorkbook(file)
  
  
  shiny::callModule(myModule, 'myModule1',nam="groc",df=df,taby="Tea",wb)
  shiny::callModule(myModule, 'myModule1',nam="groc2",df=df,taby="Apples",wb)
}


shinyApp(ui = ui, server = server)

珍惜你的时间!

最后,感谢这个线程,我似乎设法解决了这个问题:

library( "openxlsx"  )
library("shiny" )    
library(shinyalert)
library(shinysky)
library(DTedit) ## used the modified version from https://github.com/DavidPatShuiFong/DTedit
#installed with devtools::install_github('DavidPatShuiFong/DTedit@2.2.1')


results_2_UI <- function(id,nam) {
  useShinyalert()
  ns <- NS(id)
  tabPanel(
    title = "Export1",
      dteditmodUI(ns(nam)),
    actionButton(ns("reset"), "Reset to Saved", styleclass = "warning"),  actionButton(ns("saveBtn"), label = "save")
    
  )
}

results_3_UI <- function(id,nam) {
  useShinyalert()
  ns <- NS(id)
  tabPanel(
    title = "Export2",
    dteditmodUI(ns(nam)),
    actionButton(ns("reset"), "Reset to Saved", styleclass = "warning"),  actionButton(ns("saveBtn"), label = "save")
    
  )
}

results <- function(input, output, session,df,nam,taby,wb) {
  ## do some complicated data transformations
  dfr=reactiveVal()
  dfr(df)
  
  
  Grocery_List_Results <- shiny::callModule(
    dteditmod,
    id = nam,
    thedata =dfr)
  
  
  
  # ### save  part
  
  savd = data.frame(isolate(dfr()))
  
  observeEvent(input$saveBtn, {
    
    print("Q")
    ## Add worksheets
    st = paste(taby,as.character(unclass(Sys.time())),sep="_")
    addWorksheet(wb, st)
    
    writeData(x = Grocery_List_Results$thedata,
              wb = wb,
              sheet = st)
    
    
    saveWorkbook(wb, "wb.xlsx", overwrite = T)
    
    savd <<- Grocery_List_Results$thedata
    
    
    shinyalert :: shinyalert(title = "Saved!", type = "success")
  })
  
  observeEvent(input$reset, {
    dfr(savd)
    print(dfr)
    
    shinyalert::shinyalert(title = "Reset to saved data!", type = "info")
  })
  

}
### module end

ui <- fluidPage(
  tabsetPanel(
    id = "tabs",
 
    # results_1_UI(id = "test1"),
    results_2_UI(id = "test2",nam="groc"),
    results_3_UI(id = "test3",nam="groc2")
  )
)

server <- function(input, output, session) {
  
  df= data.frame(
    Buy = c('Tea', 'Biscuits', 'Apples',"Tea","Apples"),
    Quantity = c(7, 2, 5,9,44),
    stringsAsFactors = FALSE
  )
  
  file = "AICs.xlsx"
  wb <- loadWorkbook(file)
  
 
  callModule(
    module = results,
    id = "test2",
    nam="groc",df=df,taby="groc",wb=wb
  )
  
  callModule(
    module = results,
    id = "test3",
    nam="groc2",df=df,taby="groc2",wb=wb
  )
}

shinyApp(ui = ui, server = server)

手指交叉,没关系!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM