ggplot is not getting plotted

...i am uploading a csv file and trying to plot a scatter based on user input values. but on clicking plot a blank whte box pops up. can't understand if the issue is with ggplot command or with accessing the user input.

also a code has been written to create matrix by user input but does not how to access this data. we would like to plot a scatterplot based on these matrix too.

ps: sorry for the huge chunk of code but since i didn't know where the error seems to be happening i have put it all

library(shiny)
library(ggplot2)
ui <- fluidPage(
  titlePanel(" REGRESSION "),
  sidebarLayout(
    sidebarPanel(
      h4("DEFINITION ")
      ),
    mainPanel(
      column(9,  navbarPage(title = "REGRESSION MENU ",
                            
                            navbarMenu("DATA INPUT",
                                       tabPanel("IMPORT FILE",fileInput("file", h3("File input"))),
                                       tabPanel("VIEW DATA", tableOutput("table1")),
                                       tabPanel("INPUT DATA",uiOutput("lot_of_input"),tableOutput("value"),uiOutput("textoutput")),
                                       tabPanel("SUMMARY", verbatimTextOutput("summ"))),
                            
                            navbarMenu(" LINEAR REGRESSION ",
                                       tabPanel("SELECT DATA",uiOutput("selectbox1"),uiOutput("selectbox11")),
                                       tabPanel("PLOT",uiOutput("plot1")))))
    )
  )
)
server <- function(input, output) {
  
  #read file
  data <- reactive({
    file1 <- input$file
    if(is.null(file1)){return()} 
    read.table(file=file1$datapath,sep=",")
  })
  
  #print file in the view tab
  output$table1 <- renderTable({
    if(is.null(data())){return ()}
    data()
  })
  
  output$plot1<-renderUI({
    obj<-data.frame(input$select_dev1,input$select_dev11)
    plotOutput("p")
  })
  
  output$p <- renderPlot({
    plot1.obj<-data()
    p<-ggplot(plot1.obj)+
      aes(geom_point(
        x       = plot1.obj$select_dev1,
        y       = plot1.obj$select_dev11
      ))
    })
  
  #summary of file 
  output$summ <- renderPrint({
    if(is.null(input$file)){return()}
    summary(read.table(file=input$file$datapath, 
                       sep=",",header=TRUE))})
  #matrix input option in input data tab
  output$lot_of_input <- renderUI({
    x <- list(
      numericInput(inputId = "nrow",label = "number of rows",min = 1,max = 20,value = 1),
      numericInput(inputId = "ncol",label = "number of columns",min = 1,max = 20,value = 1))
    fluidRow(
      lapply(
        X = split(x, f = rep(c(1, 2), length.out = length(x))),
        FUN = column, width = 6
      )
    )
  })
  
  #selectbox dependent option in LR
  output$selectbox1 <- renderUI({
    selectInput(inputId = "select_dev1",
                label = "Select dependent variable",
                choices = names(data()))
  })
  
  #selectbox independent option in LR
  output$selectbox11 <- renderUI({
    selectInput(inputId = "select_dev11",
                label = "Select independent variable",
                choices = names(data()))
    })
  #matrix diplay option in tab
  isolate({
    output$value <-renderTable({
      num.inputs.col1 <- paste0("<input id='r", 1:input$nrow, "c", 1, "' class='shiny-bound-input' type='number' value='1'>")
      df <- data.frame(num.inputs.col1)
      if (input$ncol >= 2){
        for (i in 2:input$ncol){
          num.inputs.coli <- paste0("<input id='r", 1:input$nrow, "c", i, "' class='shiny-bound-input' type='number' value='1'>")
          df <- cbind(df,num.inputs.coli)
        }
      }
      
      colnames(df) <- paste0(" variable ",as.numeric(1:input$ncol))
      df
    }, sanitize.text.function = function(x) {x})
  })
  output$textoutput <- renderUI(paste0("Cells [1,1] and [2,2]: ",input$r1c1,",",input$r2c2))
}
shinyApp(ui = ui, server = server)

I see a few problems with your code. In this section

output$plot1<-renderUI({
    obj<-data.frame(input$select_dev1,input$select_dev11)
    plotOutput("p")
  })

you try to use renderUI to make a plot, which I do not think will work, you define the variable obj which is never used and you pass "p", a text character, to plotOutput(). Even if p was not in quotes, it is not defined anywhere within the renderUI. I suppose you intend that p to come from this code

