0

raw data I'm creating an Rshiny app that will allow a user to upload some clinical data, and view several different plots, based on the tabs they open. These include a line plot, pie chart, and mosaic plot. I'm able to view the line plot and pie chart, based on the uploaded data and user inputs, but having trouble getting the mosaic plot to appear. I get an error that says "object 'input' not found."

I tried to use the ggmosaic(geom_mosaic), and structable packages in R to display the plot. In my data table of interest, there are 5 columns: REF(reference method result for 2x2 contingency table, which is binary -- either POS or NEG clinical result), Result(4 diff values: True Positive, False negative, True negative, false positive), Value(number of patients for each result), SampleType(type of patient sample-- NS,NP, Overall are the 3 possible data values for this column) and Comparator(POS or NEG clinical result). In parenthesis, I have included the types of values one would expect for each column. Furthermore, For my R shiny mosaic app, I have several user inputs on the left hand side, which will allow the app to be constructed once the user has selected them: select input for REF column, select input for Sample type column, select input for comparator. I have code written inside the server function that uses these 3 inputs to construct the mosaic plot.

EDIT: I have attached my raw data in the link at the very top titled "raw data."

mosaic plot data table - takes data from pie chart, but displays it in a #different visual format

  MosaicDF <- reactive({
    
    #display mosaic 
    Mosaic_filtered <- select(PieData_extracted(),-c(3,5:7))
    
    
    
    #data transformation
    names(Mosaic_filtered)[1]<-"REF"
    Mosaic_filtered$SampleType <- "NS"
    Mosaic_filtered$Comparator <- c("POS","NEG","NEG","POS")
    Mosaic_filtered$REF <- c("POS","POS","NEG","NEG")
    Mosaic_filtered$F2 <- factor(as.character(Mosaic_filtered$Value))
    MYRaw <- Mosaic_filtered[rep(rownames(Mosaic_filtered),as.numeric(as.character(Mosaic_filtered$F2))), ]
    MYRaw <- as.data.frame(MYRaw)
    
    #update select input for mosaic plot
    updateSelectInput(session, inputId = 'REF', label = 'Select Reference column',
                      choices = names(MYRaw), selected = "")
    updateSelectInput(session, inputId = 'SampleType', label = 'Select Sample Type column',
                      choices = names(MYRaw), selected = "")
    updateSelectInput(session, inputId = 'Comparator', label = 'Select Comparator column',
                      choices = names(MYRaw), selected = "")
    
    return(MYRaw)
    
  })


 #display mosaic plot
  
  output$mosaic <- renderPlot({
    
    ggplot(data=MosaicDF())+geom_mosaic(aes(x=product(input$REF,input$Comparator),fill=input$REF))+labs(x="Comparator",y="REF")
  })
  
}

I'm getting the data table(from which the mosaic plot is constructed) to appear as an output, but the mosaic plot itself won't show up. It says:

"Error: object input not found".

The pie chart data table and pie chart itself do appear on the tab for this plot. (There are 3 tabs for each of the different plots within the R shiny app, of which the user can select any of these, choose some inputs from a dropdown menu, and allow an app to be automatically built based on the inputs).

I'm wondering if there's a way to modify the code for either my reactive data table or the plot itself-- should I change my code for ggplot, or use a different mosaic package for the Rshiny format?

Community
  • 1
  • 1
nk94
  • 11
  • 2
  • Hi there. Could you share your data or otherwise create a reproducible example? I know you've tried to describe what you're doing but it's hard for us to recreate. See here: https://stackoverflow.com/help/minimal-reproducible-example – Jaccar Jun 17 '19 at 16:41
  • I pasted a link above in my description, titled "raw data", which contains my data. – nk94 Jun 17 '19 at 17:09
  • People like to try to run your code and play around with changing different things. Having data in an image doesn't really help with that. – Axeman Jun 17 '19 at 18:39
  • I'm not able to upload my data as an Excel file, but I'm happy to share via email. – nk94 Jun 17 '19 at 20:27

1 Answers1

1

Without providing an example consisting of both data and code that folks can copy and run to reliably reproduce your error, it is difficult to say what thing(s) is(are) going wrong.

However, here is an example shiny app based on the titanic example in the help page for geom_mosaic().

library(ggmosaic)
library(rlang)
library(shiny)

ui <- fluidPage(

    sidebarLayout(
        sidebarPanel(
            selectInput("REF", "REF", "Survived"), 
            selectInput("Comparator", "Comparator", c("Class", "Sex", "Age"))
        ),

        mainPanel(
           plotOutput("old_mosaic"), 
           plotOutput("new_mosaic")
        )
    )
)

server <- function(input, output) {
    titanic_data <- reactive({
        data(Titanic)
        titanic <- as.data.frame(Titanic)
        titanic$Survived <- factor(titanic$Survived, levels=c("Yes", "No"))
        titanic
    })

    output$old_mosaic <- renderPlot({
        ggplot(data = titanic_data()) + 
            geom_mosaic(aes(weight = Freq, x = product(input$REF, input$Comparator), fill = input$REF)) + 
            labs(title = "Old Way")
    })

    output$new_mosaic <- renderPlot({
        ggplot(data=titanic_data()) +
            geom_mosaic(aes(weight = Freq, x = product(!!sym(input$REF), !!sym(input$Comparator)), fill = !!sym(input$REF))) + 
            labs(title = "New Way")
    })
}

shinyApp(ui, server)

The code that produces the first plot is similar to your ggplot code which attempts to use the input$id(s) as is. On my machine, this first plot produces the error you describe, and in other cases it seems this approach produces the same error.

The solution at the time of that post was to substitute aes_string() in place of aes(). However, here we should not do that because aes_string() is soft-deprecated; and more importantly, we cannot just use aes_string() because we still need to contend with the product() element.

Returning to the example app, notice the second plot is rendered without issue. In this code, I have employed the new idiomatic way which converts the input string to a symbol and then unquotes it.

Therefore, if I am correct, and this is the source of your error, then you should wrap each input$id with a !!sym() in your ggplot code.

the-mad-statter
  • 5,650
  • 1
  • 10
  • 20