繁体   English   中英

多个选项卡的 R 闪亮页面刷新按钮

[英]R Shiny Page Refresh Button for Multiple Tabs

我正在构建一个 Shiny 仪表板,它有多个选项卡,每个选项卡都在一个独立的页面中,可以从侧边栏中的选项卡项进行定向。

我正在尝试通过点击此处的链接在每个选项卡上添加页面刷新按钮R Shiny 中的页面刷新按钮

但是,我只能将它添加到一个选项卡,当我复制并粘贴其他选项卡的相同代码时失败

以下是我使用的当前结构:

library(shiny)
library(shinyjs)
library(shinydashboard)

jscode <- "shinyjs.refresh = function() { history.go(0); }"

header <- dashboardHeader(

)

sidebar <- dashboardSidebar(
  tags$head(tags$style(HTML('.content-wrapper { height: 1500px !important;}'))),
  sidebarMenu (
    menuItem("A", tabName = "d1"),
    menuItem("B", tabName = "d2"),
    menuItem("C", tabName = "d3")
  )
)

body <- dashboardBody(
  useShinyjs(),
  extendShinyjs(text = jscode),
  tabItems(
    tabItem(tabName = "d1",
            box(title = "AAA",
                actionButton("refresh", "Save"))
    ),
    tabItem(tabName = "d2",
             box(title = "BBB")
    ),
    tabItem(tabName = "d3",
            box(title = "CCC")
    )
  )
)

# UI
ui <- dashboardPage(header, sidebar, body)

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

  observeEvent({
    input$aa
    input$refresh
  })

  observeEvent(input$refresh, {
    js$refresh();
  })

  observeEvent({
    input$bb
  })

  observeEvent({
    input$cc
  })


}

# Shiny dashboard
shiny::shinyApp(ui, server)

基本上,现在我只有在选项卡 1 中名为SAVE的页面刷新按钮用于输入aa

我想知道如何在选项卡 2选项卡 3 上为输入bbcc设置相同的页面刷新按钮。 如果用户单击任何页面上的任何保存按钮,则理想的解决方案是刷新 Shiny 仪表板。

提前致谢

您必须为每个选项卡创建 3 个不同的按钮,然后您可以调用其中之一进行刷新:

library(shiny)
library(shinyjs)
library(shinydashboard)

jscode <- "shinyjs.refresh = function() { history.go(0); }"

header <- dashboardHeader(

)

sidebar <- dashboardSidebar(
  tags$head(tags$style(HTML('.content-wrapper { height: 1500px !important;}'))),
  sidebarMenu (
    menuItem("A", tabName = "d1"),
    menuItem("B", tabName = "d2"),
    menuItem("C", tabName = "d3")
  )
)

body <- dashboardBody(
  useShinyjs(),
  extendShinyjs(text = jscode),
  tabItems(
    tabItem(tabName = "d1",
            box(title = "AAA",
                actionButton("b1", "Save"))
    ),
    tabItem(tabName = "d2",
            box(title = "BBB",
                actionButton("b2", "Save"))
    ),
    tabItem(tabName = "d3",
            box(title = "CCC",
                actionButton("b3", "Save"))
    )
  )
)

# UI
ui <- dashboardPage(header, sidebar, body)

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

  observeEvent(c(input$b1,input$b2,input$b3), {
    js$refresh()
  },ignoreNULL = T,ignoreInit = T)

}

# Shiny dashboard
shiny::shinyApp(ui, server)

以防万一你对shinyJS 不是很感兴趣。 此代码应该可以帮助您。 玩得开心

library(shiny)
library(shinydashboard)


dat = data.frame(id = c("d","a","c","b"), a = c(1,2,3,4), b = c(6,7,8,9))

header <- dashboardHeader(

)

sidebar <- dashboardSidebar(
  tags$head(tags$style(HTML('.content-wrapper { height: 1500px !important;}'))),
  sidebarMenu (
    menuItem("A", tabName = "d1"),
    menuItem("B", tabName = "d2"),
    menuItem("C", tabName = "d3")
  )
)

body <- dashboardBody(

  tabItems(
    tabItem(tabName = "d1",
            box(title = "AAA",
                actionButton("refreshTab1_id", "Refresh Tab 1"),
                actionButton("sortTable1_id", "Sort Table 1"),
                DT::dataTableOutput("table_for_tab_1", width = "100%"))
    ),
    tabItem(tabName = "d2",
             box(title = "BBB",
                actionButton("refreshTab2_id", "Refresh Tab 2"),
                actionButton("sortTable2_id", "Sort Table 2"),
                DT::dataTableOutput("table_for_tab_2", width = "100%"))
    ),
    tabItem(tabName = "d3",
            box(title = "CCC",
                actionButton("refreshTab3_id", "Refresh Tab 3"),
                actionButton("sortTable3_id", "Sort Table 3"),
                DT::dataTableOutput("table_for_tab_3", width = "100%"))
    )
  )
)

# UI
ui <- dashboardPage(header, sidebar, body)

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


    observe({

        if (input$sortTable1_id || input$sortTable2_id || input$sortTable3_id) {
            dat_1 = dat %>% dplyr::arrange(id)
        } else {
            dat_1 = dat
        }

        output$table_for_tab_1 <- output$table_for_tab_2 <- output$table_for_tab_3 <- DT::renderDataTable({ 

            DT::datatable(dat_1, 
                    filter = 'bottom', 
                    selection = "single",
                    colnames = c("Id", "A", "B"),
                    options = list(pageLength = 10,
                                    autoWidth = TRUE#,
                                    # columnDefs = list(list(targets = 9, 
                                    #                        visible = FALSE))
                    )
            )
        })
    })

    observe({
        if (input$refreshTab1_id || input$refreshTab2_id || input$refreshTab3_id) {
            session$reload()
        }
    })

}

# Shiny dashboard
shiny::shinyApp(ui, server)

暂无
暂无

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

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