Hopefully just a quick question, I have added in a selectInput function into my code and linked this to the server, however whenever I change the "year" within the app the scatterplot doesn't change the plots based on the year.
Am I missing some code?
library(shiny)
library(ggplot2)
pigs <- read.csv("pigs_data.csv")
# Define UI for application
ui <- fluidPage(
# Application title
titlePanel("Pig Breeding"),
sidebarLayout(
sidebarPanel(
#Allows user to choose a year which changes the distribution of plot points
selectInput(inputId = "year",
label = "Choose a year:",
choices = c(2016, 2017, 2018),
selectize = FALSE
)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("scatterplot")
)
)
)
# Define server logic
server <- function(input, output) {
output$scatterplot <- renderPlot({
input$year
ggplot(pigs,
aes(x = sow_count, y = species, col = species)) +
geom_point() +
facet_grid(. ~year)
})
}
# Run the application
shinyApp(ui = ui, server = server)
EDIT - before trying the observeEvent solution:
Depending on what exactly you want to plot it could be becaose of facet_grid(. ~year) and not facet_grid(. ~input$year).
If facet_grid(. ~input$year) is not what you are looking for, then ...
You can try the observeEvent from the shiny package:
observeEvent(input$year, {
output$scatterplot <- renderPlot({
input$year
ggplot(pigs,
aes(x = sow_count, y = species, col = species)) +
geom_point() +
facet_grid(. ~year)
})
})
Basically whenever the object input$year changes, you render a new plot.
Your example will look like this:
library(shiny)
library(ggplot2)
pigs <- read.csv("pigs_data.csv")
# Define UI for application
ui <- fluidPage(
# Application title
titlePanel("Pig Breeding"),
sidebarLayout(
sidebarPanel(
#Allows user to choose a year which changes the distribution of plot points
selectInput(inputId = "year",
label = "Choose a year:",
choices = c(2016, 2017, 2018),
selectize = FALSE
)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("scatterplot")
)
)
)
# Define server logic
server <- function(input, output) {
observeEvent(input$year, {
output$scatterplot <- renderPlot({
input$year
ggplot(pigs,
aes(x = sow_count, y = species, col = species)) +
geom_point() +
facet_grid(. ~year)
})
})
}
# Run the application
shinyApp(ui = ui, server = server)
I hope this works for you :)
I think that you need to update your table pigs if it contains the variable year like this:
server <- function(input, output) {
output$scatterplot <- renderPlot({
input$year
ggplot(pigs %>% filter(year %in% input$year),
aes(x = sow_count, y = species, col = species)) +
geom_point() +
facet_grid(. ~year)
})
}
Hope this help.
Related
I have the following shiny app, which consists of a numeric input and as outputs two ggplot-graphics.
library(shiny)
n <- 100
dat <- data.frame(var1 = round(rnorm(n, 50, 10),0),
var2 = sample(c("A", "B"), n, replace = TRUE))
# USER INTERFACE
ui <- fluidPage(
titlePanel("My Sample App"),
sidebarLayout(
sidebarPanel(
numericInput("n", "Number of cases", value=100)
),
mainPanel(
plotOutput("boxplot"),
plotOutput("distribution")
)
)
)
# SERVER
server <- function(input, output) {
output$boxplot <- renderPlot({
ggplot(data = dat, aes(x = var2, y = var1)) + geom_boxplot() + ggtitle("Boxplot")
})
output$distribution <- renderPlot({
ggplot(data = dat, aes(var1)) + geom_histogram() + ggtitle("Histogram")
})
}
# Run the application
shinyApp(ui = ui, server = server)
I've been trying to replace n = 10 with n = input$n. However it didn't work and I am unsure, where exactly I have to define the data.frame (inside the server function?). Can someone help please?
input$n is a reactive variable that can only be used in a reactive context. You can only define a reactive context in the server function, e.g. using reactive. Have a look here for an explanation.
library(shiny)
library(ggplot2)
# USER INTERFACE
ui <- fluidPage(
titlePanel("My Sample App"),
sidebarLayout(
sidebarPanel(
numericInput("n", "Number of cases", value=100)
),
mainPanel(
plotOutput("boxplot"),
plotOutput("distribution")
)
)
)
# SERVER
server <- function(input, output) {
dat <- reactive({
data.frame(var1 = round(rnorm(input$n, 50, 10),0),
var2 = sample(c("A", "B"), input$n, replace = TRUE))
})
output$boxplot <- renderPlot({
ggplot(data = dat(), aes(x = var2, y = var1)) + geom_boxplot() + ggtitle("Boxplot")
})
output$distribution <- renderPlot({
ggplot(data = dat(), aes(var1)) + geom_histogram() + ggtitle("Histogram")
})
}
# Run the application
shinyApp(ui = ui, server = server)
Given a shiny application with a ggplot2 plot, how would you update which x & y variable are used to construct the plot based on user input?
Code:
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("xcol",
"X:",
choices = c("Sepal.Length", "Sepal.Width")
),
selectInput("ycol",
"Y:",
choices = c("Sepal.Length", "Sepal.Width")
)
),
mainPanel(plotOutput("plot"))
)
)
server <- function(input,output) {
output$plot <- renderPlot({
iris %>%
ggplot(aes(input$xcol, input$ycol)) +
geom_point()
})
}
shinyApp(ui, server)
Desired output:
Current output:
You are trying to map aesthetics with character vectors in the aes function. You need aes_string instead:
###<Omitted Library Calls and UI>
server <- function(input,output) {
output$plot <- renderPlot({
iris %>%
ggplot(aes_string(x= input$xcol, y = input$ycol)) +
geom_point()
})
}
###<Omitted shinyApp call>
Good day
I am trying to plot the means and 95% confidence intervals for my shiny webpage but I can't seem to get it right.
I would like output similar to this
I have tried two methods
Using geom_errorbar
Here I tried creating a summary table that calculates the 95% CI and then plotting from there.
My code follows
ui <- fluidPage(
titlePanel("questionnaire"),
sidebarLayout(
sidebarPanel(
selectInput("question", "Choose a question",
colnames(Data[,3:(ncol(Data)-1)]))
),
mainPanel(
plotOutput("meanCI")
)
)
)
server <- function(input, output) {
ci <- reactive({
groupwiseMean(input$question ~ Date,
data = Data,
conf = 0.95,
digits = 3)
})
output$meanCI <- renderPlot(
ggplot(ci, aes(x=Date, y=Mean)) +
geom_errorbar(aes(ymin=Trad.lower, ymax=Trad.upper), width=.1) +
geom_point()
)
}
shinyApp(ui = ui, server = server)
But it gives me this error,
data must be a data frame, or other object coercible by fortify(), not an S3 object with class reactiveExpr/reactive
Option 2 was to use plotmeans from the gplot package
ui <- fluidPage(
titlePanel("questionnaire"),
sidebarLayout(
sidebarPanel(
selectInput("question", "Choose a question",
colnames(Data[,3:(ncol(Data)-1)]))
),
mainPanel(
plotOutput("meanCI")
)
)
)
server <- function(input, output) {
output$meanCI <- renderPlot(
plotmeans(input$question~Data$Date, connect = FALSE)
)
}
shinyApp(ui = ui, server = server)
But it results is this error,
variable lengths differ (found for 'Data$Date')
Any help will be greatly appreciated!
library(shiny)
library(rcompanion)
library(ggplot2)
ui <- fluidPage(
titlePanel("questionnaire"),
sidebarLayout(
sidebarPanel(
selectInput("question", "Choose a question",
colnames(iris)[1:4])
),
mainPanel(
plotOutput("meanCI")
)
)
)
server <- function(input, output) {
ci <- reactive({
groupwiseMean(data = iris,
var = input[["question"]],
group = "Species",
conf = 0.95,
digits = 3)
})
output[["meanCI"]] <- renderPlot({
ggplot(ci(), aes(x=Species, y=Mean)) +
geom_errorbar(aes(ymin=Trad.lower, ymax=Trad.upper), width=.1) +
geom_point()
})
}
shinyApp(ui = ui, server = server)
Your main error is the missing parentheses in ggplot(ci(), ....... The other one is input$question ~ Date, which doesn't work because input$question is a character string.
I am trying to create a scatterplot based on a csv I have loaded however when I run the code I either get no plot showing or an error when I include the aes mapping: "Mapping should be created with aes() or aes_()."
Can anyone give me pointers on where I am going wrong?
Code:
library(shiny)
library(ggplot2)
ui <- (fluidPage(
titlePanel("Pig Breeds"),
sidebarLayout(
sidebarPanel(
selectInput(inputId = "x",
label = "Pig Breeds:",
choices = c("total_pigs", "female_breeding_herd",
"in_pig_sows", "in_pig_gifts", "other_sows",
"maiden_gilts", "boars_for_service", "other_pigs"),
selected = "total_pigs"),
selectInput(inputId = "y",
label = "Year by year change:",
choices = c(2016, 2017, 2018, "year_on_year_change"),
selected = 2016),
actionButton(inputId = "update", label = "update")
),
mainPanel = (
plotOutput(outputId = "scatterplot")
)
)
)
)
server <- (function(input, output) {
output$scatterplot <- renderPlot({
ggplot(data=(read.csv("eu_pigs.csv")),
aes(x = output$x, y = output$y) +
geom_point())
observeEvent(input$update, {print(as.numeric(input$update))})
}
)
}
)
shinyApp(ui, server)
As the error message says, you’re using aes incorrectly. The function takes column names, not variable references. That is, replace
aes(x = output$x, y = output$y)
by
aes(x = x, y = y)
Or, more likely you want to be able to control the plot from the inputs, so you’d want to use
aes_string(x = input$x, y = input$y)
There are also quite a few stray parentheses and braces in your code. Remove those. Furthermore, mainPanel is a function that you need to call. Your code is instead assigning something to it.
And lastly, you actually need to plot your plot. After all these things are fixed, the relevant code looks like this:
ui <- fluidPage(
titlePanel("Pig Breeds"),
sidebarLayout(
sidebarPanel(…),
mainPanel(
plotOutput(outputId = "scatterplot")
)
)
)
server <- function(input, output) {
output$scatterplot <- renderPlot({
p = ggplot(data = read.csv("eu_pigs.csv")) +
aes_string(x = input$x, y = input$y) +
geom_point()
plot(p)
observeEvent(input$update, print(as.numeric(input$update)))
})
}
If the plot object is the last thing you are executing in the renderPlot function, you can omit plot:
output$scatterplot <- renderPlot({
ggplot(data = read.csv("eu_pigs.csv")) +
aes_string(x = input$x, y = input$y) +
geom_point()
})
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/