简体   繁体   English

R shiny,可编辑 DT 表模块,以反应数据作为输入

[英]R shiny, Editable DT table Module with reactive data as input

I am trying to create a editable shiny DT table module.我正在尝试创建一个可编辑的 shiny DT 表模块。 It works well when I pass in iris data.当我传入虹膜数据时效果很好。 However, when I tried to pass a reactive value, the module does not work.但是,当我尝试传递反应值时,模块不起作用。 Does anyone had similar experience before?以前有没有人有过类似的经历? Could you share your thought?你能分享你的想法吗?

library(shiny)
library(DT)

editableUI<-function(id){
  ns <- NS(id)
  DT::dataTableOutput(ns("mod_table"))
}

editableUIServer<-function(id,data,disable_col,change){
  moduleServer(id,
               function(input,output,session){
                 print('hi')
                 v<-reactiveValues(data = data)
                 print('here we got v')
                 print(v$data)

                 output$mod_table <- renderDT(v$data,
                                             editable = list(target = 'cell',
                                                             disable = list(columns=c(disable_col))))
                 proxy = dataTableProxy('mod_table')
                 observeEvent(input$mod_table_cell_edit, {
                  info = input$mod_table_cell_edit
                  v$data <<- editData(v$data, info)
                  replaceData(proxy, v$data, resetPaging = FALSE)
                })
                 observeEvent(change(),{
                   v$data<<-data
                 })
                 
    return(v)
}
)}

# Define UI for application that draws a histogram
ui <- fluidPage(
      fluidPage(
        fluidRow(pickerInput('spec',label = 'Species',choices = unique(as.character(iris$Species)),selected = "versicolor")
                 ),
        fluidRow(editableUI("test")))

)

# Define server logic required to draw a histogram
server <- function(input, output) {
  data<-reactive({
    iris %>% filter(Species %in% c(input$spec))
    })
  observe({print(data())})
  
  v<-reactive({editableUIServer("test",data(),c(1), change_ppg = reactive(input$spec))})
}

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

I changed a couple of lines and now it works.我改变了几行,现在它可以工作了。 Not sure why it was wrong in the first place, though虽然不知道为什么它首先是错误的


library(shiny)
library(DT)



editableUI<-function(id){
  ns <- NS(id)
  DT::dataTableOutput(ns("mod_table"))
}

editableUIServer<-function(id,data,disable_col){
  moduleServer(id,
               function(input,output,session){
                 print('hi')
                 v<-reactiveValues(data = data)
                 print('here we got v')
                

                 output$mod_table <- renderDT(v$data,
                                             editable = list(target = 'cell',
                                                             disable = list(columns=c(disable_col))))
                 proxy = dataTableProxy('mod_table')
                 observeEvent(input$mod_table_cell_edit, {
                  info = input$mod_table_cell_edit
                  v$data <<- editData(v$data, info)
                  replaceData(proxy, v$data, resetPaging = FALSE)
                })
             
                 
    return(v)
}
)}

# Define UI for application that draws a histogram
ui <- fluidPage(
      fluidPage(
        fluidRow(pickerInput('spec',label = 'Species',choices = unique(as.character(iris$Species)),selected = "versicolor")
                 ),
        fluidRow(editableUI("test")))

)

# Define server logic required to draw a histogram
server <- function(input, output) {
  data<-reactive({
    iris %>% filter(Species %in% c(input$spec))
    })

 # v<-reactive({editableUIServer("test",data =iris,c(1), change_ppg = reactive(input$spec))})
  v<- reactive(editableUIServer("test",data(),c(1)))
  observe(print(v()$data))
}

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


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

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