How to remove NA value from the ggplot in shiny app? - r

library(shiny)
library(palmerpenguins)
library(ggplot2)
library(dplyr)
penguin <- penguins
penguin$year <- as.factor(penguin$year)
ui <- fluidPage(
titlePanel("Data Visualisation of Penguins Data"),
sidebarPanel(
selectInput("yaxis",
label = "Choose a y-axis variable to display",
choices = list("bill_length_mm",
"bill_depth_mm",
"flipper_length_mm",
"body_mass_g"),
selected = "bill_length_mm"),
selectInput("xaxis",
label = "Choose a x-axis variable to display",
choices = c("species",
"sex",
"year"),
selected = "sex"),
checkboxGroupInput("islandlevels",
label = "Check to display different island levels",
choices = c("island"),
selected = NULL),
br(), br(),
selectInput("species",
label = "Choose species to view separate plot",
choices = list("Adelie",
"Chinstrap",
"Gentoo"),
selected = NULL)),
mainPanel(
plotOutput("plot1"),
br(), br(),
plotOutput("plot2")
)
)
server <- function(input, output){
output$plot1 <- renderPlot({
if(is.null(penguin))
return(NULL)
ggplot(penguin, aes(x = penguin[[input$xaxis]], y = penguin[[input$yaxis]])) +
geom_boxplot()
})
}
shinyApp(ui = ui, server = server)
This is my shiny code, but I'd like to remove NA value when x-axis variable is sex.
I can't just remove row with NA values because I have to use variable (that is not missing value but the row has missing value such as row 9 in image 2) when I change x-axis variable or/and y-axis variable.
I wanted to find the solution but I wonder what function should I use. Do I have to use if statement, reactive function, or else?
Thank you for help in advance.
sex variable with NA value(want to delete NA on my plot)

You can prevent the NA values of showing up as categories by making use of scale_x_discrete(na.translate = FALSE):
library(ggplot2)
library(palmerpenguins)
ggplot(penguins, aes(x = sex, y = bill_length_mm)) +
geom_boxplot() +
scale_x_discrete(na.translate = FALSE)
#> Warning: Removed 11 rows containing missing values (stat_boxplot).

