Reputation: 23
As an exercice to learn how to use R Shiny, I want to build a ggplot histogram with the values to be represented, fill, color and binwidth (choosable thanks to radioButton, selectInput and numericInput) working as reactive events : I want the plot to be generated when I click on the action button. There seems to be a problem with fill, color and binwidth during the graph generation.
Thank you very much for your help !
Here is my code
library(shiny)
ui <- fluidPage(
box(
title="Histogram ggplot",
status="primary",
solidHeader = TRUE,
collapsible=TRUE,
plotOutput("plot3", height = 250)),
box(title="Bin",
radioButtons("Bin1", "Nombre de Bins", c(100,500,700),100)),
box(title="Fill",
selectInput("Fill1", "Couleur de remplissage", c("white","blue"),"white")),
box(title="Colour",
selectInput("Color1", "Couleur de la bordure", c("black","blue"),"black" )),
box(title="binwidth",
numericInput("Binwidth1", "Binwidth", 0.3,0.1, 0.5, 0.005)),
box(title='Bouton',
actionButton("go3", "Show Graph"))
)
server <- function(input, output) {
valeur_random<-eventReactive(input$go3,{
data.frame(a=rnorm(input$Bin1),b=rnorm(input$Bin1))
})
Fill2<-eventReactive(input$go3,{
input$Fill1
})
Color2<-eventReactive(input$go3,{
input$Color1
})
Binwidth2<-eventReactive(input$go3,{
input$Binwidth1
})
output$plot3 <- renderPlot({
ggplot(data=valeur_random(), aes(a)) +
geom_histogram(fill=Fill2,color=Color2,binwidth = Binwidth2)
# when I replace this line with geom_histogram(fill="blue",color="white",binwidth = 0.1) it works !
})
}
shinyApp(ui, server)
Upvotes: 0
Views: 444
Reputation: 906
Reactives are like any other function in shiny so you have to call it like a function for eg. data()
library(shiny)
library(tidyverse)
ui <- fluidPage(
plotOutput("plot3", height = 250),
radioButtons("Bin1", "Nombre de Bins", c(100,500,700),100),
selectInput("Fill1", "Couleur de remplissage", c("white","blue"),"white"),
selectInput("Color1", "Couleur de la bordure", c("black","blue"),"black" ),
numericInput("Binwidth1", "Binwidth", 0.3,0.1, 0.5, 0.005),
actionButton("go3", "Show Graph")
)
server <- function(input, output) {
valeur_random<-eventReactive(input$go3,{
data.frame(a=rnorm(input$Bin1),b=rnorm(input$Bin1))
})
Fill2<-eventReactive(input$go3,{
input$Fill1
})
Color2<-eventReactive(input$go3,{
input$Color1
})
Binwidth2<-eventReactive(input$go3,{
input$Binwidth1
})
output$plot3 <- renderPlot({
ggplot(data=valeur_random(), aes(a)) +
geom_histogram(fill=Fill2(),color=Color2(),binwidth = Binwidth2())
# when I replace this line with geom_histogram(fill="blue",color="white",binwidth = 0.1) it works !
})
}
shinyApp(ui, server)
But instead, you can call input variables directly instead of passing it in eventReactive
ggplot(data=valeur_random(), aes(a)) +
geom_histogram(fill = input$Fill1, color = input$Color1, binwidth = input$Binwidth1)
Upvotes: 1
Reputation: 388982
Try using input$variable_name
directly in geom_histogram
.
library(shiny)
library(shinydashboard)
library(ggplot2)
ui <- fluidPage(
box(
title="Histogram ggplot",
status="primary",
solidHeader = TRUE,
collapsible=TRUE,
plotOutput("plot3", height = 250)),
box(title="Bin",
radioButtons("Bin1", "Nombre de Bins", c(100,500,700),100)),
box(title="Fill",
selectInput("Fill1", "Couleur de remplissage", c("white","blue"),"white")),
box(title="Colour",
selectInput("Color1", "Couleur de la bordure", c("black","blue"),"black" )),
box(title="binwidth",
numericInput("Binwidth1", "Binwidth", 0.3,0.1, 0.5, 0.005)),
box(title='Bouton',
actionButton("go3", "Show Graph"))
)
server <- function(input, output) {
valeur_random<-eventReactive(input$go3,{
data.frame(a=rnorm(input$Bin1),b=rnorm(input$Bin1))
})
output$plot3 <- renderPlot({
ggplot(data=valeur_random(), aes(a)) +
geom_histogram(fill = input$Fill1, color = input$Color1, binwidth = input$Binwidth1)
})
}
shinyApp(ui, server)
Upvotes: 0