【发布时间】:2020-11-20 20:55:30
【问题描述】:
我不确定为什么没有显示图表。 All I want is the user must select a menuSubItem from the sidebarMenu and select a choice in the radioButton, when these are selected, respective tabs in the dashboardBody must be displayed.当我尝试执行此代码时,没有任何效果,也没有引发错误。请帮忙。还建议是否可以进行任何替代编码来完成这项工作。 谢谢,
ui <- dashboardPage(#skin="purple-light",
dashboardHeader(title="Testing"),
dashboardSidebar(width=200,
sidebarMenu(id= "sbar",verbatimTextOutput("text1"),verbatimTextOutput("text2"),
menuItem("Menu1", tabName = "MenuTab1",startExpanded = TRUE,
menuSubItem("Sub Menu1", tabName = "sub1"),
menuSubItem("Sub Menu2", tabName = "sub2")),
menuItem("Menu2", tabName= "MenuTab2",startExpanded = TRUE,
menuSubItem("Sub Menu3", tabName = "sub3"),
menuSubItem("Sub Menu4", tabName = "sub4")))),
dashboardBody(radioButtons("rb1",label=NULL, choices = c("choice1","choice2"), selected=NULL),
tabItems(
tabItem("sub1",title= "Tab1",fluidRow(plotOutput("plot1"),plotOutput("plot3"))),
tabItem("sub2",title= "Tab2",fluidRow(plotOutput("plot1"),plotOutput("plot2"))),
tabItem("sub3",title= "Tab3",fluidRow(plotOutput("plot1"),plotOutput("plot3"))))))
server <- function(input,output){
set.seed(1234)
observe(input$sbar)
p1 <- reactive({
req(input$rb1)
req(input$sbar)
if ( input$sbar == "sub1" && input$rb1 =="choice1" ){
return(qplot(rnorm(500),fill=I("blue"),binwidth=0.2))
}else if (input$sbar == "sub1" && input$rb1 =="choice2") {
return(qplot(rnorm(500),fill=I("red"),binwidth=0.2))
}else if (input$sbar == "sub2" && input$rb1 =="choice1") {
return(qplot(rnorm(500),fill=I("black"),binwidth=0.2))
}else if (input$sbar == "sub2" && input$rb1 =="choice2") {
return(qplot(rnorm(500),fill=I("red"),binwidth=0.2))
}else if (input$sbar == "sub3" && input$rb1 =="choice1") {
return(qplot(rnorm(500),fill=I("black"),binwidth=0.2))
}else if (input$sbar == "sub3" && input$rb1 =="choice2") {
return(qplot(rnorm(500),fill=I("blue"),binwidth=0.2))
}else if (input$sbar == "sub4" && input$rb1 =="choice1") {
return(qplot(rnorm(500),fill=I("yellow"),binwidth=0.2))
}else if (input$sbar == "sub4" && input$rb1 =="choice2") {
return(qplot(rnorm(500),fill=I("orange"),binwidth=0.2))
}else {return(qplot(rnorm(500),fill=I("green"),binwidth=0.2))}
})
p2 <- qplot(rnorm(500),fill=I("red"),binwidth=0.2)
p3 <- qplot(rnorm(500),fill=I("yellow"),binwidth=0.2)
observe(input$sbar)
output$text1 <- renderText(print(input$rb1))
output$text2 <- renderText(print(input$sbar))
output$plot1 <- renderPlot({p1})
output$plot2 <- renderPlot({p2()})
output$plot3 <- renderPlot({p3})
shinyApp(用户界面,服务器)
【问题讨论】:
标签: shiny shinydashboard shiny-server shinyapps shiny-reactivity