Conditionally filter your data, perhaps something like this:
dat <- reactive({
if (input$xaxis == "sex") penguin[ !is.na(penguin$sex), ] else penguin
})
output$plot1 <- renderPlot({
req(penguin, input$xaxis, input$yaxis)
ggplot(dat(), aes_string(x = isolate(input$xaxis), y = input$yaxis)) +
geom_boxplot()
})
Several critical changes here:
In case you want to do more than a single plot with the filtered data, I make a reactive data component named dat with the filtered data. In this way, if you ever add (say) a table or another plot or something, you don't need to handle selective filtering in each of them, you just need to use dat() everywhere and everything benefits from it.
Reactive can be volatile, and having both the data and the plot reacting to input$xaxis will cause the plot to be rendered twice for each change to xaxis. Because of this, I isolate(input$xaxis) in the plot reactive. When the user changes xaxis, the dat will change which will trigger (once!) the plot to change. (No need to isolate yaxis, as that's correct in this case.)
In general, you should not use ggplot2(x, aes(x$a, x$b)). More specifically, using $ and/or [[ in aesthetic definitions is poor practice, and will fail given certain situations. It is much better to use aes with symbols (e.g., cyl from mtcars) or aes_string with strings ("cyl"). Since you're defining the aesthetics programmatically, it is better to use aes_string.
I changed your if (is.null(penguin)) to shiny's more canonical req, and added checks in the inputs as well. While most simpler shiny apps don't always need this, I've found that more complex apps can cause just enough delay in input instantiation that an output reactive block may trigger before all inputs have been assigned, meaning in this example it might be possible for input$xaxis to be null. While unlikely in simpler shiny apps like this, I still think it's safe.
There may be reasons to use individual req lines, one for each input. The results in this case will be the same, but there are times when it makes sense to break them out.
The use of req prohibits the rest of the plot rendering from occurring, but it also does it in a way that shiny components recognize, not causing errors or rendering issues. (I prefer it to manual if (is.null(.)) return(NULL) logic.)
Note: I think #stefan's answer may be the more canonical way in ggplot2 to omit NA values from the axis, so perhaps that is the best way to go for that side of things. However, I still believe that points 3 and 4 are still (also) relevant to your app even with stefan's change.

Related

Creating if functions with checkboxes in R-shiny

I am currently doins a personal project to get used to using R-shiny. I am using the penguins dataset in R. This project is creating several different boxplots. I have been able to create the main code for the boxplots to show and now I am trying to use the checkbox input to allow the user to select if it wants the boxplots to be divided by the Islands ivestigated or rather just see the data as it.
My code is the following.
library("palmerpenguins")
library("shiny")
library("ggplot2")
penguins.data<- penguins
sum(is.na(penguins))
#As their are a few penguins with missing values (19 out of 2752) we decide to carry out the data visualization with
#Only the complete cases of the data
penguins.data<-na.omit(penguins.data)
#Making sure the categorical variables are factors
str(penguins.data)
penguins.data$species<-as.factor(penguins.data$species)
penguins.data$sex<-as.factor(penguins.data$sex)
penguins.data$year<-as.factor(penguins.data$year)
penguins.data$island<-as.factor(penguins.data$island)
#Defining the UI for the App.
ui <- fluidPage(
#Adding a suitable title
titlePanel("Penguin exploration"),
#Getting the layout
sidebarLayout(
#Setting the panel for the used to select the inputs they want
sidebarPanel(
#Selecting the variable for the X-axis
selectInput("horiz", "Select x-axis variable:",
c("Sex" = "sex",
"Species" = "species",
"Year" = "year"),
selected = "sex" ),
#Selecting the variable in the Y-axis
selectInput("vert", "Select y-axis variable:",
c("Bill Length" = "bill_length_mm",
"Bill Depth" = "bill_depth_mm",
"Flipper length" = "flipper_length_mm",
"Body mass" = "body_mass_g"),
selected = "flipper_length_mm" ),
#We create the checkbox input for the user to select if they want to see the data in
checkboxInput("Divide", "check to look at how data is divided by Island Level", value = F)),
mainPanel(
#Setting a title for the output
h3("plot"),
#We decide how to name the plot to use it in the output
plotOutput("PengPlot")
),
)
)
server <- function(input, output) {
horizontal<-reactive(input$horiz)
vertical<-reactive(input$vert)
output$PengPlot <- renderPlot({
if(output$Divide){
ggplot(data = penguins.data, aes_string(horizontal(), vertical()))+
geom_boxplot(aes(fill= island))+
facet_wrap(~horizontal())
}else{
ggplot(data = penguins.data, aes_string(horizontal(), vertical()))+
geom_boxplot()
}
})
}
shinyApp(ui = ui, server = server)
I am currently getting the error Reading from shinyoutput object is not allowed. I am lost on what specifically to do. I am considering if maybe creating both boxplots as reactive objects and then use the if functions but I have seen in other posts that doing that may overcomplicate the code.
Any advice or help will be great. Thank in advance

Beginner - I want to make ggplot on Shiny in R and I don't know where I made up mistake

I am using Shiny in R to make two different tabs.
I think one tab would be a good idea to make a summary tab and another one is for the plot.
I want to give user a selection of x, y and color section... When I finished the part of ggplot section, the plot is not shown as I expected.
Please help me out where I made up mistake and please help me to understand of it.
Thank you.
# ui.R
library(shiny)
library(ggplot2)
library(plyr)
library(dplyr)
data(mtcars)
new_mtcars <- mtcars %>%
select(wt,mpg,cyl,vs,am,gear,carb)
# Define UI for an application that draws a histogram
shinyUI(fluidPage(
# Application title
titlePanel("prac"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectInput("xvalue","Please Choose X Value : ", c("wt","mpg")),
selectInput("yvalue","Please Choose Y Value : ", c("wt","mpg")),
selectInput("color","Please Choose color Value : ", choices = c("cyl","vs","am","gear","carb"))
),
mainPanel(
tabsetPanel(#tabPanel("Information",tableOutput("info")),
tabPanel("Summary",tableOutput("summary")),
tabPanel("Comparison",plotOutput("plot")))
)
)
# Show a plot of the generated distribution
))
library(shiny)
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
df_sub <- reactive({
new_mtcars[,c(input$xvalue,input$yvalue,input$color)]
})
output$plot <- renderPlot({
category <- input$color
ggplot(df_sub(), aes(input$xvalue,input$yvalue)) +
geom_point(aes_(color = as.name(category)),size = 3) +
geom_smooth(method = "lm")
})
})
The issue is that input$xvalue and input$yvalue are characters. To tell ggplot that it should look for variables with these names in your dataset
use .data[[input$xvalue]] and .data[[input$yvalue]] inside aes() or
use aes_string instead of aes().

How to fix "object 'mydata' not found" in shiny app when plotting histogram

