r - R Shiny - 在侧边栏面板外显示静态文本
问题描述
我想在 Shiny 的侧边栏面板之外显示静态文本。我能够在侧边栏面板内显示文本。但是,如果我尝试在侧边栏面板之外显示文本,则会收到此错误:“match.arg 中的错误:'arg' 必须为 NULL 或字符向量”。
下面是一个示例代码,它在侧边栏面板内显示句子“这是一个静态文本”。我想在侧边栏面板“正下方”显示文本,但不在面板窗口内。
下面的代码给了我这个输出:
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
with classwell
中。一个 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)
推荐阅读
- python - OpenCV显示带有黑条的额外窗口?
- swagger - 在 Open API 3.0 中的响应正文中描述对象中的管道分隔编码
- python - 为什么不提供 Binding 6?Sqlite/Python/Tkinter
- node.js - 你能阻止 node.js 在本地安装包吗?(使用全局包)
- python - 如何使用 plotly.express 为映射创建 id
- react-native-gifted-chat - React Native Gifted Chat:隐藏其他用户的头像
- javascript - isomorphic-git clone() TypeError:无法读取未定义的属性“绑定”
- lua - Lua - 组合两个变量
- javascript - 过滤掉多维数据
- python - “NoneType”对象没有属性“文本”BeautifulSoup Python