English 中文(简体)
进入另一单元
原标题:call module into another module
  • 时间:2023-12-27 08:17:15
  •  标签:
  • r
  • golem

I want to display a dataframe when a button is clicked. I create a mod for the button and a mod for the dataframe. In the mod_button_server function i call mod_dataframe_server where I create the dataframe and assign it to output

mod_button.R

mod_button_ui <- function(id){ ns <- NS(id); tagList(
    actionButton(ns("button"), id) )}

mod_button_server <- function(id){ moduleServer(id, function(input, output, session){ns <- session$ns
    observeEvent(input$button, {
      print(id)
      mod_dataframe_server("table")
    })})}

mod_dataframe.R

mod_dataframe_ui <- function(id){ ns <- NS(id); tagList(
    tableOutput(ns("mytable")))}

mod_dataframe_server <- function(id){moduleServer(id, function(input, output,session){ ns <- session$ns;
    df <- data.frame(Years = seq(2000, 2009), Colonne2 = rnorm(10), Colonne3 = rnorm(10), Colonne4 = rnorm(10))
    output$mytable <- renderTable({df})})}

app_ui.R

app_ui <- function(request) {tagList(golem_add_external_resources(),
    mod_button_ui("B1"), #mod_button_ui("B2"),
    mod_dataframe_ui("table"),
  )}

app_server.R

app_server <- function(input, output, session) {
  mod_button_server("B1") #mod_button_server("B2")}
问题回答

Option 1

您可从<代码>上生成一个数据框架。 类似:

mod_dataframe_server <- function(id){
  moduleServer( id, function(input, output, session){
    ns <- session$ns
    df <- data.frame(Years = seq(2000, 2009),
                     Colonne2 = rnorm(10),
                     Colonne3 = rnorm(10),
                     Colonne4 = rnorm(10))
    return(df)
  })
}

可在<代码>mod_button_server上查阅。 类似:

mod_button_server <- function(id){
  moduleServer( id, function(input, output, session){
    ns <- session$ns

    observeEvent(input$button, {
      df1 <- mod_dataframe_server(ns("mytable"))
      output$table <- renderTable(df1)
    })
  })
}

在发生违约时可以中止。 圆门模板,因为没有必要。 表格的显示载于mod_button_ui:

mod_button_ui <- function(id){
  ns <- NS(id)
  tagList(
    actionButton(ns("button"), label = "Click"),
    tableOutput(ns("table"))
  )
}

服务器功能与您的尝试相同,app_ui。 looks:

app_ui <- function(request) {
  tagList(
    golem_add_external_resources(),
    fluidPage(
      mod_button_ui("B1"),
    )
  )
}

Option 2

Alternatively, you could use just the button module and remove the dataframe module. Meaning mod_button_server would use observe() and bindEvent() to create and refresh the table upon pressing the button. mod_button_ui, app_ui, and app_server are the same as in the first option.

mod_button_server <- function(id){
  moduleServer( id, function(input, output, session){
    ns <- session$ns

    df1 <- observe({
      output$table <- renderTable(
        data.frame(Years = seq(2000, 2009),
                   Colonne2 = rnorm(10),
                   Colonne3 = rnorm(10),
                   Colonne4 = rnorm(10))
      )
    }) |> bindEvent(input$button)
  })
}

Working Example

采用上述备选办法2。 备选案文1已经列入,但我就此发表了意见。

library(shiny)

mod_dataframe_server <- function(id){
  moduleServer(id, function(input, output, session) {
    ns <- session$ns
    df <- data.frame(Years = seq(2000, 2009),
                     Colonne2 = rnorm(10),
                     Colonne3 = rnorm(10),
                     Colonne4 = rnorm(10))
    return(df)
  })
}

mod_button_server <- function(id){
  moduleServer(id, function(input, output, session) {
    ns <- session$ns
    
    # option 1
    # observeEvent(input$button, {
    #   df1 <- mod_dataframe_server(ns("mytable"))
    #   output$table <- renderTable(df1)
    # })

    # option 2
    df1 <- observe({
      output$table <- renderTable(
        data.frame(Years = seq(2000, 2009),
                   Colonne2 = rnorm(10),
                   Colonne3 = rnorm(10),
                   Colonne4 = rnorm(10))
      )
    }) |> bindEvent(input$button)
  })
}

mod_button_ui <- function(id){
  ns <- NS(id)
  tagList(
    actionButton(ns("button"), label = "Click"),
    tableOutput(ns("table"))
  )
}

app_ui <- function(request) {
  tagList(
    fluidPage(
      mod_button_ui("B1"),
    )
  )
}

app_server <- function(input, output, session) {
  mod_button_server("B1") 
}

shinyApp(
  ui = app_ui,
  server = app_server
)




相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签