shiny - 带有未显示结果的小部件的闪亮仪表板菜单项
问题描述
我正在使用shinydashboard 构建一个应用程序,该应用程序包含一个带有menuItem / tabItems 的侧栏菜单,但我无法获得第二个选项卡(包括小部件)来显示结果。正文仅显示第一个选项卡。这一定是非常简单的事情,但我看不出我做错了什么......
这是一个可重现的例子:
## app.R ##
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
sidebarMenu(
menuItem("SomeText",
tabName = "sometext"
),
menuItem("Histogram",
tabName = "histogram",
# input1: number of observations:
sliderInput(
inputId = "n",
label = "Number of observations",
min = 10, max = 100, value = 30
)
) # end menuItem
) # end sidebarMenu
), # end dashboardSidebar
dashboardBody(
tabItems(
tabItem(tabName = "sometext",
h2("blah blah blah")
),
tabItem(tabName = "histogram",
plotOutput("my_histogram")
)
)
)
)
server <- function(input, output) {
output$my_histogram <- renderPlot({
hist(input$n, col = "red" )
})
}
shinyApp(ui, server)
为什么我在第二个选项卡项上看不到直方图?
解决方案
shinydashboard's sidebar distinguishes "childless" and "childfull" menuItem
s. By placing the sliderInput
inside the menuItem
"histogram" it becomes "childfull", which means one or more sub categories are available for navigation, accordingly there is no need to display content in the body, as the user is expected to navigate to one of the childs. Please read this for further information.
Therefore, if you want to display content in the body for the "histogram"-tab it needs to be "childless".
Here is a solution placing the slider outside of the "histogram"-tab, but only displaying it when "histogram" is selected:
## app.R ##
library(shiny)
library(shinydashboard)
ui <- dashboardPage(dashboardHeader(),
dashboardSidebar(
sidebarMenu(
id = "mySidebar",
menuItem("SomeText", tabName = "sometext"),
menuItem("Histogram", tabName = "histogram"),# end menuItem
conditionalPanel(condition = "input.mySidebar == 'histogram'", {
# input1: number of observations:
sliderInput(
inputId = "n",
label = "Number of observations",
min = 10,
max = 100,
value = 30
)
})
) # end sidebarMenu
), # end dashboardSidebar
dashboardBody(tabItems(
tabItem(tabName = "sometext",
h2("blah blah blah")),
tabItem(tabName = "histogram",
plotOutput("my_histogram"))
)))
server <- function(input, output) {
output$my_histogram <- renderPlot({
hist(round(runif(input$n, 1, 10), digits = 0), col = "red")
})
}
shinyApp(ui, server)
推荐阅读
- sql - SQL规范化数据表并创建多个表
- xpages - XPages - 提交时依赖组合框为空
- python - ~20 x 20 x 20 立方体的绘图密度
- android - 是否可以在启动广播的活动中接收广播结果?
- r - 为许多不同的图创建leaflet.minicharts 弹出标签
- marklogic - MarkLogic - 使用 mlcp 导入时出现 java 堆空间错误
- javascript - “可观察”类型上不存在属性“mergeMap”
' - reactjs - 以编程方式反应复选框的更新状态,而无需在创建组件时单击它
- wildfly - Wildfly 15 Wildfly Maven 插件
- arrays - bash 数组在索引 0 处产生 array_name[@]