[英]Shiny dashboard and DT table not showing
我有一个仪表板,我想在其中显示表格,但是我无法弄清楚为什么我的表格没有显示。 例如,如果我用一些文本替换表格,则会显示h2(....)
。 我想单击“种类”,并在单击时在右侧显示表格。
library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(DT)
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(sidebarMenu(
menuItem(
"Species",
tabName = "Species",
icon = NULL,
switchInput(
inputId = "long1",
onLabel = "Go",
offLabel = "NoGo",
value = T
),
actionButton("Gobtn", "Get data"),
menuItem("Specs", tabName = "Specs", icon = NULL)
)
)),
dashboardBody(tabItems(
tabItem(tabName = "Species",
DT::renderDataTable("Table1")),
tabItem(tabName = "Specs",
h2("Hi"))
))
)
服务器
server <- shinyServer(function(input, output, session) {
output$Table1 <- DT::renderDataTable({
iris
})
})
shinyApp(ui, server)
很少有什么可以让您的代码在这里启动和运行。 夫妇已经被其他贡献者注意到。
我们需要在UI端使用DT::dataTableOutput("Table1")
,因为renderDataTable
在这里不起作用,即服务器端函数。
另一个可能是,在menuItem
中使用switchInput
可能会使应用程序感到困惑,因为这些不是传递给函数的标准参数。 从我的代码中可以看到,这是一个常见的挑战,那就是您希望仅在选择“ Species”选项卡时才能显示此switchInput
。 我们可以使用conditionalPanel
解决这个conditionalPanel
。 为此,我们可以在sidebarMenu
设置id = "tabs"
,然后在conditionalPanel
引用此sidebarMenu
:
conditionalPanel(
condition = "input.tabs== 'Species' ",
switchInput(
inputId = "long1",
onLabel = "Go",
offLabel = "NoGo",
value = T
)
)
最后,我已经更改了ui.R和server.R的布局,因为应用程序不需要与服务器和ui文件一起使用shinyApp
函数。 这就是我布置仪表板的方式。 它可能向您展示了在Shiny中使用应用程序结构的其他几种可能方式,但是同样,您也可以将更改与基本布局对齐。
用户界面
header <- dashboardHeader(title = "Basic dashboard")
sidebar <- dashboardSidebar(
sidebarMenu(id = "tabs",
menuItem(
"Species",
tabName = "Species",
icon = NULL),
conditionalPanel(
condition = "input.tabs== 'Species' ",
switchInput(
inputId = "long1",
onLabel = "Go",
offLabel = "NoGo",
value = T
)
),
actionButton("Gobtn", "Get data"),
menuItem("Specs", tabName = "Spec", icon = NULL)
)
)
body <- dashboardBody(
tabItems(
tabItem(tabName = "Species",
DT::dataTableOutput("Table1")),
tabItem(tabName = "Spec",
h2("Hi"))
)
)
dashboardPage(skin = "blue", header = header, sidebar = sidebar, body = body)
服务器
library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(DT)
shinyServer(function(input, output, session){
output$Table1 <- DT::renderDataTable({
datatable(iris)
})
})
您需要更改/添加dashboardBody
某些部分,请参阅使用发光模块和shinydashboard:shiny.tag错误
library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(DT)
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(sidebarMenu(
menuItem(
"Species",
tabName = "Species",
icon = NULL,
switchInput(
inputId = "long1",
onLabel = "Go",
offLabel = "NoGo",
value = T
),
actionButton("Gobtn", "Get data")
)
)),
dashboardBody(tags$div(
tabName = "Species",
fluidRow(box(DT::dataTableOutput("Table1"))), class = "tab-content"
))
)
服务器
server <- shinyServer(function(input, output, session) {
output$Table1 <- DT::renderDataTable({
iris
})
})
shinyApp(ui, server)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.