[英]Shiny R histogram not changing with slider
I am trying to create an histogram using shiny on my dataset .我正在尝试在我的数据集上使用 shiny 创建直方图。 I would like to be able to use a slider on it in order to change the number of histograms.
我希望能够在其上使用 slider 以更改直方图的数量。 My dataset has a column (cocoa percentage) with values in [50, 100].
我的数据集有一列(可可百分比),其值在 [50, 100] 中。 After making it numeric instead of categorical, I would use the histogram to see the frequence of 50, 51, ... or the frequence of 50-55, 56-60, ... I wrote this code, but nothing happens when I run it.
在将其设为数字而不是分类后,我将使用直方图查看 50、51、... 的频率或 50-55、56-60、...的频率 我写了这段代码,但是当我运行。 What am I doing wrong?
我究竟做错了什么? Can you help me make it work please?
你能帮我让它工作吗?
library(shiny)
library(shinythemes)
library(ggplot2)
library(plotly)
library(leaflet)
library(DT)
ui <- fluidPage(
titlePanel("Chocolate Bar Ratings"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 1)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
server <- function(input, output) {
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(chocolate$CocoaPerc, xlab="Percentuale di cacao", main="Frequenza della percentuale
di cacao", col = c("chocolate", "chocolate3", "chocolate2", "chocolate4"))
})
}
# Run the application
shinyApp(ui = ui, server = server)
The reason is that input$bins
was not used in the output$distPlot
function see the # <---
comment.原因是
input$bins
未在output$distPlot
function 中使用,请参见# <---
注释。 Note also that I just invented a random data set:另请注意,我刚刚发明了一个随机数据集:
library(shiny)
chocolate <- data.frame(CocoaPerc=runif(100, min=0, max=99))
ui <- fluidPage(
titlePanel("Chocolate Bar Ratings"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 1)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
server <- function(input, output) {
output$distPlot <- renderPlot({
# draw the histogram with the specified number of bins
hist(chocolate$CocoaPerc,
nclass = input$bins, # <---
xlab="Percentuale di cacao",
main="Frequenza della percentuale di cacao",
col = c("chocolate", "chocolate3", "chocolate2", "chocolate4"))
})
}
# Run the application
shinyApp(ui = ui, server = server)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.