派,r闪亮
我想让左侧的滑块更改派中显示的类数。我需要添加/更改以做什么行才能做到这一点? 这是我的CSV文件数据:
这是我写的代码:
library(shiny)
library(readr)
GiuseppeData <- read_csv("Classes_with_Giuseppe.csv")
#As I understand the line should be added/changed in this part
#Here I Define UI for application that draws a pie
ui <- fluidPage(
# App title ----
titlePanel("Hello Giuseppe!"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Slider for the number of bins ----
sliderInput(inputId = "bins",
label = "Amount of Classes",
min = 1,
max = 18,
value = 7)
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Histogram ----
plotOutput(outputId = "distPie")
)
)
)
# Define server logic required to draw a pie
server <- function(input, output) {
output$distPie <- renderPlot({
x <- GiuseppeData$length_of_classes
bins <- seq(min(x), max(x), length.out = input$bins + 1)
pie(x, breaks = bins, col = "76776", border = "pink",
xlab = "Pie of Length of Each Class (in hours)",
main = "Pie of Classes")
})
}
# Run the application
shinyApp(ui = ui, server = server)
I want to make the slider on the left change the number of classes shown in the pie. What line do I need to add/change to make it do that?
Here is my CSV file data:
Here is the code I wrote:
library(shiny)
library(readr)
GiuseppeData <- read_csv("Classes_with_Giuseppe.csv")
#As I understand the line should be added/changed in this part
#Here I Define UI for application that draws a pie
ui <- fluidPage(
# App title ----
titlePanel("Hello Giuseppe!"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Slider for the number of bins ----
sliderInput(inputId = "bins",
label = "Amount of Classes",
min = 1,
max = 18,
value = 7)
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Histogram ----
plotOutput(outputId = "distPie")
)
)
)
# Define server logic required to draw a pie
server <- function(input, output) {
output$distPie <- renderPlot({
x <- GiuseppeData$length_of_classes
bins <- seq(min(x), max(x), length.out = input$bins + 1)
pie(x, breaks = bins, col = "76776", border = "pink",
xlab = "Pie of Length of Each Class (in hours)",
main = "Pie of Classes")
})
}
# Run the application
shinyApp(ui = ui, server = server)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了完整。
重写
renderplot
部分:坦率地说,我不知道您想用
breaks = bins = bins
hreak> breaks 不是一个pie
的有效参数,但是此代码的想法是选择行1,直到sliderInput
(Input $ bins >)。然后,您使用
$
运算符选择列length_of_classes
,该操作员将自动将单列转换为数字向量。小脚注:在常规
data.frames
中,您还可以使用giuseppedata [1:Input $ bins,“ lentha”向量。但是,您正在使用的
readr
软件包不会导入文件为data.frame
,而是作为spec_tbl_df
,因此$ < /代码>需要操作员
For sake of completeness.
Rewrite the
renderPlot
portion to this:Frankly, I don't know what you were trying to achieve with
breaks = bins
asbreaks
is not a valid parameter forpie
, but the idea of this code is that you select rows 1 until the number of rows specified by thesliderInput
(input$bins
). Then, you select the columnlength_of_classes
using the$
operator, which will automatically convert the single column to a numeric vector.Small footnote: in regular
data.frames
you can also useGiuseppeData[1:input$bins,"length_of_classes"]
as regular data.frames will automatically convert single column tables to a vector. However, thereadr
package that you are using does not import files asdata.frame
but as aspec_tbl_df
, therefore the$
operator is required