在 RShiny 中使用模块化的数据表

发布于 2025-01-11 16:29:08 字数 2465 浏览 0 评论 0原文

我正在尝试使用 R 中的 iris 数据集制作一个简单的闪亮仪表板。

到目前为止我所完成的工作:当前仪表板有两个下拉列表。一个用于过滤 Species 列,另一个用于过滤依赖于第一个下拉列表的 subspecies 列。这两个下拉菜单有效。

不起作用:根据两个下拉列表,我想看到一个数据表,它应该是经过过滤的数据集。

我想我使用了错误的名称空间?

任何建议都会有很大帮助!

library(shiny)
library(DT)
library(dplyr)


## global.R
# Create sub_species column
iris2 <- iris %>% 
  mutate(
    subspecies = case_when(
      startsWith(as.character(Species), "setosa") ~ rep(c("setosa1", "setosa2"), length.out = n()),
      startsWith(as.character(Species), "versicolor") ~ rep(c("versicolor1", "versicolor2"), length.out = n()),
      startsWith(as.character(Species), "virginica") ~ rep(c("virginica1", "virginica2"), length.out = n())
    )
  ) 


## ui.R
fluidPage(
  sidebarLayout(
    sidebarPanel(
      dropdownsUI("dropdowns")
    ),
    
    mainPanel(
      DT::dataTableOutput("table1")
    )
  )
)


## server.R
function(input, output, session) {
  subspeciesServer("dropdowns")
  
  data1 <- filteredDataServer("table1")
  output$table1 <- DT::renderDataTable({
    data1()
  })
}


## modules.R
# UI logic
dropdownsUI <- function(id) {
  ns <- NS(id) 
  
  # All input IDs in the function body must be wrapped with ns()
  tagList(
    selectInput(ns("speciesDropdown"), label = "Species:", choices = c("setosa", "versicolor", "virginica")),
    uiOutput(ns("subspeciesDropdown")),
    DT::dataTableOutput(ns("datatable"))
  )
}

# Sub Species Dropdown logic
subspeciesServer <- function(id) {
  moduleServer(id, function(input, output, session) {
    
    dependent_subspecies <- reactive({
      iris2 %>%
        filter(Species == req(input$speciesDropdown)) %>% 
        pull(subspecies) %>%
        unique()
    })
    
    output$subspeciesDropdown <- renderUI({
      selectInput("vars_subspecies", "Sub Species:", choices = dependent_subspecies())
    })
  }
  )
}

# Filtered data logic
filteredDataServer <- function(id) {
  moduleServer(id, function(input, output, session) {
    
    df <- reactive({
      req(input$speciesDropdown, input$subspeciesDropdown)
      
      iris2 %>%
        # may be this what's causing the error ?
        filter(Species %in% input$speciesDropdown & subspecies %in% input$vars_subspecies) 
    })
    return(df)
  }
  )
}

输入图片此处描述

I'm trying to make a simple Shiny dashboard using the iris dataset in R.

What I accomplished so far: The current dashboard has two dropdowns. One that filters the Species column and one for the subspecies column that's dependent on the first dropdown. These two dropdowns work.

What's not working: Based on the two dropdowns, I'd like to see a datatable which should be a filtered dataset.

I think I'm using a wrong name space ?

Any advice would be of great help!

library(shiny)
library(DT)
library(dplyr)


## global.R
# Create sub_species column
iris2 <- iris %>% 
  mutate(
    subspecies = case_when(
      startsWith(as.character(Species), "setosa") ~ rep(c("setosa1", "setosa2"), length.out = n()),
      startsWith(as.character(Species), "versicolor") ~ rep(c("versicolor1", "versicolor2"), length.out = n()),
      startsWith(as.character(Species), "virginica") ~ rep(c("virginica1", "virginica2"), length.out = n())
    )
  ) 


## ui.R
fluidPage(
  sidebarLayout(
    sidebarPanel(
      dropdownsUI("dropdowns")
    ),
    
    mainPanel(
      DT::dataTableOutput("table1")
    )
  )
)


## server.R
function(input, output, session) {
  subspeciesServer("dropdowns")
  
  data1 <- filteredDataServer("table1")
  output$table1 <- DT::renderDataTable({
    data1()
  })
}


## modules.R
# UI logic
dropdownsUI <- function(id) {
  ns <- NS(id) 
  
  # All input IDs in the function body must be wrapped with ns()
  tagList(
    selectInput(ns("speciesDropdown"), label = "Species:", choices = c("setosa", "versicolor", "virginica")),
    uiOutput(ns("subspeciesDropdown")),
    DT::dataTableOutput(ns("datatable"))
  )
}

# Sub Species Dropdown logic
subspeciesServer <- function(id) {
  moduleServer(id, function(input, output, session) {
    
    dependent_subspecies <- reactive({
      iris2 %>%
        filter(Species == req(input$speciesDropdown)) %>% 
        pull(subspecies) %>%
        unique()
    })
    
    output$subspeciesDropdown <- renderUI({
      selectInput("vars_subspecies", "Sub Species:", choices = dependent_subspecies())
    })
  }
  )
}

# Filtered data logic
filteredDataServer <- function(id) {
  moduleServer(id, function(input, output, session) {
    
    df <- reactive({
      req(input$speciesDropdown, input$subspeciesDropdown)
      
      iris2 %>%
        # may be this what's causing the error ?
        filter(Species %in% input$speciesDropdown & subspecies %in% input$vars_subspecies) 
    })
    return(df)
  }
  )
}

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情丝乱 2025-01-18 16:29:08

