闪亮的 R 直方图不随滑块变化
Shiny R histogram not changing with slider
我正在尝试在 dataset 上使用 shiny 创建直方图。我希望能够在其上使用滑块来更改直方图的数量。
我的数据集有一列(cocoa 百分比),其值在 [50, 100] 中。在将其设为数字而不是分类后,我会使用直方图查看 50、51 的频率,...或 50-55、56-60 的频率,...
我写了这段代码,但是当我 运行 它时没有任何反应。我究竟做错了什么?你能帮我让它工作吗?
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)
原因是 input$bins
没有在 output$distPlot
函数中使用,参见 # <---
注释。另请注意,我刚刚发明了一个随机数据集:
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)
我正在尝试在 dataset 上使用 shiny 创建直方图。我希望能够在其上使用滑块来更改直方图的数量。 我的数据集有一列(cocoa 百分比),其值在 [50, 100] 中。在将其设为数字而不是分类后,我会使用直方图查看 50、51 的频率,...或 50-55、56-60 的频率,... 我写了这段代码,但是当我 运行 它时没有任何反应。我究竟做错了什么?你能帮我让它工作吗?
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)
原因是 input$bins
没有在 output$distPlot
函数中使用,参见 # <---
注释。另请注意,我刚刚发明了一个随机数据集:
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)