【问题标题】:R fails render on Shiny DashBoard after editing tabitems编辑 tabitems 后,R 在 Shiny DashBoard 上渲染失败
【发布时间】:2020-04-04 18:08:36
【问题描述】:
library(shiny)
library(tidyverse)
library(leaflet)
library(shinydashboard)
library(RColorBrewer)
library(viridis)
library(shinyWidgets)


ui <- dashboardPage(
    dashboardHeader(title = "Basic dashboard"),
    dashboardSidebar(
        sidebarMenu(
            menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")

                ),
            menuItem("Widgets", tabName = "widgets", icon = icon("th"))
        )
    ),
    dashboardBody(
        # Boxes need to be put in a row (or column)
        tabItems(
            # First tab content
            tabItem(tabName = "dashboard",
                    fluidRow(
                        box(plotOutput("plot1", height = 250)),
                        box(
                            title = "Controls",
                            sliderInput("slider", "Number of observations:", 1, 100, 50)
                        )
                    )
            ),
            # Second tab content
            tabItem(tabName = "widgets",
                    h2("Widgets tab content")
            )
        )
    )
)
server <- function(input, output) {
    set.seed(122)
    histdata <- rnorm(500)
    output$plot1 <- renderPlot({
        data <- histdata[seq_len(input$slider)]
        hist(data)
    })
}
shinyApp(ui, server)

上面的例子,当单独点击选项卡仪表板和小部件时,输出呈现。在仪表板选项卡中,我在正文中有滑块输入。 现在,当我将滑块更改为将其放在侧边栏时:

library(shiny)
library(tidyverse)
library(leaflet)
library(shinydashboard)
library(RColorBrewer)
library(viridis)
library(shinyWidgets)

ui = dashboardPage(
    dashboardHeader(title =  "Dashboard"),
    dashboardSidebar(
        sidebarMenu(
            id = "sidebarmenu",
            menuItem("menuItem1", 
                     tabName = "tab1",
                     sliderInput("slider", "Number of observations:", 1, 100, 50)
            )
            ,
            menuItem("menuItem2", tabName = "tab2")
        )
    ),
    dashboardBody(
        tabItems(
            tabItem(tabName = "tab1",
                    tableOutput("myTable"),
            ),
            tabItem(tabName = "tab2",
                    h2("Placeholder Cloud"),
                    plotOutput("myPlot"))
        )
    )
)

server <- function(input, output, session) {

    tableData = reactiveVal(data.frame(x = 1:10, y = LETTERS[1:10]))
    plotData = reactiveVal()

    observeEvent(input$sidebarmenu, {

        if(input$sidebarmenu == "tab2"){
            #Code for tab 2
            req(is.null(plotData()))
            print("Tab 2 code is run")
            plotData(runif(100))

        }

    })

    output$myTable = renderTable({
        tableData()
    })

    set.seed(122)
    histdata <- rnorm(500)
    output$myPlot = renderPlot({
        data <- histdata[seq_len(input$slider)]
        hist(data)
    })
}

shinyApp(ui, server)

这里的问题: 1)文本占位符云,最初仅在单击选项卡 menuItem2 时呈现,现在与直方图一起呈现,该直方图应该仅在我单击 menuItem1 时呈现

2) 单击 menuItem2 不会执行任何操作。单击时我希望 tab2 显示一个散点图,如下面的代码块所示。

即,我想“将下面的第三个代码块”集成到上面的第二个代码块中,所以当我运行第二个代码块时,当我单击 tab1 时,会出现滑块的输入并呈现直方图。当我点击tab2时,散点图将被渲染

library(shiny)
library(tidyverse)
library(leaflet)
library(shinydashboard)
library(RColorBrewer)
library(viridis)
library(shinyWidgets)

ui = dashboardPage(
    dashboardHeader(title =  "Dashboard"),
    dashboardSidebar(
        sidebarMenu(
            id = "sidebarmenu",
            menuItem("menuItem1", 
                     tabName = "tab1")
                ,
            menuItem("menuItem2", tabName = "tab2")
        )
    ),
    dashboardBody(
        tabItems(
            tabItem(tabName = "tab1",
                    tableOutput("myTable")),
            tabItem(tabName = "tab2",
                    h2("Placeholder Cloud"),
                    plotOutput("myPlot"))
        )
    )
)

server <- function(input, output, session) {

    tableData = reactiveVal(data.frame(x = 1:10, y = LETTERS[1:10]))
    plotData = reactiveVal()

    observeEvent(input$sidebarmenu, {

        if(input$sidebarmenu == "tab2"){
            #Code for tab 2
            req(is.null(plotData()))
            print("Tab 2 code is run")
            plotData(runif(100))

        }

    })

    output$myTable = renderTable({
        tableData()
    })

    output$myPlot = renderPlot({
        plot(plotData())
    })
}

shinyApp(ui, server)

感谢任何帮助。我是 Shiny 的新手。

【问题讨论】:

  • 您好,可以提供minimal, reproducible example吗?这意味着您不应使用只有您拥有的数据,而应使用 R 中包含的数据集,例如 mtcarsiris,并且您必须找到导致问题的应用程序的哪个部分。例如,如果问题仅来自 ui 中的某个项目,则无需在您的帖子中放置一个长应用程序
  • 嗨@bretauv,感谢您的评论。我已将示例编辑为不包含数据集。 (我可能偶然标记了您的评论,因为我认为它会触发回复按钮。抱歉。)

标签: r shiny


【解决方案1】:

对于您的第二部分,您说“单击 menuItem2 不会做任何事情。”...但是您没有对 menuItem2 执行任何操作,请参阅

menuItem("menuItem2", tabName = "tab2")

【讨论】:

  • 嗨,我再次编辑了帖子。我想将第 3 块合并到第 2 块代码中,以便选项卡工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-22
  • 2019-05-15
  • 2020-04-26
  • 2019-03-07
相关资源
最近更新 更多