Creating if functions with checkboxes in R-shiny - r

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

Related

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

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.

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.

R Shiny: keep old output

In a Shiny app, is there a way to keep old reactive output and display it in the app along with new reactive output? To provide an example: say I want to display the summary table of a linear model to which I gradually add more variables. I currently have a checkboxGroupInput panel using which I select the explanatory variables to be included in the model, and then I render a table which contains the summary of the model estimated using the selected variables. Now when I decide to use a different set of variables, I'd like to keep the original summary table displayed but also add the new summary table below the old one. If possible, I'd like to display all past instances of the reactive table, i.e., the number of tables displayed in the app should be equal to the number of different sets of explanatory variables I have decided to use throughout the app. At the moment, the table is rendered with htmlOutput in the ui part and stargazer package and renderText in the server part.
Here's an approach that works. It uses a reactiveValues object, and each time you click the "Fit Model" button, it appends the new model to the end of the list. The numeric input controls how many models to display on the screen. This preserves every model you fit in the session.
I didn't do the stargazer table because I'm not that familiar with it. But you should be able to adapt this pretty easily.
library(shiny)
library(broom)
shinyApp(
ui =
shinyUI(
fluidPage(
sidebarLayout(
sidebarPanel(
checkboxGroupInput(inputId = "indep",
label = "Independent Variables",
choices = names(mtcars)[-1],
selected = NULL),
actionButton(inputId = "fit_model",
label = "Fit Model"),
numericInput(inputId = "model_to_show",
label = "Show N most recent models",
value = 2)
),
mainPanel(
htmlOutput("model_record")
)
)
)
),
server =
shinyServer(function(input, output, session){
Model <- reactiveValues(
Record = list()
)
observeEvent(
input[["fit_model"]],
{
fit <-
lm(mpg ~ .,
data = mtcars[c("mpg", input[["indep"]])])
Model$Record <- c(Model$Record, list(fit))
}
)
output$model_record <-
renderText({
tail(Model$Record, input[["model_to_show"]]) %>%
lapply(tidy) %>%
lapply(knitr::kable,
format = "html") %>%
lapply(as.character) %>%
unlist() %>%
paste0(collapse = "<br/><br/>")
})
})
)

Shiny Correlation Plot

I'm trying to build a simple Shiny app, using USArrests database, that shows the correlation between density population and the 3 variables of crimes (Murder, Assault, Rape), changing the crime variables with selectInpuct.
Here the code of ui.R:
shinyUI(fluidPage(
titlePanel("Violent Crime Rates by US State"),
sidebarLayout(
sidebarPanel(
helpText("A series of plots that display correlation between density population and various kind of crimes"),
selectInput("var",
label = "Choose a crime",
choices = c("Murder"=1, "Assault"=2,
"Rape"=4),
selected = "Murder")
),
mainPanel(plotOutput('crimeplot'))
)
))
and the server.R
shinyServer(function(input,output){
output$crimeplot<- renderPlot({
x<-as.numeric(input$var)
y<-as.numeric(USArrests$UrbanPop)
plot(x, y, log = "xy")
})
}
but running the app it returns:
ERROR: 'x' and 'y' lengths differ
Could you help me, and explain what's wrong with what I am doing?
Thank you very much in advance!
I fould a couple of small errors that I have fixed in the code below.
Your selection returns the column number (as a string) and you need to convert it to a number and extract the relevant column from the data frame in server.R.
The default value of selected in ui.R should be the starting value and not the label.
The updated server.R looks as follows
shinyServer(function(input,output){
output$crimeplot<- renderPlot({
x<-as.numeric(USArrests[,as.numeric(input$var)])
y<-as.numeric(USArrests$UrbanPop)
plot(x, y, log = "xy", xlab=colnames(USArrests)[as.numeric(input$var)], ylab="Urban Pop")
})
})
and ui.R looks like this:
shinyUI(fluidPage(
titlePanel("Violent Crime Rates by US State"),
sidebarLayout(
sidebarPanel(
helpText("A series of plots that display correlation between density population and various kind of crimes"),
selectInput("var",
label = "Choose a crime",
choices = c("Murder"=1, "Assault"=2,
"Rape"=4),
selected = 1)
),
mainPanel(plotOutput('crimeplot'))
)
))

Resources