Make a boxplot interactive using Shiny - r

I've been trying to develop an interactive boxplot with selective input in Shiny.
current code:
library(shiny)
shinyUI(fluidPage(
titlePanel("Sample 1"),
sidebarLayout(
sidebarPanel(
selectInput("p", "Choose your salaries", choices = c("low"='a',"mid"='b',"high"='c',"riches!"='d'), selected = 4)
),
mainPanel(
plotOutput("boxplot")
)
)
))
library(shiny)
read.csv("Salaries.csv")
Categories <- cut (Salaries$TotalPay, breaks = c(0,36466,73678,104359,567595), labels = c("low","mid","high","riches!"))
shinyServer(function(input, output){
output$boxplot <- renderPlot({
if(input$p=='a'){
i<"1"
}
if(input$p=='b'){
i<-"2"
}
if(input$p=='c'){
i<-"3"
}
if(input$p=='d'){
i<- "riches!"
}
boxplot(TotalPay~Categories[i])
})
})
I can't get the boxplot to react to the selection made in the UI. I suspect it has to do with the levels as when I call:
> Categories["riches!"]
[1] <NA>
Levels: low mid high riches!
'
Do i need to add factors to these? Or am I missing the point entirely?
Thanks in advance!

Have a look how to access the column by name. Example below is with mtcars dataset
library(shiny)
ui <- fluidPage(
selectInput("p","p",choices = names(mtcars)),
plotOutput("myplot"))
server <- function(input, output, session) {
output$myplot <- renderPlot({
boxplot(mtcars[,input$p])
})
}
shinyApp(ui, server)

Related

Scatter plots for different variables in a single shiny app

I use R shiny to create multiple tabs for the scatter plot of different variables in a single app. My code seems alright, but the error says I have not "defined my main panel."
Can someone please help where I have gone wrong, or if my whole approach is inaccurateenter code here, please let me know!
library(shiny)
library(tidyverse)
India <- read.csv("D:/R/Practice 3/Indiadata.csv")
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Deaths vs all variables "),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectInput("Deaths", "All variables:",
choices = c("cases"="total_cases","vaccinations"="total_vaccinations",
"people vaccinated"="people_vaccinated","people fully vaccinated"="people_fully_vaccinated",
"total booster"="total_boosters","new vaccinations"="new_vaccinations", "median age"="median_age"))
)
),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel(plotOutput("plot1")),
tabPanel(plotOutput("plot2")),
tabPanel(plotOutput("plot3")),
tabPanel(plotOutput("plot4")),
tabPanel(plotOutput("plot5")),
tabPanel(plotOutput("plot6")),
tabPanel(plotOutput("plot7"))
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$plot1 <- renderPlot({
ggplot(India,aes(y=total_deaths,x=total_cases))
})
output$plot2 <- renderPlot({
ggplot(India,aes(y=total_deaths,x=total_vaccinations))
})
output$plot3 <- renderPlot({
ggplot(India,aes(y=total_deaths,x=people_vaccinated))
})
output$plot4 <- renderPlot({
ggplot(India,aes(y=total_deaths,x=people_fully_vaccinated))
})
output$plot5 <- renderPlot({
ggplot(India,aes(y=total_deaths,x=total_boosters))
})
output$plot6 <- renderPlot({
ggplot(India,aes(y=total_deaths,x=new_vaccinations))
})
output$plot7 <- renderPlot({
ggplot(India,aes(y=total_deaths,x=median_age))
})
}
# Run the application
shinyApp(ui = ui, server = server)
mainPanel is an argument of the sidebarLayout() function. So, you just need to move it up into the sidebarLayout() function:
ui <- fluidPage(
# Application title
titlePanel("Deaths vs all variables "),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectInput("Deaths", "All variables:",
choices = c("cases"="total_cases","vaccinations"="total_vaccinations",
"people vaccinated"="people_vaccinated","people fully vaccinated"="people_fully_vaccinated",
"total booster"="total_boosters","new vaccinations"="new_vaccinations", "median age"="median_age"))
),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel(plotOutput("plot1")),
tabPanel(plotOutput("plot2")),
tabPanel(plotOutput("plot3")),
tabPanel(plotOutput("plot4")),
tabPanel(plotOutput("plot5")),
tabPanel(plotOutput("plot6")),
tabPanel(plotOutput("plot7"))
)
)
)
)

