R闪亮的采购UI独立于每个面板的文件

ecbunoof  于 2023-09-27  发布在  其他
关注(0)|答案(1)|浏览(67)

我试图从单独的文件建立用户界面,以保持一切有组织,但运行到问题时,采购他们到新的水龙头.下面是一个简单的例子,我源两个文件(这些可以在下面找到)。源代码1是完全好的,但当我添加第二个源代码,第一页将采取UI布局,从,或它去狂暴。也许我遗漏了什么或者这不是我们应该做的。我想这会让一切都很干净。我需要做什么才能正确地从每个选项卡的文件中获取布局?

library(shiny)
library(gridlayout)
library(bslib)
library(dplyr)

ui <- tabsetPanel(
  id = "tabset",
  tabPanel(title = "PAGE 1", source(file = "~/page_1.R", local = TRUE)$value),
  tabPanel(title = "PAGE 2", source(file = "~/page_2.R", local = TRUE)$value),
)

server <- function(input, output) {

  # DATA:
  x1 <- reactive({runif(input$obs1)})
  y1 <- reactive({runif(input$obs1)})
  
  x2 <- reactive({runif(input$obs2)})
  y2 <- reactive({runif(input$obs2)})
  # PAGE 1
  output$test <- renderPlot({
    plot1 <- plot(x1(), y1())
    plot1
  })
  # PAGE 2
  output$test2 <- renderPlot({
    plot2 <- plot(x2(), y2())
    plot2
  })
  
  output$tbl1 <- renderTable({
    tbl <- tibble(x = x2(),
                  y = y2())
    tbl
  })
}

shinyApp(ui, server)

第1页文件

grid_page(
  layout = c(
    "tabpanel tabpanel",
    "sidebar  plot1"),
  row_sizes = c(
    "10px",
    "1fr"
  ),
  col_sizes = c(
    "150px",
    "1fr"
  ),
  gap_size = "1rem",
  grid_card(
    area = "sidebar",
    card_header("Settings"),
    card_body(
      sliderInput("obs1", "First Page OBS:", min = 0, max = 1000, value = 500)
    )
  ),
  grid_card(
    area = "plot1",
    card_body(plotOutput(outputId = "test"))
  )
)

第2页文件

grid_page(
  layout = c(
    "tabpanel tabpanel tabpanel",
    "sidebar  tbl1  plot2"),
  row_sizes = c(
    "10px",
    "1fr"
  ),
  col_sizes = c(
    "150px",
    "1fr",
    "1fr"
  ),
  gap_size = "1rem",
  grid_card(
    area = "sidebar",
    card_header("Settings"),
    card_body(
      sliderInput("obs2", "Second Page OBS:", min = 0, max = 1000, value = 500)
    )
  ),
  grid_card(
    area = "plot2",
    card_body(plotOutput(outputId = "test2"))
  ),
  grid_card(
    area = "tbl1",
    card_body(tableOutput(outputId = "tbl1"))
  )
)
gstyhher

gstyhher1#

我同意模块是一条路要走,但如果有人想使这一工作,它是可能的删除grid_page从这些文件,并使用fluidPagegrid_container。然后你就不再需要'tabset'行了,所以例子是这样的。在两个文件上执行此操作,就像我期望的那样。

fluidPage(
  grid_container(
    layout = c(
      "sidebar  plot1"),
    row_sizes = c(
      "1fr"
    ),
    col_sizes = c(
      "150px",
      "1fr"
    ),
    gap_size = "1rem",
    grid_card(
      area = "sidebar",
      card_header("Settings"),
      card_body(
        sliderInput("obs1", "First Page OBS:", min = 0, max = 1000, value = 500)
      )
    ),
    grid_card(
      area = "plot1",
      card_body(plotOutput(outputId = "test"))
    )
  )
)

相关问题