除了命名空间问题之外,您还遇到了一些其他问题。您需要在模块之间传递反应变量。它们在全球范围内不可用。试试这个

library(shiny)
library(DT)
library(dplyr)

## global.R
# Create sub_species column
iris2 <- iris %>% 
  dplyr::mutate(
    subspecies = case_when(
      startsWith(as.character(Species), "setosa") ~ rep(c("setosa1", "setosa2"), length.out = n()),
      startsWith(as.character(Species), "versicolor") ~ rep(c("versicolor1", "versicolor2"), length.out = n()),
      startsWith(as.character(Species), "virginica") ~ rep(c("virginica1", "virginica2"), length.out = n())
    )
  ) 

## modules.R
# UI logic
dropdownsUI <- function(id) {
  ns <- NS(id) 
  
  # All input IDs in the function body must be wrapped with ns()
  tagList(
    selectInput(ns("speciesDropdown"), label = "Species:", choices = c("setosa", "versicolor", "virginica")),
    uiOutput(ns("subspeciesDropdown"))
    #,DT::dataTableOutput(ns("datatable"))
  )
}

# Sub Species Dropdown logic
subspeciesServer <- function(id) {
  moduleServer(id, function(input, output, session) {
    ns <- session$ns
    rv <- reactiveValues()
    
    dependent_subspecies <- reactive({
      iris2 %>%
        filter(Species == req(input$speciesDropdown)) %>% 
        pull(subspecies) %>%
        unique()
    })
    
    output$subspeciesDropdown <- renderUI({
      req(dependent_subspecies())
      selectInput(ns("vars_subspecies"), "Sub Species:", choices = dependent_subspecies())
    })
    
    observe({
      
      rv$var1 <- input$speciesDropdown
      rv$var2 <- input$vars_subspecies
    })
    return(rv)
  }
  )
}

# Filtered data logic
filteredDataServer <- function(id,sp,subsp,mydf) {
  moduleServer(id, function(input, output, session) {
    
    df <- reactive({
      mydf  %>% dplyr::filter(subspecies %in% subsp())
    })
    
    return(df)
  }
  )
}

## ui.R
ui <-  fluidPage(
    sidebarLayout(
      sidebarPanel(
        dropdownsUI("dropdowns")
      ),
      
      mainPanel(
        DT::dataTableOutput("table1")
      )
    )
)

## server.R
server <- function(input, output, session) {
  myvars <- subspeciesServer("dropdowns")
  
  data1 <- filteredDataServer("table1", reactive(myvars$var1), reactive(myvars$var2),iris2)
 
  output$table1 <- DT::renderDataTable({
    datatable(req(data1()))
  })
}

shinyApp(ui = ui, server = server)

Apart from namespace issue, you had a few other issues. You need to pass the reactive variables between modules. They are not available globally. Try this

library(shiny)
library(DT)
library(dplyr)

## global.R
# Create sub_species column
iris2 <- iris %>% 
  dplyr::mutate(
    subspecies = case_when(
      startsWith(as.character(Species), "setosa") ~ rep(c("setosa1", "setosa2"), length.out = n()),
      startsWith(as.character(Species), "versicolor") ~ rep(c("versicolor1", "versicolor2"), length.out = n()),
      startsWith(as.character(Species), "virginica") ~ rep(c("virginica1", "virginica2"), length.out = n())
    )
  ) 

## modules.R
# UI logic
dropdownsUI <- function(id) {
  ns <- NS(id) 
  
  # All input IDs in the function body must be wrapped with ns()
  tagList(
    selectInput(ns("speciesDropdown"), label = "Species:", choices = c("setosa", "versicolor", "virginica")),
    uiOutput(ns("subspeciesDropdown"))
    #,DT::dataTableOutput(ns("datatable"))
  )
}

# Sub Species Dropdown logic
subspeciesServer <- function(id) {
  moduleServer(id, function(input, output, session) {
    ns <- session$ns
    rv <- reactiveValues()
    
    dependent_subspecies <- reactive({
      iris2 %>%
        filter(Species == req(input$speciesDropdown)) %>% 
        pull(subspecies) %>%
        unique()
    })
    
    output$subspeciesDropdown <- renderUI({
      req(dependent_subspecies())
      selectInput(ns("vars_subspecies"), "Sub Species:", choices = dependent_subspecies())
    })
    
    observe({
      
      rv$var1 <- input$speciesDropdown
      rv$var2 <- input$vars_subspecies
    })
    return(rv)
  }
  )
}

# Filtered data logic
filteredDataServer <- function(id,sp,subsp,mydf) {
  moduleServer(id, function(input, output, session) {
    
    df <- reactive({
      mydf  %>% dplyr::filter(subspecies %in% subsp())
    })
    
    return(df)
  }
  )
}

## ui.R
ui <-  fluidPage(
    sidebarLayout(
      sidebarPanel(
        dropdownsUI("dropdowns")
      ),
      
      mainPanel(
        DT::dataTableOutput("table1")
      )
    )
)

## server.R
server <- function(input, output, session) {
  myvars <- subspeciesServer("dropdowns")
  
  data1 <- filteredDataServer("table1", reactive(myvars$var1), reactive(myvars$var2),iris2)
 
  output$table1 <- DT::renderDataTable({
    datatable(req(data1()))
  })
}

shinyApp(ui = ui, server = server)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文