闪亮的仪表板和 DT table 未显示
Shiny dashboard and DT table not showing
我有一个仪表板,我想在其中显示 table,但我不明白为什么我的 table 没有显示。例如,如果我用一些文本替换 table,h2(....)
它会显示。我想点击 "Species" 并在点击它时在右侧显示 table 。
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.r
server <- shinyServer(function(input, output, session) {
output$Table1 <- DT::renderDataTable({
iris
})
})
shinyApp(ui, server)
您需要 change/add dashboardBody
的某些部分,请参阅 Using shiny modules and shinydashboard: shiny.tag error
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.r
server <- shinyServer(function(input, output, session) {
output$Table1 <- DT::renderDataTable({
iris
})
})
shinyApp(ui, server)
一些事情可以让你的代码和 运行 在这里。 Couple 已被其他贡献者注意到。
我们需要在 UI 端使用 DT::dataTableOutput("Table1")
,因为 renderDataTable
在这里不起作用,那是服务器端功能。
另一个是在 menuItem
中使用 switchInput
可能会使应用程序感到困惑,因为这些不是传递到函数中的标准参数。从您的代码中我可以看出,这是一个常见的挑战,即您希望仅在选择 'Species' 选项卡时才能显示此 switchInput
。我们可以使用 conditionalPanel
来解释这一点。为此,我们可以在 sidebarMenu
中设置 id = "tabs"
,然后在 conditionalPanel
:
中引用此 sidebarMenu
conditionalPanel(
condition = "input.tabs== 'Species' ",
switchInput(
inputId = "long1",
onLabel = "Go",
offLabel = "NoGo",
value = T
)
)
最后,我更改了 ui.R 和 server.R 的布局,因为应用程序不需要 shinyApp
功能来与服务器一起工作 ui 文件。这就是我布置仪表板的方式。它可能会向您展示一些其他可能的方式,您可以在 Shiny 中使用应用程序结构,但同样地,您可以将更改与基本布局对齐。
ui.R
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)
server.R
library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(DT)
shinyServer(function(input, output, session){
output$Table1 <- DT::renderDataTable({
datatable(iris)
})
})
我有一个仪表板,我想在其中显示 table,但我不明白为什么我的 table 没有显示。例如,如果我用一些文本替换 table,h2(....)
它会显示。我想点击 "Species" 并在点击它时在右侧显示 table 。
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.r
server <- shinyServer(function(input, output, session) {
output$Table1 <- DT::renderDataTable({
iris
})
})
shinyApp(ui, server)
您需要 change/add dashboardBody
的某些部分,请参阅 Using shiny modules and shinydashboard: shiny.tag error
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.r
server <- shinyServer(function(input, output, session) {
output$Table1 <- DT::renderDataTable({
iris
})
})
shinyApp(ui, server)
一些事情可以让你的代码和 运行 在这里。 Couple 已被其他贡献者注意到。
我们需要在 UI 端使用 DT::dataTableOutput("Table1")
,因为 renderDataTable
在这里不起作用,那是服务器端功能。
另一个是在 menuItem
中使用 switchInput
可能会使应用程序感到困惑,因为这些不是传递到函数中的标准参数。从您的代码中我可以看出,这是一个常见的挑战,即您希望仅在选择 'Species' 选项卡时才能显示此 switchInput
。我们可以使用 conditionalPanel
来解释这一点。为此,我们可以在 sidebarMenu
中设置 id = "tabs"
,然后在 conditionalPanel
:
sidebarMenu
conditionalPanel(
condition = "input.tabs== 'Species' ",
switchInput(
inputId = "long1",
onLabel = "Go",
offLabel = "NoGo",
value = T
)
)
最后,我更改了 ui.R 和 server.R 的布局,因为应用程序不需要 shinyApp
功能来与服务器一起工作 ui 文件。这就是我布置仪表板的方式。它可能会向您展示一些其他可能的方式,您可以在 Shiny 中使用应用程序结构,但同样地,您可以将更改与基本布局对齐。
ui.R
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)
server.R
library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(DT)
shinyServer(function(input, output, session){
output$Table1 <- DT::renderDataTable({
datatable(iris)
})
})