I'm trying to create a Shiny app that lets users
create a dataset by entering frequency counts for different values
plot a histogram of that dataset
A paired back example of the code is as follows:
library(shiny)
library(ggplot2)
# Define UI for application
ui <- fluidPage(
# Sidebar with inputs
sidebarLayout(
sidebarPanel(
numericInput("data1s",
"How many have a score of 1?",
value = 0,
min = 0
),
numericInput("data2s",
"How many have a score of 2?",
value = 0,
min = 0
),
sliderInput("bins",
"Number of bins:",
min = 1,
max = 3,
value = 1)
),
# Show a plot of the data
mainPanel(
htmlOutput("mydatatable"),
plotOutput("distPlot")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
#show the data
output$mydatatable <- renderTable({
#create the dataframe from the frequncies
mydata <- data.frame(our_data=c(rep(1,input$data1s),rep(2,input$data2s))
)
}
)
#show the histogram
output$distPlot <- renderPlot({
ggplot(mydata, aes(x=our_data)) +
geom_histogram(bins = input$bins)
})
}
# Run the application
shinyApp(ui = ui, server = server)
I have achieved the creation of the dataset, but the code for displaying a histogram of the data returns an error: "object 'mydata' not found" instead of showing the histogram. The histogram should update whenever any of the inputs are changed.
Any help to resolve would be much appreciated.
The mydata that you define in the mydatatable reactive is not visible anywhere else. To understand why, I suggest you read about R's namespaces and environments; one good tutorial on it is Hadley's Advanced R -- Environments.
To fix it, I suggest you make the data itself a reactive block, and depend on it in your two other blocks (table and plot):
server <- function(input, output) {
mydata <- reactive({
req(input$data1s, input$data2s)
data.frame(our_data=c(rep(1,input$data1s),rep(2,input$data2s)))
})
#show the data
output$mydatatable <- renderTable({ req(mydata()); })
#show the histogram
output$distPlot <- renderPlot({
req(mydata())
ggplot(mydata(), aes(x=our_data)) +
geom_histogram(bins = input$bins)
})
}
(Untested.)
I added the use of req solely to prevent start-up jittering and warnings/errors in the app. When the shiny app is warming up, it's common to have input variables empty (NULL), and things that depend on it will temporarily produce errors until the inputs stabilize. (For an example of why things will stumble, input$data1s may initially show a NULL value, and try to see if data.frame(our_data=rep(1,NULL)) will work.)
req just looks for something that is "truthy", meaning: not NULL, not NA, not FALSE, length greater than 0, etc. See ?shiny::req for more details.
While req is not strictly required, it has its advantages. As you may infer from the table code, req(x) will return the "first value that was passed in" (from ?req), so it can be used in this shortcut mode for brevity.
And one last soap-box: in my limited experience with shiny reactivity, there are few times that I've generated data within a reactive block and used it solely within that reactive block. Given that, whenever you make a data.frame (or list or ... some important structure that is dependent on user input), it is often beneficial to make it its own reactive component (specifically, not an output component), and then depend on it as many times as necessary.

Having trouble getting 2x2 table Mosaic Plot to display in R-shiny

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?
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.

How can I use conditions to add data to a ggplot making it interactive?

This is my first question on stackoverflow, so please forgive me if my problem is not perfectly described.
I am working on an interactive plot using R shiny. The aim is to compare air quality data for different cities. This should be done in a ggplot where the user can select a pollutant (y-axis) and a possible correlation factor (e.g. air temperature, x-axis). The user should then be able to select all the cities (as CheckboxGroupInput) of which the data should be plotted. Selecting the two variables (x-/y-axis) works out fine, however I struggle to plot several cities at once.
I already created the inputs, that seem to work out fine. I can also plot one city at a time. I also managed to plot several selected cities, however they are not plotted in the same ggplot, but only the topmost plot is visible (see simplified code below).
UI:
library(shiny)
library(ggplot2)
berlin_d <- read.csv("berlin_d.csv")
london_d <- read.csv("London_d.csv")
warsaw_d <- read.csv("Warsaw_d.csv")
checkboxGroupInput(
inputId = "city",
label = "select a city/multiple cities",
choices = c(
"Berlin" = "Berlin",
"London" = "London",
"Warsaw" = "Warsaw"
)
),
selectInput(
inputId = "box1",
label = "select a variable to plot (x-axis)",
choices = c("temperature" = "temp",
"month" = "month",
"weekday" = "weekday"
),
selected = "temp"
),
selectInput(
inputId = "box2",
label = "select a pollutant to plot (y-axis)",
choices = c("Ozone" = "O3",
"NO2" = "NO2",
"PM10" = "PM10"
),
)
Server:
output$plot <- renderPlot(ggplot()+
geom_point(if (input$city=="Berlin") {aes(berlin_d[[input$box1]], berlin_d[[input$box2]])})+
geom_point(if (input$city=="London") {aes(london_d[[input$box1]], london_d[[input$box2]])})+
geom_point(if (input$city=="Warsaw") {aes(warsaw_d[[input$box1]], warsaw_d[[input$box2]])})
)
I don't understand why the data isn't displayed in the same plot. Is there a way to plot the data in one ggplot and still have the options to select the cities?
Any help is appreciated!
To answer your question a small change in your code should be enough to create the functionality you are looking for.
You have to look at the output of input$city. If you check more than one box the vector length changes and then only the first element will be used when checking the if-clause. To avoid this, you can rewrite the if-clause as follows
if ("Berlin" %in% input$city)
The whole plot would look like this.
ggplot() +
geom_point(if ("Berlin" %in% input$city) {aes(berlin_d[[input$box1]], berlin_d[[input$box2]])}) +
geom_point(if ("London" %in% input$city) {aes(london_d[[input$box1]], london_d[[input$box2]])}) +
geom_point(if ("Warsaw" %in% input$city) {aes(warsaw_d[[input$box1]], warsaw_d[[input$box2]])})
However, a much better approach would be to create one data set containing all the data, where city is just a grouping variable. Then create a reactive Expression subsetting the data in shiny according to the input filter (input$city). Then you can create a plot with one call to ggplot and setting city as a factor variable for colour, for example.

Resources