【问题标题】:Dynamic Graphs not getting displayed properly in Shiny R?动态图在 Shiny R 中没有正确显示?
【发布时间】:2020-06-22 02:10:00
【问题描述】:

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

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)

在我的例子中,CheckboxGroupInput() 中的选择是动态的。根据“年”单选按钮选择,将生成复选框。当复选框被选中时,相应的图表将在主面板中生成。

图表正在生成,但是当我选择下一个“数字”单选按钮并选择“数字”单选按钮下的复选框时,正在为该复选框选择生成图表但它在之前生成的图表下方在之前的“年”单选按钮复选框选择中。我不确定为什么会出现这个特定问题。

我只想要那些为特定单选按钮选择及其对应的复选框选择生成的图表。根据当前代码,即使用户切换单选按钮,复选框选择也会保留。请不要对这个特定功能进行任何更改。

请告诉我你的建议。

【问题讨论】:

    标签: r checkbox graph shiny


    【解决方案1】:

    那是因为您没有检查该部分 - 未选择单选按钮时该怎么办。只需将程序中的output$graphoutput$graph_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)
      })
    

    应该可以的。

    【讨论】:

      猜你喜欢
      • 2022-12-09
      • 1970-01-01
      • 2017-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-31
      • 1970-01-01
      相关资源
      最近更新 更多