How to make a boxplot for Shiny web page - r

Good day
I am trying to make a simple boxplot for a shiny web page but for some reason, it is not working.
I did it in markdown using the below code and I got the results I wanted.
ggplot(test, aes(x = Date, y = test$Var1, group = Date)) +
geom_boxplot()
This is what I get in markdown, which is also what I want for the shiny web page
The following code is what I am using for the shiny web page
library(readxl)
library(shiny)
library(ggplot2)
library(dplyr)
ui <- fluidPage(
titlePanel("questionnaire"),
sidebarLayout(
sidebarPanel(
selectInput("question", "Choose a question",
colnames(test))
),
mainPanel(
plotOutput("coolplot")
)
)
)
server <- function(input, output) {
output$coolplot <- renderPlot(
ggplot(test, aes(x = Date, y = input$question, group = Date)) +
geom_boxplot()
)
}
shinyApp(ui = ui, server = server)
As you can see the code I use to create the boxplots here is almost the same as what I used in markdown.
So why do I get this when I run the app?
Any help in fixing this problem will be greatly appreciated!

I had to use the following code to fix my problem
server <- function(input, output) {
output$coolplot <- renderPlot(
ggplot(test, aes(x = Date, y = get(input$question), group = Date)) +
geom_boxplot()
)

Related

How to display ggplot2 on Rshiny main panel

The ggplot2 object doesnt display properly in RShiny mainpanel. For recreation, the below code uses iris dataset. Need help
I checked the link - RShiny ggplot2 not showing , but this didnt help. I also ran through https://shiny.rstudio.com/ website, but nothing had explanation with example on how to display the ggplot2 object. I used renderPlot and renderImage functions, but none gave required results.
'''
library(shiny)
library(shinydashboard)
ui <- fluidPage(titlePanel("Sample Shiny"),
navbarPage(
br(),
tabPanel(h4("Iris Data"),
sidebarPanel(
radioButtons("var1",
label = "Choose a FILL field",
choices = c("Species"),
selected = "Species"),
mainPanel(plotOutput("plot",click = "plot_click")))
)
))
server <- function(input, output) {
output$plot <- renderPlot(
{
#browser()
sw <- input$var1
### "a" below is iris dataset which I pass on as input**
ggplot(data = a) +
aes(x = Sepal.Length, fill = sw) +
geom_bar() +
theme_minimal() +
coord_flip()
},width = "auto",height = "auto",res = 72)
}
# Run app ----
shinyApp(ui, server)
'''
I was hoping to see the graph in the middle of mainpanel, but all I see is a small graph with no proper margins.
Expected: (Something like this on RShiny)
Here is what I see now:
input$var1 is a string => use aes_string:
ggplot(data = a) +
aes_string(x = "Sepal.Length", fill = sw) + ......

Graph is not generating properly in shiny only a single bar is shown in graph

I have imported the dataset = students for generating the reactive plots but proper plot is not generated .I am using ggplot for plots so could you
please tell me whats wrong in my code.
library(shiny)
library(ggplot2)
ui <- navbarPage("A SHINY APP!! ",
tabPanel("Plots",headerPanel("Different plots of data"),
sidebarLayout(
sidebarPanel(
selectInput("x.col","x.variable",choices=names(students))
),
mainPanel(plotOutput("histPlot")))
)
)
server <- function(input, output) {
plot <- reactive({ ggplot(students,aes(x=input$x.col))
})
output$histPlot <- renderPlot({
plot() + geom_histogram(stat = "count",bins = 30)
})
}
shinyApp(ui = ui, server = server
Try with get() function like the following:
ggplot(students, aes(x = get(input$x.col)))

Unable to plot stacked barplot using R Shiny

I am new to R Shiny. Actually i have drawn Stacked Barplot using ggplot in my
R code. I want to draw the same using shiny. Below is my R code:
ggplot(data = df, aes(x = OutPut, y = Group, fill = Group)) +
geom_bar(stat = "identity") +
facet_grid(~ Environment)
In my R code it is giving correct results.But i am trying to draw in shiny. Below is my shiny R code.
ui <- fluidPage(theme = shinytheme("lumen"),
titlePanel("Data Analysis"),
selectInput("variable", "Variable:", c("OutPut", "Member", "Levels")),
mainPanel(plotOutput("plot")))
# Define server function
server <- function(input, output){
x = ggplot(data = df, aes(x = variable.names(), y = Group, fill = Group)) +
geom_bar(stat = "identity") +
facet_grid(~ Environment)
plot(x)
}
# Create Shiny object
shinyApp(ui = ui, server = server)
It is throwing an error,here i have created a dropdown box where all the variables have been stored. So when i select one variable, it should plot the Stacked barplot. Could anyone please help me.
Like it was mentioned in the comments, you need to use the rendering functions and actually assign them to the output to get the outputs you need.
I believe an example of using plots in rshiny would help, since it wouldn't make sense to have it in the comments, here it is:
library(shiny)
library(ggplot2)
ui <- fluidPage(titlePanel("Fast Example with mtcars"),
# inputs
selectInput("x", "Choose x:", choices = names(mtcars), selected = 'mpg'),
selectInput("y", "Choose y:", choices = names(mtcars), selected = 'hp'),
selectInput("fill", "Choose fill:", choices = names(mtcars), selected = 'carb'),
mainPanel(
#outputs
h2("Chosen variables are:"),
h4(textOutput("vars")),
plotOutput("plot")))
server <- function(input, output) {
df <- mtcars
# here's how you would use the rendering functions
# notice that I used aes_string
output$plot <- renderPlot({
ggplot(data=df,
aes_string(x= input$x, y= input$y, fill=input$fill)) +
geom_point()
})
output$vars <- renderText(paste0('x: ', input$x, " , ",
'y: ', input$y, " , ",
'fill: ', input$fill))
}
shinyApp(ui = ui, server = server)
The Rshiny tutorial is pretty helpful, you can take a look at it here https://shiny.rstudio.com/tutorial/

How can I unpick the renderPlot() function in shinyr with ggplot

Below is functioning code for a basic shiny app that allows the user to pick a column and then plots a ggplot::histogram() of the selected column:
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
titlePanel("ggplot"),
sidebarLayout(
sidebarPanel(
uiOutput("column_select")
),
mainPanel(plotOutput("plot"))
)
)
# Define server logic required to draw a histogram
server <- function(input, output){
dat <- reactive({iris})
output$column_select <- renderUI({selectInput("col", label = "column", choices = as.list(names(iris)), selected = "Sepal.Length")})
output$plot <- renderPlot({ggplot(dat(), aes_string(x = input$col)) +
geom_histogram()})
p <- ggplot(dat(), aes_string(x = input$col)) +
geom_histogram()
renderPlot
}
# Run the application
shinyApp(ui = ui, server = server)
I am not sure, however, why I am unable to remove the ggplot() function from within renderPlot() and still get the same result. I have tried:
p <- reactive({ggplot(dat(), aes_string(x = input$col)) +
geom_histogram()})
outputPlot <- renderPlot({p})
But this results in no plot being drawn.
I assume there is a simple fix to this, but thus far it escapes me.

Plotting pie char using ggplot2 in Shiny with input$ variables

I am building a Shiny app that helps user to create pie chart with ggplot2 on their own. Therefore, it would have a selector input for user to select variables from different dataframes. I was stuck with the input$ statements, it seemed that Shiny took the input$ with double quotes as a character, instead of a variable. Below is a simplified version of code, kindly let me know how to fix this. Thanks in advance!
library(ggplot2)
server <- function(input, output) {
output$var_selector <- renderUI({
selectInput('var_selector', 'Please select:', choices = names(mtcars))
})
output$pie_plot <- renderPlot({
ggplot(mtcars, aes(x = factor(1), fill = factor(input$var_selector))) + geom_bar(width = 1) + coord_polar(theta = "y")
})
output$text <- renderText({
print(input$var_selector)
})
}
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
h3('ggplot2 pie chart'),
uiOutput('var_selector')
),
mainPanel(
plotOutput('pie_plot')
)
)
)
shinyApp(ui = ui, server = server)

Resources