gpt4 book ai didi

r - Shiny 中 fillPage 的基本示例 - 它是如何工作的?

转载 作者:行者123 更新时间:2023-12-05 03:05:20 25 4
gpt4 key购买 nike

我不明白我怎样才能得到一个图来完全填满我的仪表板(除了标题)。我想我必须使用 fillPage,但我无法让它工作。这是我的例子。感谢您提供任何提示!

ipak <- function(pkg){
new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if (length(new.pkg))
install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}

packages <- c("tidyverse","shiny","shinydashboard","dashboardthemes","ggplot2")
ipak(packages)

#---------------------------------------------------------------------------------------------------------------------
ui <- dashboardPage(
dashboardHeader(title = "FullscreenDashboard", titleWidth = 450),
dashboardSidebar(disable = TRUE),
dashboardBody(
### change theme
shinyDashboardThemes(
theme = "grey_dark"
),
fillPage(
plotOutput("plot1", height = "100%", width = "100%")
)
)
)

server <- function(input, output, session){
output$plot1 <- renderPlot({ #reactivePlot
dat <- data.frame(
time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")),
total_bill = c(14.89, 17.23)
)
#Plot
p<-ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) +
geom_bar(stat="identity")

print(p)
})
}
shinyApp(ui, server)

最佳答案

这应该可以完成工作:

library(shiny)
library(shinydashboard)
library(dashboardthemes)
library(ggplot2)
ui <- dashboardPage(
dashboardHeader(title = "FullscreenDashboard", titleWidth = 450),
dashboardSidebar(disable = T),
dashboardBody(
### change theme
shinyDashboardThemes(
theme = "grey_dark"
),
fillPage(
tags$style(type = "text/css", "#plot1 {height: calc(100vh - 80px) !important;}"),
plotOutput("plot1", width = "100%",height = "100%")
)
)
)

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

output$plot1 <- renderPlot({ #reactivePlot
dat <- data.frame(
time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")),
total_bill = c(14.89, 17.23)
)
#Plot
p<-ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) +
geom_bar(stat="identity")
p
})
}
shinyApp(ui, server)

enter image description here

编辑: 如何使用 tabPanel 填充 navbarPage

library(shiny)
library(ggplot2)

ui <- navbarPage("Navbar!",
tabPanel("Plot",
fillPage(
tags$style(type = "text/css", "#plot1 {height: calc(100vh - 80px) !important;}"),
plotOutput("plot1", width = "100%",height = "100%")
)
),
tabPanel("Summary",
fillPage(
tags$style(type = "text/css", "#plot2 {height: calc(100vh - 80px) !important;}"),
plotOutput("plot2", width = "100%",height = "100%")
)
)
)

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

data <- reactive({
dat <- data.frame(
time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")),
total_bill = c(14.89, 17.23)
)
#Plot
p<-ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) +
geom_bar(stat="identity")
p
})

output$plot1 <- renderPlot({
data()
})
output$plot2 <- renderPlot({
data()
})
}
shinyApp(ui, server)

enter image description here

整个应用的背景图片:有些人可能希望您的应用有漂亮的背景,在这种情况下,我建议您看一下setBackgroundImage根据 hereshinyWidgets 包中运行

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
tags$h2("Add a shiny app background image"),
setBackgroundImage(
src = "https://www.fillmurray.com/1920/1080"
)
)

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

}

shinyApp(ui, server)

enter image description here

关于r - Shiny 中 fillPage 的基本示例 - 它是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51096375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com