动态图形在R中未正确显示?

动态图形在R中未正确显示?,r,checkbox,graph,shiny,R,Checkbox,Graph,Shiny,我正在尝试创建一个闪亮的应用程序。在这个特定的应用程序中,我有一组单选按钮,选择其中一个将在下面显示一组选项作为复选框,选择另一个单选按钮将选择另一组选项,单击复选框将生成图形。请在下面找到用户界面和服务器代码 library(shiny) library(shinydashboard) library(shinyWidgets) library(dplyr) d <- data.frame( year = c(1995, 1995, 1995, 1996, 1996, 199

我正在尝试创建一个闪亮的应用程序。在这个特定的应用程序中,我有一组单选按钮,选择其中一个将在下面显示一组选项作为复选框,选择另一个单选按钮将选择另一组选项,单击复选框将生成图形。请在下面找到用户界面和服务器代码

library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(dplyr)
d <-
  data.frame(
    year = c(1995, 1995, 1995, 1996, 1996, 1996, 1997, 1997, 1997),
    Product_Name = c(
      "Table",
      "Chair",
      "Bed",
      "Table",
      "Chair",
      "Bed",
      "Table",
      "Chair",
      "Bed"
    ),
    Product_desc = c("X", "X", "X", "Y", "Y", "Y", "Z", "Z", "Z"),
    Cost = c(1, 2, 3, 4, 2, 3, 4, 5, 6)
  )

ui <- shinyUI(fluidPage(
  useShinydashboard(),
  tabPanel(
    "Plot",
    sidebarLayout(
      sidebarPanel(
        radioButtons(
          "Choose",
          "Choose One",
          c("Year" = "p", "Numbers" = "l")
        ),
        uiOutput('checkbox'),
        #width = 2,
        position = "bottom"),
      mainPanel(uiOutput("graph"),
                uiOutput("graph_1"))
      
    )
  )
))

server <- function(input, output, session) {
  
  z_1 <- reactiveValues(years = NULL)
  z_2 <- reactiveValues(numbers = NULL)
  
  observeEvent(input$X, {
    z_1$years <- input$X
  })
  
  observeEvent(input$X_1, {
    z_2$numbers <- input$X_1
  })
  
  output$checkbox <- renderUI({
    if (input$Choose == "p") {
      checkboxGroupInput("X",
                         "year",
                         choices = (unique(d$year)),selected = z_1$years)
      
    } else{
      checkboxGroupInput("X_1",
                         "Numbers",
                         choices = c("1","2","3","4"), ,selected = z_2$numbers)
    }
    
  })
  
  output$graph <- renderUI({
    ntabs = length(input$X)
    myTabs = lapply(seq_len(ntabs), function(i) {

        fluidRow(plotOutput(paste0("plot", i)))
    })
  })
  
  
  output$graph_1 <- renderUI({
    ntabs = length(input$X_1)
    myTabs = lapply(seq_len(ntabs), function(i) {

      fluidRow(plotOutput(paste0("plot_1", i)))
    })
  })
  
  
  observe (lapply(length(input$X), function(i) {
    output[[paste0("plot", i)]] <- renderPlot({
      if (length(input$X) > 0) {
        d %>%
          ggplot(aes(Product_Name, Cost)) +
          geom_col(aes(fill = Product_desc),
                   position = position_dodge(preserve = "single")) +
          facet_wrap( ~ input$X[i],
                      scales = "free_x",
                      strip.position = "bottom") +
          theme(strip.placement = "outside") +
          theme_bw()
      }
    })
    
  }))
  
  
  observe (lapply(length(input$X_1), function(i) {
    output[[paste0("plot_1", i)]] <- renderPlot({
      if (length(input$X_1) > 0) {
        d %>%
          ggplot(aes(Product_Name, Cost)) +
          theme(strip.placement = "outside") +
          theme_bw()
      }
    })
    
  }))
  
}

shinyApp(ui, server)
库(闪亮)
图书馆(shinydashboard)
图书馆(shinyWidgets)
图书馆(dplyr)

d这是因为您没有检查该部分-当单选按钮未选中时该怎么办。只需将程序中的
output$graph
output$graph\u 1
替换为下面给出的代码即可

output$graph <- renderUI({
    ntabs = length(input$X)
    if (input$Choose == "p") {
      myTabs = lapply(seq_len(ntabs), function(i) {
        fluidRow(plotOutput(paste0("plot", i)))
      })
    }else return(NULL)
  })
  
  output$graph_1 <- renderUI({
    ntabs = length(input$X_1)
    if (input$Choose == "l") {
      myTabs = lapply(seq_len(ntabs), function(i) {
        fluidRow(plotOutput(paste0("plot_1", i)))
      })
    }else return(NULL)
  })
输出$graph