派,r闪亮

发布于 2025-02-08 06:47:58 字数 1777 浏览 1 评论 0原文

我想让左侧的滑块更改派中显示的类数。我需要添加/更改以做什么行才能做到这一点? 这是我的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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吻泪 2025-02-15 06:47:58

为了完整。

重写renderplot部分:

output$distPie <- renderPlot({
        
    x <- GiuseppeData[1:input$bins,]$length_of_classes
        
    pie(x, col = "76776", border = "pink",
         xlab = "Pie of Length of Each Class (in hours)",
         main = "Pie of Classes")
        
})

坦率地说,我不知道您想用breaks = bins = bins hreak> breaks 不是一个pie的有效参数,但是此代码的想法是选择行1,直到sliderInputInput $ 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:

output$distPie <- renderPlot({
        
    x <- GiuseppeData[1:input$bins,]$length_of_classes
        
    pie(x, col = "76776", border = "pink",
         xlab = "Pie of Length of Each Class (in hours)",
         main = "Pie of Classes")
        
})

Frankly, I don't know what you were trying to achieve with breaks = bins as breaks is not a valid parameter for pie, but the idea of this code is that you select rows 1 until the number of rows specified by the sliderInput (input$bins). Then, you select the column length_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 use GiuseppeData[1:input$bins,"length_of_classes"] as regular data.frames will automatically convert single column tables to a vector. However, the readr package that you are using does not import files as data.frame but as a spec_tbl_df, therefore the $ operator is required

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文