output$p <- renderPlot({
    plot1.obj<-data()
    p<-ggplot(plot1.obj)+
      aes(geom_point(
        x       = plot1.obj$select_dev1,
        y       = plot1.obj$select_dev11
      ))
    })

but the code inside of that renderPlot() is not visible to the renderUI(). Also, the call to ggplot() is not correct, you cannot place geom_point() inside of aes() and the columns in object plot1.obj cannot be referred to as select_dev1 and select_dev11.

Here is a modified version of your code that I used to make a plot. There may be other problems, as I have not looked beyond the plotting section. I marked with comments where I made changes.

library(shiny)
library(ggplot2)
ui <- fluidPage(
  titlePanel(" REGRESSION "),
  sidebarLayout(
    sidebarPanel(
      h4("DEFINITION ")
    ),
    mainPanel(
      column(9,  navbarPage(title = "REGRESSION MENU ",
                            
                            navbarMenu("DATA INPUT",
                                       tabPanel("IMPORT FILE",fileInput("file", h3("File input"))),
                                       tabPanel("VIEW DATA", tableOutput("table1")),
                                       tabPanel("INPUT DATA",uiOutput("lot_of_input"),tableOutput("value"),uiOutput("textoutput")),
                                       tabPanel("SUMMARY", verbatimTextOutput("summ"))),
                            
                            navbarMenu(" LINEAR REGRESSION ",
                                       tabPanel("SELECT DATA",uiOutput("selectbox1"),uiOutput("selectbox11")),
                                       tabPanel("PLOT", plotOutput("plot1"))))) ###FJCC
    )
  )
)
server <- function(input, output) {
  
  #read file
  data <- reactive({
    file1 <- input$file
    if(is.null(file1)){return()} 
    read.table(file=file1$datapath,sep=",")
  })
  
  #print file in the view tab
  output$table1 <- renderTable({
    if(is.null(data())){return ()}
    data()
  })
  
  ######## FJCC
   output$plot1<-renderPlot({
     plot1.obj<-data()
     ggplot(plot1.obj, aes(x = plot1.obj[[input$select_dev11]],
                              y = plot1.obj[[input$select_dev1]])) + 
       geom_point()
   })
  #############
   

  
  #summary of file 
  output$summ <- renderPrint({
    if(is.null(input$file)){return()}
    summary(read.table(file=input$file$datapath, 
                       sep=",",header=TRUE))})
  #matrix input option in input data tab
  output$lot_of_input <- renderUI({
    x <- list(
      numericInput(inputId = "nrow",label = "number of rows",min = 1,max = 20,value = 1),
      numericInput(inputId = "ncol",label = "number of columns",min = 1,max = 20,value = 1))
    fluidRow(
      lapply(
        X = split(x, f = rep(c(1, 2), length.out = length(x))),
        FUN = column, width = 6
      )
    )
  })
  
  #selectbox dependent option in LR
  output$selectbox1 <- renderUI({
    selectInput(inputId = "select_dev1",
                label = "Select dependent variable",
                choices = names(data()))
  })
  
  #selectbox independent option in LR
  output$selectbox11 <- renderUI({
    selectInput(inputId = "select_dev11",
                label = "Select independent variable",
                choices = names(data()))
  })
  #matrix diplay option in tab
  isolate({
    output$value <-renderTable({
      num.inputs.col1 <- paste0("<input id='r", 1:input$nrow, "c", 1, "' class='shiny-bound-input' type='number' value='1'>")
      df <- data.frame(num.inputs.col1)
      if (input$ncol >= 2){
        for (i in 2:input$ncol){
          num.inputs.coli <- paste0("<input id='r", 1:input$nrow, "c", i, "' class='shiny-bound-input' type='number' value='1'>")
          df <- cbind(df,num.inputs.coli)
        }
      }
      
      colnames(df) <- paste0(" variable ",as.numeric(1:input$ncol))
      df
    }, sanitize.text.function = function(x) {x})
  })
  output$textoutput <- renderUI(paste0("Cells [1,1] and [2,2]: ",input$r1c1,",",input$r2c2))
}
shinyApp(ui = ui, server = server)

1 Like

You will greatly benefit by learning how to create a reprex, e.g. https://mastering-shiny.org/action-workflow.html#reprex. This will not only make it easier for other people to help you, but it will also help you figure out where exactly the problem is.