R Shiny: varSelectInput only numeric variables from dataframe

In a shiny app I have a varSelectInput widget to select variables from the iris dataset. Is there a way where I can limit the variables in the varSelectInput to numeric variables only. I tried with is.numeric(iris), but no had no success. Thanks.
my code:
library(shiny)
library(ggplot2)
# single selection
shinyApp(
ui = fluidPage(
varSelectInput("variable", "Variable:", is.numeric(iris),
selected = NULL),
plotOutput("data")
),
server = function(input, output) {
output$data <- renderPlot({
ggplot(iris, aes(!!input$variable)) + geom_histogram()
})
}
)
You can try with Filter :
library(shiny)
library(ggplot2)
shinyApp(
ui = fluidPage(
varSelectInput("variable", "Variable:", Filter(is.numeric, iris),
selected = NULL),
plotOutput("data")
),
server = function(input, output) {
output$data <- renderPlot({
ggplot(iris, aes(.data[[input$variable]])) + geom_histogram()
})
}
)

How do I complete this code for Shiny App?

I am struggling in writing this shinyApp. It's main aim is to investigate on the variables of a dataset. First it produces the summary statistics on the selected variable.
In the second part; I want this app to give me the pairplot of the varibles that I have selected in the checkbox in the UI. I have used the dataset IRIS which is available to everyone, but I need the code to be adaptable to other datasets.
Can someone please help me?
library(shiny)
library(plotly)
data(iris)
ui<-fluidPage(
titlePanel("Iris"),
sidebarLayout(
sidebarPanel(
selectInput("var",label="Choose a variable",
choice=list("Sepal.Length"=1, "Sepal.Width"=2, "Petal.Length"=3, "Petal.Width"=4, "Species"=5), selectize=FALSE),
checkboxGroupInput(inputId ="independent",label = "Select independent variables", choices = names(iris)),
mainPanel(
verbatimTextOutput("sum"),
plotlyOutput('plot_id_in_ui ', height = "900px")
)
))
)
server<-function(input,output){
output$sum <- renderPrint({
summary(iris[, as.numeric(input$var)])
})
output$plot_id_in_ui <- renderplot( { "DON'T KNOW HOW TO WRITE THIS PART"
pairplot(iris, varnames, type = "both", penalty.par.val = "lambda.1se",
nvals = c(20, 20), pred.type = "response") } )
})
shinyApp(ui, server)
Maybe this little example can help you. It illustrates how to plot a normal R-Plot and a Plotly-Plot in a ShinyApp:
library(shiny)
library(plotly)
ui <- fluidPage(
titlePanel("Iris"),
sidebarLayout(
sidebarPanel(
selectInput("var",label="Choose a variable",
choice=list("Sepal.Length"=1, "Sepal.Width"=2, "Petal.Length"=3, "Petal.Width"=4, "Species"=5), selectize=FALSE),
checkboxGroupInput(inputId ="independent",label = "Select independent variables", choices = names(iris))
),
mainPanel(
verbatimTextOutput("sum"),
plotOutput("plot"),
plotlyOutput("plotly")
)
)
)
server <- function(input,output) {
output$sum <- renderPrint({
summary(iris[, as.numeric(input$var)])
})
output$plot <- renderPlot({
plot(iris)
})
output$plotly <- renderPlotly({
plot_ly(iris) %>%
add_trace(x=iris$Sepal.Length, y=iris$Sepal.Width, type="scatter", mode="markers")
})
}
shinyApp(ui, server)

Using shiny to highlight specific bar in a bar chart

