0
votes

I am trying to create a dynamic plot in R Shiny but the plot does not show in the Dashboard.

I have the following test data which I will upload in the shiny app:

Region.1=c( 375.00,375.00,370.00,350.00,350.00,305.00,300.00,250.00,245.00,240.00,235.00,225.00,215.00,200.00,100.00,100.00,100.00,100.00,100.00,100.00)
Region.2 =c(0.00,0.00,0.00,0.00,0.00,200.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,20.00,500.00,235.00,0.00)
Region.3 =c(100000.00,76000.00,60000.00,50000.00,50000.00,30000.00,30000.00,26000.00,19000.00,11000.00,10000.00,8000.00,7000.00,5000.00,4070.00,4000.00,3660.00,2585.00,2550.00,250.00)
Type=c("B","F","F","B","B","A","A","F","F","B","A","B","F","B","F","F","A","A","F","B")
data <- data.frame("Region.1"=Region.1, "Region.2"=Region.2, "Region.3"=Region.3, "Type"=Type)

My Script for Shiny looks like this (simplified code)

server.R

function(input, output,session) {

  myData <- reactive({
    req(input$file)
    inFile <- input$file
    if(is.null(inFile)){return(NULL)}
    read.csv(inFile$datapath, header = TRUE, sep = input$sep,
             quote = '""')

  })

  df<-reactive({

    df<-myData()
    dfType <- df[ , grepl( "Type" , names( df ) ) ]
    dfRegion<-df[ , grepl( "Region" , names( df ) ) ]

    df4<-cbind(dfType, dfRegion)
    colnames(df4)[which(names(df4) == "dfType")] <- "Type"
    colnames(df4)[which(names(df4) == "dfRegion")] <- "Region"
    df4[df4==""] <- NA
    df4 <- na.omit(df4)
    return(df4)
  })


  df.datasets <- reactive({
      df<-df()
      df<- melt(df,id.vars=c("Type"),
                measure.vars=c("Region.1", "Region.2", "Region.3"))
      test<-aggregate(df$value, by=list(Region=df$variable, Type=df$Type), FUN=sum)
      test<-test %>% group_by(Region) %>% transmute(Type, percent = (x/sum(x))*100)
      test$percent<-round(test$percent, digits = 0)
      test[test==0] <- NA
      test<-test[complete.cases(test),]

  })


  output$plot1<-renderPlot({
    blank_theme <- theme_minimal()+
      theme(
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        panel.border = element_blank(),
        panel.grid=element_blank(),
        axis.ticks = element_blank(),
        plot.title=element_text(size=14, face="bold")
      )

    test<-df.datasets()
    levels(test$"Region")[levels(test$"Region") %in% c("Region.1")] <- "Region 1"
    levels(test$"Region")[levels(test$"Region") %in% c("Region.2")] <- "Region 2"
    levels(test$"Region")[levels(test$"Region") %in% c("Region.3")] <- "Region 3"
    levels(test$"Region")[levels(test$"Region") %in% c("Region.4")] <- "Region 4"

    p<-ggplot(test,
              aes(x =" ", y = percent, fill = Type))+
      geom_bar(width = 1, stat = "identity",position="fill", color="grey35")+
      coord_polar("y")+
      facet_grid(.~Region)+
      geom_text(aes( label= paste(percent, "%")) ,
                position=position_fill(0.5),  size=4.5)+
                 theme(axis.text.x=element_blank(),
                       strip.text.x = element_text(size=15, color="black", face="bold"),
                       strip.text.y = element_text(size=15, color="black", face="bold"),
                       legend.text = element_text(size=15),
                       legend.title = element_text(size=15, face="bold"),
                       plot.title = element_text(hjust = 0.5, size = 25),
                       axis.text=element_text(size=10))
      })
}

my ui.R

myData <- NULL
sidebar <- dashboardSidebar(
  sidebarMenu(
    fileInput('file', 'Upload File (.csv | .txt)',
              accept=c('text/csv', 
                       'text/comma-separated-values,text/plain', 
                       '.csv')),
    radioButtons('sep', 'File Type',
                 c(CSV=';',
                   Text='\t'))
    #actionButton("goButton", "  Legenda", icon = icon("play-circle"))

  )
)

dashboardPage(
  dashboardHeader(title = "Overview"),

  sidebar,
  dashboardBody(tags$style("image {max-width: 100%; width: 100%; height: auto}"),
                fluidRow(
                  tabBox(
                    title = " ",
                    # The id lets us use input$tabset1 on the server to find the current tab
                    id = "tabset1",
                    height = "1200px", width = "1000px"
                    ,
                    tabPanel( "Plots",  
                              column(12,
                                     plotOutput("plot1"))

                  )

                )
  )))

I tried to strip down my code to detect the error but I can't find it. When I run the code outside of Shiny, the plot does show like it suppose to: enter image description here I Also don't get any error message. So I think that the plot is working but somehow it doesn't show. I tried modifying the size of the column but that didn't work..

I would like a result like this: enter image description here

Anybody got an idea? I would really appreciate it, thank you :)

1

1 Answers

0
votes

It looks like you assigned the plot to p then never called p in the renderPlot block, what happens if you just add p at the end of the block?