作为学习如何使用R Shiny的练习,我想构建一个ggplot直方图,其中包含要表示的值,填充,颜色和binwidth(由于radioButton, selectInput和numericInput可选择)作为响应事件:我希望当我点击动作按钮时生成该情节。在图形生成过程中,填充、颜色和宽度似乎有问题。
非常感谢您的帮助!
我的代码
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)
响应就像shiny中的任何其他函数一样,所以您必须像调用函数一样调用它。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)
但是,你可以直接调用输入变量而不是在eventReactive
ggplot(data=valeur_random(), aes(a)) +
geom_histogram(fill = input$Fill1, color = input$Color1, binwidth = input$Binwidth1)
尝试在geom_histogram
中直接使用input$variable_name
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)