gpt4 book ai didi

R Shiny - 在侧边栏面板外显示静态文本

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

我想在 Shiny 的侧边栏面板之外显示静态文本。我能够在侧边栏面板内显示文本。但是,如果我尝试在侧边栏面板之外显示文本,则会收到此错误:“match.arg 中的错误:'arg' 必须为 NULL 或字符向量”。

下面是一个示例代码,它在侧边栏面板内显示句子“这是一个静态文本”。我想在侧边栏面板的“正下方”显示文本,但不在面板窗口内。

下面的代码给了我这个输出:

this output但我希望它看起来像这样:

like this我如何实现这一目标?

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

# Application title
titlePanel("Old Faithful Geyser Data"),

# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30),
h5("This is a static text")
),

# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)

# Define server logic required to draw a histogram
server <- function(input, output) {

output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)

# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})

}

# Run the application
shinyApp(ui = ui, server = server)

最佳答案

sidebarPanel函数会将所有内容放入 form与类(class) well .一个hacky解决方案(也许有更好的解决方案)是创建一个自定义函数siderbarPanel将元素放在 form 之外的函数.以下是带有函数 sidebarPanel2 的代码这只是对原始函数的自定义,以将元素放在“正下方”。您可以放置​​任何内容,而不仅仅是文本。

library(shiny)

sidebarPanel2 <- function (..., out = NULL, width = 4)
{
div(class = paste0("col-sm-", width),
tags$form(class = "well", ...),
out
)
}

# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel2(fluid = FALSE,
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30),
out = h5("This is a static text")
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)

# Define server logic required to draw a histogram
server <- function(input, output) {
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
}

# Run the application
shinyApp(ui = ui, server = server)

关于R Shiny - 在侧边栏面板外显示静态文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52544228/

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