I am not sure if Im doing this the right way (I am open for suggestions!). However what I try to do if to create a Shiny app where i can pick a bar and then the bar should be highlighted in the graph.
For this example I use the titanic_train dataset.
I do:
library(shiny)
library(ggplot2)
library(titanic)
library(dplyr)
UI <- fluidPage(
# Application title
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(
selectInput("specific_bar", "Pick bar to highlight:",
choices = unique(titanic_train$Embarked))
),
mainPanel(
plotOutput("plot_nice")
)
)
)
Server <- function(input, output) {
filtered <- reactive({
titanic_train$Specific <- ifelse((titanic_train$Embarked == input$specific_bar), 1,0)
})
output$plot_nice <- renderPlot({
ggplot(filtered(), aes_string(x="Embarked", y="Survived", fill = "Specific")) +
geom_bar(stat = "identity")
})
}
shinyApp(ui = UI, server = Server)
Running this however gives me the following error:
ggplot2 doesn't know how to deal with data of class numeric
And the problem really seems to have to do with the filtered() reactive function. Any thoughts on what is going wrong here?
you have to ask for the data.frame object back in the reactive part,
what you were doing is getting a vector back instead of getting another column added to titanic_train.
this should fix it:
library(shiny)
library(ggplot2)
library(titanic)
library(dplyr)
UI <- fluidPage(
# Application title
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(
selectInput("specific_bar", "Pick bar to highlight:",
choices = unique(titanic_train$Embarked))
),
mainPanel(
plotOutput("plot_nice")
)
)
)
Server <- function(input, output) {
filtered <- reactive({
titanic_train$Specific <- ifelse((titanic_train$Embarked == input$specific_bar), 1,0)
return(titanic_train)
})
output$plot_nice <- renderPlot({
ggplot(filtered(), aes_string(x="Embarked", y="Survived", fill = "Specific")) +
geom_bar(stat = "identity")
})
}
shinyApp(ui = UI, server = Server)

R zoomChart Shiny

i've been trying to interactively zoom in certain parts of a chartSeries with zoomChart and shiny, but can't find the right solution. I would use dateRangeInput or a slider, but i'm not sure how to connect the zoomChart-option from quantmod with shiny. As you might have already assumed, I'm relatively new to shiny and very thankful for your advices!
edit: Data is in the xts-format.
MyCode:
library(quantmod)
library(shiny)
date_range <- as.POSIXct(index(data))
if (interactive()) {
options(device.ask.default = FALSE)
ui <- fluidPage(
titlePanel("Select Range to zoom-in:"),
sidebarLayout(
sidebarPanel(
dateRangeInput("Range", "Choose Date Range:", min=first(date_range),
max=last(date_range), format = "dd-mm-yyyy")
),
mainPanel(
plotOutput("Plot")
)
)
)
server <- function(input, output) {
output$Plot <- renderPlot({
chartSeries(data, type = c("auto", "candlesticks", "matchsticks", "bars","line"),
theme=chartTheme("white"), name=paste(start(data), end(data),sep = " "))
zoomChart(dateRangeInput)
})
}
shinyApp(ui, server)
}
Actually, you were very close. Note the changes in dateRangeInput(): The start and end argument are used instead of min, max. And then you can use the input on the server-side to use zoom-chart.
library(quantmod)
library(shiny)
getSymbols("YHOO")
data <- YHOO
date_range <- index(data)
if (interactive()) {
options(device.ask.default = FALSE)
ui <- fluidPage(
titlePanel("Select Range to zoom-in:"),
sidebarLayout(
sidebarPanel(
dateRangeInput("Range", "Choose Date Range:", start=first(date_range),
end=last(date_range), format = "yyyy-mm-dd")
),
mainPanel(
plotOutput("Plot")
)
)
)
server <- function(input, output) {
output$Plot <- renderPlot({
chartSeries(data, type = c("auto", "candlesticks", "matchsticks", "bars","line"),
theme=chartTheme("white"), name=paste(start(data), end(data),sep = " "))
zoomChart(paste(input$Range, collapse = "::"))
})
observe({
print(input$Range)
})
}
shinyApp(ui, server)
}
As #drmariod indicated It would be beneficial to have a fully reproducible exmaple, which was easy to get in this case via getSymbols().

Resources