【问题标题】:Reactive shiny modules sharing data反应性闪亮模块共享数据
【发布时间】:2020-03-12 22:13:10
【问题描述】:

我正在尝试使用模块创建一个闪亮的应用程序。两个数据帧(表 a 和 b)是反应性的,可以修改,第三个数据帧(表 c)也是反应性的,基于表 a 和 b。

我尝试关注这个question,它对文本输入而不是数据框做同样的事情,但我的代码不起作用 - 我得到一个object of type closure is not subsettable error

感谢您的帮助。

### Libraries
library(shiny)
library(tidyverse)
library(DT)    

### Data----------------------------------------

table_a <- data.frame(
  id=seq(from=1,to=10),
  x_1=rnorm(n=10,mean=0,sd=10),
  x_2=rnorm(n=10,mean=0,sd=10),
  x_3=rnorm(n=10,mean=0,sd=10),
  x_4=rnorm(n=10,mean=0,sd=10)
) %>% 
  mutate_all(round,3)

table_b <- data.frame(
  id=seq(from=1,to=10),
  x_5=rnorm(n=10,mean=0,sd=10),
  x_6=rnorm(n=10,mean=0,sd=10),
  x_7=rnorm(n=10,mean=0,sd=10),
  x_8=rnorm(n=10,mean=0,sd=10)
)%>% 
  mutate_all(round,3)


### Modules------------------------------------ 

mod_table_a <- function(input, output, session, data_in,reset_a) {

  v <- reactiveValues(data = data_in)
  proxy = dataTableProxy("table_a")

  #set var 2
  observeEvent(reset_a(), {
    v$data[,"x_2"] <- round(rnorm(n=10,mean=0,sd=10),3)
    replaceData(proxy, v$data, resetPaging = FALSE) 
  })

  # render table
  output$table_a <- DT::renderDataTable({

    DT::datatable(
      data=v$data,
      editable = TRUE,
      rownames = FALSE,
      class="compact cell-border",
      selection = list(mode = "single", 
                       target = "row"
                       ),
      options = list(
        dom="t",
        autoWidth=TRUE,
        scrollX = TRUE,
        ordering=FALSE,
        bLengthChange= FALSE,
        searching=FALSE
      )
    )
  })

  return(v)

}

mod_table_b <- function(input, output, session, data_in,reset_b) {

  v <- reactiveValues(data = data_in)
  proxy = dataTableProxy("table_b")

  #reset var
  observeEvent(reset_b(), {
    v$data[,"x_6"] <- round(rnorm(n=10,mean=0,sd=10),3)
    replaceData(proxy, v$data, resetPaging = FALSE)  # replaces data displayed by the updated table
  })

  # render table
  output$table_b <- DT::renderDataTable({

    DT::datatable(
      data=v$data,
      editable = TRUE,
      rownames = FALSE,
      class="compact cell-border",
      selection = list(mode = "single", 
                       target = "row"
      ),
      options = list(
        dom="t",
        autoWidth=TRUE,
        scrollX = TRUE,
        ordering=FALSE,
        bLengthChange= FALSE,
        searching=FALSE
      )
    )
  })

  return(v)
}

mod_table_c <- function(input, output, session, tbl_a_proxy,tbl_b_proxy) {

  v <- reactive({
  table_c <- data.frame(id=seq(from=1,to=10)) %>%
    left_join(tbl_a_proxy$data,by="id") %>%
    left_join(tbl_b_proxy$data,by="id") %>%
    mutate(y_1=x_1+x_6)%>%
    select(x_2,x_6,y_1)
  })


  # render table
  output$table_c <- DT::renderDataTable({

    DT::datatable(
      data=v$table_c,
      editable = TRUE,
      rownames = FALSE,
      class="compact cell-border",
      selection = list(mode = "single", 
                       target = "row"
      ),
      options = list(
        dom="t",
        autoWidth=TRUE,
        scrollX = TRUE,
        ordering=FALSE,
        bLengthChange= FALSE,
        searching=FALSE
      )
    )
  })
}


modFunctionUI <- function(id) {
  ns <- NS(id)
  DT::dataTableOutput(ns(id))
}


### Shiny App---------
#ui----------------------------------
  ui <- fluidPage(
    fluidRow(
      br(),
      column(1,
             br(),
             actionButton(inputId = "reset_a", "Reset a")
             ),
      column(6,
             modFunctionUI("table_a")
      ),
      column(1),
      column(4)
    ),
    fluidRow(
      br(),
      br(),
      column(1,
             br(),
             actionButton(inputId = "reset_b", "Reset b")),
      column(6,
             modFunctionUI("table_b")
      ),
      column(5,
             modFunctionUI("table_c")
             )
    ),
    #set font size of tables
    useShinyjs(),
    inlineCSS(list("table" = "font-size: 10px"))
  )

#server--------------
  server <-  function(input, output) {

    #table a
    tbl_a_proxy <- callModule(module=mod_table_a,
               id="table_a",
               data_in=table_a,
               reset_a = reactive(input$reset_a)
    )

    #table b
    tbl_b_proxy <- callModule(module=mod_table_b,
               id="table_b",
               data_in=table_b,
               reset_b = reactive(input$reset_b)
    )

    #table c
    callModule(module=mod_table_c,
               id="table_c",
               tbl_a_proxy,
               tbl_b_proxy
    )

  }  


  shinyApp(ui, server)

