【问题标题】:How to automatically navigate to another sidebar after data is uploaded in a sidebar within Shinydashboard?在 Shinydashboard 的侧边栏中上传数据后,如何自动导航到另一个侧边栏?
【发布时间】:2020-02-23 12:03:31
【问题描述】:

您好 Shiny 仪表板专家,

以下 reprex 作品。 即一旦文件上传到侧边栏:mod1, 我们可以导航到侧边栏:mod2 并查看显示的上传数据。

下面是代码: 1. 模块与它的 UI 一起显示 2. 模块通过其 UI 读取数据 调用模块的服务器和 UI。

我们可以自动化吗?

即在侧边栏中上传数据后:mod1,

侧边栏:上传数据的用户应该可以看到 mod2。


library(shiny)
library(tidyverse)

# Module UI to read content
mod_readUI <- function(id) {
  ns <- shiny::NS(id)
  shiny::tagList(
    fileInput(ns("file1"), 
              h3("Choose xlsx file"),
              accept=c(".xlsx")),
    actionButton(ns("ref"), "Refresh")
  )
}

# Server modules of reading content
mod_read <- function(input, output, session){
  # Uploaded data as reactive element
  getData <- reactive({
    req(input$file1) # Ensure file is uploaded
    if(!is.null(input$file1)){
      my_data <- readxl::read_excel(input$file1$datapath)
      my_data
    }
    else{
      my_data <- "nothing" %>% as.data.frame()
      my_data
    }
  })

  ### In order to pass data as reactive elements to other module:
  # Created list
  reactive({
    # browser()
    list("excl" = getData())
  })
}

# Module UI to display content
mod_displayUI <- function(id) {
  ns <- shiny::NS(id)
  shiny::tagList(
    DT::dataTableOutput(ns("contents"))
  )
}

# Server functions
mod_display <- function(input, output, session, file) {
  output$contents <- DT::renderDataTable({
    req(file())
    DT::datatable(file()$excl,
                  options = list(pageLength = 7,scrollX = TRUE))
  })
}


ui <- 
  shinydashboard::dashboardPage(
    shinydashboard::dashboardHeader(),
    shinydashboard::dashboardSidebar(
      shinydashboard::sidebarMenu(id = "menu1",
                                  shinydashboard::menuItem('mod1',
                                                           tabName = 'mod1', 
                                                           icon = shiny::icon('file')),

                                  shinydashboard::menuItem('mod2',
                                                           tabName = 'mod2', 
                                                           icon = shiny::icon('file'))
      )),
    shinydashboard::dashboardBody(
      shinydashboard::tabItems(
        shinydashboard::tabItem("mod1",
                                mod_readUI("sidemod1")),
        shinydashboard::tabItem("mod2",
                                mod_displayUI("bodymod2")
        )
      )))

server <- function(input, output) {
  # storing mod_read in a variable
  readFile1 <- shiny::callModule(mod_read, "sidemod1")
  # passing the output of readFile into mod_display module
  displayFile <- shiny::callModule(mod_display, "bodymod2", file = readFile1)
}

shinyApp(ui,server)

【问题讨论】:

    标签: r shiny shinydashboard


    【解决方案1】:

    我想您可以添加observe 以查看sidemod1-file1 input 何时不是NULL 或更改。发生这种情况时,您可以使用updateTabItems。请注意,您还需要将 session 作为 server 函数参数。

    server <- function(input, output, session) {
      # storing mod_read in a variable
      readFile1 <- shiny::callModule(mod_read, "sidemod1")
      # passing the output of readFile into mod_display module
      displayFile <- shiny::callModule(mod_display, "bodymod2", file = readFile1)
    
      observe({
        req(input$`sidemod1-file1`)
        updateTabItems(session, "menu1", "mod2")
      })
    }
    

    【讨论】:

    • 感谢您的回复。实际上,我用你的替换了服务器功能。另外,``` library(shinydashboard) choice 警告:选择错误:缺少参数“选择”,没有默认值[否堆栈跟踪可用] 上传文件时
    • @Abhi - 我很困惑。首先,您能否确认该行为适用于我的server 代码和您的ui 代码组合?这个对我有用。其次,至于附加代码choice &lt;- reactive ({ input$sel }),我没有看到名称为“sel”的输入,也没有看到choice 在任何地方使用。也许您使用的代码与发布的代码不同?我需要您修改后的代码才能更好地理解错误的含义。如果我遗漏了其他内容,请告诉我。
    • 对不起,是的。我的原始代码中还有一些功能。但是,是的,您的代码有效。
    猜你喜欢
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 2014-07-03
    • 2021-09-28
    • 2016-01-03
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    相关资源
    最近更新 更多