为什么在包含Rmarkdown文件时改变了Shiny应用布局?

ldfqzlk8  于 2023-03-27  发布在  其他
关注(0)|答案(1)|浏览(132)

我正在玩一个有多个标签使用tabsetPaneltabPanel闪亮的应用程序.一切都很好,直到包括一个Rmarkdown文件到其中一个标签.整个应用程序的大小减少后,.rmd文件呈现为HTML.此外,选项number_sections: true没有做任何事情.有人知道如何解决这个问题吗?谢谢!

问题视频

App.R

if (!require("pacman")) install.packages("pacman")
pacman::p_load(skimr)
pacman::p_load(markdown)
pacman::p_load(knitr)
pacman::p_load(shiny)

# 1) Define UI for random distribution app ----
ui <- fluidPage(
  
  # App title ----
  titlePanel("Tabsets"),
  
  # Sidebar layout with input and output definitions ----
  sidebarLayout(
    
    # Sidebar panel for inputs ----
    sidebarPanel(
      
      # Input: Select the random distribution type ----
      radioButtons("dist", "Distribution type:",
                   c("Normal" = "norm",
                     "Uniform" = "unif",
                     "Log-normal" = "lnorm",
                     "Exponential" = "exp")),
      
      br(),
      
      # * Input: Slider for the number of observations to generate ----
      sliderInput("n",
                  "Number of observations:",
                  value = 500,
                  min = 1,
                  max = 1000)
      
    ),
    
    # * Main panel for displaying outputs ----
    mainPanel(
      
      # * Output: Tabset w/ plot, summary, and table, etc. ----
      tabsetPanel(type = "tabs",
                  tabPanel("Plot", plotOutput("plot")),
                  tabPanel("Summary", verbatimTextOutput("summary")),
                  tabPanel("RMarkdown", uiOutput('rmd_summary'))
      )
      
    )
  )
)

# 2) Define server logic for random distribution app ----
server <- function(input, output) {
  
  # Reactive expression to generate the requested distribution ----
  d <- reactive({
    dist <- switch(input$dist,
                   norm = rnorm,
                   unif = runif,
                   lnorm = rlnorm,
                   exp = rexp,
                   rnorm)
    
    dist(input$n)
  })
  
  # Generate a plot of the data ----
  output$plot <- renderPlot({
    dist <- input$dist
    n <- input$n
    
    hist(d(),
         main = paste("r", dist, "(", n, ")", sep = ""),
         col = "#007bc2", border = "white")
  })
  
  # Generate a summary of the data ----
  output$summary <- renderPrint({
    summary(d())
  })
  
  # Data Summary ----
  output$rmd_summary <- renderUI({
    HTML(mark_html(knit('data_summary.Rmd', quiet = TRUE)))
  })
  
}

# Create Shiny app ----
shinyApp(ui, server)

数据_摘要.Rmd

---
title: "Overview of the Data"
output: 
  html_document:
    number_sections: true
---

<br> 

# Data Statistics

## Skimr

```{r, echo=FALSE, warning=FALSE}
skimr::skim(iris)
dxxyhpgq

dxxyhpgq1#

不知 prop 体是怎么回事,但看起来mark_html()函数默认导入一个模板,该模板具有与之关联的CSS文件,这些文件覆盖了原始的闪亮CSS。关闭该功能的最简单方法是将模板参数设置为template=FALSE
这意味着没有(或至少是最小的)导入CSS用于markdown渲染,所以你可能需要对shiny渲染的markdown部分做一些CSS调整。我没有改变markdown文件。下面是修改后的UI和服务器的样子:

if (!require("pacman")) install.packages("pacman")
pacman::p_load(skimr)
pacman::p_load(markdown)
pacman::p_load(knitr)
pacman::p_load(shiny)

# 1) Define UI for random distribution app ----
ui <- fluidPage(
  
# I added CSS to add cell space the table in the markdown file and add white space at 
# the bottom of the page. More tweaks might be needed. 

  tags$head(
    tags$style(
      HTML("td, th {padding: 10px;} #rmd_summary {margin-bottom: 60px;}"))),
  
# end of UI changes, server changes below
  
  # App title ----
  titlePanel("Tabsets"),
  
  # Sidebar layout with input and output definitions ----
  sidebarLayout(
    
    # Sidebar panel for inputs ----
    sidebarPanel(
      
      # Input: Select the random distribution type ----
      radioButtons("dist", "Distribution type:",
                   c("Normal" = "norm",
                     "Uniform" = "unif",
                     "Log-normal" = "lnorm",
                     "Exponential" = "exp")),
      
      br(),
      
      # * Input: Slider for the number of observations to generate ----
      sliderInput("n",
                  "Number of observations:",
                  value = 500,
                  min = 1,
                  max = 1000)
      
    ),
    
    # * Main panel for displaying outputs ----
    mainPanel(
      
      # * Output: Tabset w/ plot, summary, and table, etc. ----
      tabsetPanel(type = "tabs",
                  tabPanel("Plot", plotOutput("plot")),
                  tabPanel("Summary", verbatimTextOutput("summary")),
                  tabPanel("RMarkdown", uiOutput('rmd_summary'))
      )
      
    )
  )
)

# 2) Define server logic for random distribution app ----
server <- function(input, output) {
  
  # Reactive expression to generate the requested distribution ----
  d <- reactive({
    dist <- switch(input$dist,
                   norm = rnorm,
                   unif = runif,
                   lnorm = rlnorm,
                   exp = rexp,
                   rnorm)
    
    dist(input$n)
  })
  
  # Generate a plot of the data ----
  output$plot <- renderPlot({
    dist <- input$dist
    n <- input$n
    
    hist(d(),
         main = paste("r", dist, "(", n, ")", sep = ""),
         col = "#007bc2", border = "white")
  })
  
  # Generate a summary of the data ----
  output$summary <- renderPrint({
    summary(d())
  })
  
# I added the template = FALSE argument

  # Data Summary ----
  output$rmd_summary <- renderUI({
    HTML(mark_html(knit('data_summary.Rmd', quiet = TRUE) , template = FALSE))
  })
  
}

# Create Shiny app ----
shinyApp(ui, server)

它看起来是这样的:

相关问题