【问题讨论】:

    标签: r module shiny reactive


    【解决方案1】:

    您的问题是在mod_table_c 中,data=v$table_c 没有任何意义,因为v 是反应性的,并且已经对应于您要显示的(反应性)table_c。因此,您需要将其替换为data=v()(因为反应式表达式在使用时需要在其名称后加上())。

    这是您更正后的示例:

    ### Libraries
    library(shiny)
    library(tidyverse)
    library(DT)   
    library(shinyjs)
    
    ### Data----------------------------------------
    
    table_a <- data.frame(
      id=seq(from=1,to=10),
      x_1=rnorm(n=10,mean=0,sd=10),
      x_2=rnorm(n=10,mean=0,sd=10),
      x_3=rnorm(n=10,mean=0,sd=10),
      x_4=rnorm(n=10,mean=0,sd=10)
    ) %>% 
      mutate_all(round,3)
    
    table_b <- data.frame(
      id=seq(from=1,to=10),
      x_5=rnorm(n=10,mean=0,sd=10),
      x_6=rnorm(n=10,mean=0,sd=10),
      x_7=rnorm(n=10,mean=0,sd=10),
      x_8=rnorm(n=10,mean=0,sd=10)
    )%>% 
      mutate_all(round,3)
    
    
    ### Modules------------------------------------ 
    
    mod_table_a <- function(input, output, session, data_in,reset_a) {
    
      v <- reactiveValues(data = data_in)
      proxy = dataTableProxy("table_a")
    
      #set var 2
      observeEvent(reset_a(), {
        v$data[,"x_2"] <- round(rnorm(n=10,mean=0,sd=10),3)
        replaceData(proxy, v$data, resetPaging = FALSE) 
      })
    
      # render table
      output$table_a <- DT::renderDataTable({
    
        DT::datatable(
          data=v$data,
          editable = TRUE,
          rownames = FALSE,
          class="compact cell-border",
          selection = list(mode = "single", 
                           target = "row"
          ),
          options = list(
            dom="t",
            autoWidth=TRUE,
            scrollX = TRUE,
            ordering=FALSE,
            bLengthChange= FALSE,
            searching=FALSE
          )
        )
      })
    
      return(v)
    
    }
    
    mod_table_b <- function(input, output, session, data_in,reset_b) {
    
      v <- reactiveValues(data = data_in)
      proxy = dataTableProxy("table_b")
    
      #reset var
      observeEvent(reset_b(), {
        v$data[,"x_6"] <- round(rnorm(n=10,mean=0,sd=10),3)
        replaceData(proxy, v$data, resetPaging = FALSE)  # replaces data displayed by the updated table
      })
    
      # render table
      output$table_b <- DT::renderDataTable({
    
        DT::datatable(
          data=v$data,
          editable = TRUE,
          rownames = FALSE,
          class="compact cell-border",
          selection = list(mode = "single", 
                           target = "row"
          ),
          options = list(
            dom="t",
            autoWidth=TRUE,
            scrollX = TRUE,
            ordering=FALSE,
            bLengthChange= FALSE,
            searching=FALSE
          )
        )
      })
    
      return(v)
    }
    
    mod_table_c <- function(input, output, session, tbl_a_proxy,tbl_b_proxy) {
    
      v <- reactive({
        table_c <- data.frame(id=seq(from=1,to=10)) %>%
          left_join(tbl_a_proxy$data,by="id") %>%
          left_join(tbl_b_proxy$data,by="id") %>%
          mutate(y_1=x_1+x_6)%>%
          select(x_2,x_6,y_1)
      })
    
    
      # render table
      output$table_c <- DT::renderDataTable({
    
        DT::datatable(
          data=v(),
          editable = TRUE,
          rownames = FALSE,
          class="compact cell-border",
          selection = list(mode = "single", 
                           target = "row"
          ),
          options = list(
            dom="t",
            autoWidth=TRUE,
            scrollX = TRUE,
            ordering=FALSE,
            bLengthChange= FALSE,
            searching=FALSE
          )
        )
      })
    }
    
    
    modFunctionUI <- function(id) {
      ns <- NS(id)
      DT::dataTableOutput(ns(id))
    }
    
    
    ### Shiny App---------
    #ui----------------------------------
    ui <- fluidPage(
      fluidRow(
        br(),
        column(1,
               br(),
               actionButton(inputId = "reset_a", "Reset a")
        ),
        column(6,
               modFunctionUI("table_a")
        ),
        column(1),
        column(4)
      ),
      fluidRow(
        br(),
        br(),
        column(1,
               br(),
               actionButton(inputId = "reset_b", "Reset b")),
        column(6,
               modFunctionUI("table_b")
        ),
        column(5,
               modFunctionUI("table_c")
        )
      ),
      #set font size of tables
      useShinyjs(),
      inlineCSS(list("table" = "font-size: 10px"))
    )
    
    #server--------------
    server <-  function(input, output) {
    
      #table a
      tbl_a_proxy <- callModule(module=mod_table_a,
                                id="table_a",
                                data_in=table_a,
                                reset_a = reactive(input$reset_a)
      )
    
      #table b
      tbl_b_proxy <- callModule(module=mod_table_b,
                                id="table_b",
                                data_in=table_b,
                                reset_b = reactive(input$reset_b)
      )
    
      #table c
      callModule(module=mod_table_c,
                 id="table_c",
                 tbl_a_proxy,
                 tbl_b_proxy
      )
    
    }  
    
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-01
      • 2021-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-10
      • 2018-07-13
      • 1970-01-01
      相关资源
      最近更新 更多