简体   繁体   中英

Shiny renderplot returns blank

I am having problems with generating an output in shiny where I want the client to manually change the colour upon their request and the data is reactive. I simply get a blank page and nothing shown into it apart from the selectInput filter, and the dropdown item, but not the plot.

My attempt consisted of a combination of lapply with colourpicker function and created a dropdown item where the user can pick the colour he likes. Unfortunately, I could not provide a reproducible example or the dataset because there is data that could not be shared and I apologise in advance. I hope that someone might recognize the problem by looking at the code in case it is very obvious. In summary the data that has been used for this example consists of three columns: title, partner, quantity(ex a title of a book, the partner that prints it and the number of copies printed).

I believe that it has to do sth with the 3 lines of code for the 'cols' before I generate the plot inside the renderPlot. I would greatly appreciate if sb could lead me in the right direction. Thanks a lot in advance.

### 1) Loading the libraries ----

library(shinydashboard)
library(shiny)
library(shinyWidgets)
library(plotly)
library(colourpicker)
library(tidyverse)


###  UI ----


header <- 
  dashboardHeader( title = HTML("Dashboard"), 
                   disable = FALSE, 
                   titleWidth  = 230)


sidebar <- dashboardSidebar(
  sidebarMenu(id="sbmenu",
              menuItem("General Dashboard", tabName = "dashboard", icon = icon("dashboard"))))


body <- dashboardBody( 
  
  tabItems(
    tabItem(tabName = "dashboard",
            fluidRow(
              
              selectizeInput("choice","Please select", 
                          choices=levels(Complete$title),multiple=T)),
            

            fluidPage(
              uiOutput("myPanel"),
              plotOutput("plot")))))





## UI wrapper -----


ui <- dashboardPage(header, sidebar, body)


## Server ----

server <- function(input, output) {
  

data_complete<-reactive(Complete %>% 
                filter(title%in% input$choice,quantity>0))
  


### Plot 

output$myPanel <- renderUI({ 
    
  lev <- sort(unique(input$choice)) 
  
  cols <- length(lev)


  dropdownButton(lapply(seq_along(lev), function(i) {
      colourpicker::colourInput(inputId = paste0("col", lev[i]),
                  label = paste0("Choose colour", lev[i]), 
                  value = cols[i])}),
      circle = TRUE, size="xs",status = "danger", icon = icon("gear"), width = "50px",
      tooltip = tooltipOptions(title = "Change your colour"))
  })
  
  
output$plot <- renderPlot({

  cols <- paste0("c(", paste0("input$col", sort(input$choice), collapse = ", "), ")")
  
  cols <- eval(parse(text = cols))
  
  req(length(cols) == length(input$choice)) 
 

data_complete() %>% 
    group_by(title,partner) %>% 
    summarise(total=sum(quantity)) %>% 
    ggplot(aes(x=partner,y=total,fill=title))+
    geom_col(stat="identity",position = position_dodge())+
    scale_fill_manual(values = cols)
  })  



}


shinyApp(ui, server)




Your app works fine. I added the following lines at the very top of your code to simulate your input data, and it works great.

Complete <- data.frame(
    title    = c("A", "B", "C", "D", "E", "F"), 
    partner  = c("1", "1", "2", "2", "3", "3"), 
    quantity = c(100, 200, 300, 400, 500, 600)
)

在此处输入图像描述

It does forget the color selections if you change the title selections, and there are some warnings but nothing critical.

This means that its something else that it causing your issues. Check that your packages are up to date, that your data is available to your plot, and that your data is what you think it is. A good way to debug this sort of thing is to get it working outside of shiny first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM