滑块故障中的按钮

发布于 2025-02-08 04:49:34 字数 1748 浏览 4 评论 0原文

我的滑块有问题。它不会显示为13号的划分,并且按下我按下按钮检查不起作用。我该怎么办?请帮忙。

这是我写的代码:

library(shiny)
library(readr)

# GiuseppeData <- read_csv("Classes_with_Giuseppe.csv")
GiuseppeData <- structure(list(number_of_classes = c("1_class", "2_class", "3_class",
                                                     "4_class", "5_class", "6_class", "7_class", "8_class", "9_class", "10_class",
                                                     "11_class", "12_class", "13_class", "14_class", "15_class", "16_class",
                                                     "17_class", "18_class"), 
                               length_of_classes = c(2.24, 2.37, 2.14, 2.18, 2.28,
                                                     2.3, 2.32, 2.24, 2.36, 2.38, 2.21, 2.25, 2.17, 2.17, 2, 2.1, 2.05, 2.2)),
                               row.names = c(NA, -18L),
                               class = "data.frame")

ui <- fluidPage(
  titlePanel("Hello Giuseppe!"),
  sidebarLayout(
    sidebarPanel(
      sliderInput(inputId = "rows",
                  label = "Amount of Classes",
                  min = 1,
                  max = nrow(GiuseppeData),
                  value = 7, step = 1,
                  animate = animationOptions(interval = 600, loop = TRUE)),
      actionButton(
        inputId = "check",
        label = "Check"
      )
    ),
    mainPanel(
      plotOutput(outputId = "distPie")
    )
  )
)

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

shinyApp(ui = ui, server = server)

I have a problem with my slider. It doesn't show divisions up to number 13 and the button check doesn't work when I press it. What should I do to fix it? Please, help.

Here is the code I wrote:

library(shiny)
library(readr)

# GiuseppeData <- read_csv("Classes_with_Giuseppe.csv")
GiuseppeData <- structure(list(number_of_classes = c("1_class", "2_class", "3_class",
                                                     "4_class", "5_class", "6_class", "7_class", "8_class", "9_class", "10_class",
                                                     "11_class", "12_class", "13_class", "14_class", "15_class", "16_class",
                                                     "17_class", "18_class"), 
                               length_of_classes = c(2.24, 2.37, 2.14, 2.18, 2.28,
                                                     2.3, 2.32, 2.24, 2.36, 2.38, 2.21, 2.25, 2.17, 2.17, 2, 2.1, 2.05, 2.2)),
                               row.names = c(NA, -18L),
                               class = "data.frame")

ui <- fluidPage(
  titlePanel("Hello Giuseppe!"),
  sidebarLayout(
    sidebarPanel(
      sliderInput(inputId = "rows",
                  label = "Amount of Classes",
                  min = 1,
                  max = nrow(GiuseppeData),
                  value = 7, step = 1,
                  animate = animationOptions(interval = 600, loop = TRUE)),
      actionButton(
        inputId = "check",
        label = "Check"
      )
    ),
    mainPanel(
      plotOutput(outputId = "distPie")
    )
  )
)

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

shinyApp(ui = ui, server = server)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

沙沙粒小 2025-02-15 04:49:34

要显示一个自定义按钮,我们可以使用AnimationOptions,然后将ActionButton传递到其Playbutton参数。

可以开箱即用自定义滑块tick-但是,我们可以使用htmltools :: tagquery在这里可以找到一个相关的线程。

library(shiny)
library(readr)
library(plotly)
library(htmltools)

# GiuseppeData <- read_csv("Classes_with_Giuseppe.csv")
GiuseppeData <- structure(list(number_of_classes = c("1_class", "2_class", "3_class",
                                                     "4_class", "5_class", "6_class", "7_class", "8_class", "9_class", "10_class",
                                                     "11_class", "12_class", "13_class", "14_class", "15_class", "16_class",
                                                     "17_class", "18_class"), 
                               length_of_classes = c(2.24, 2.37, 2.14, 2.18, 2.28,
                                                     2.3, 2.32, 2.24, 2.36, 2.38, 2.21, 2.25, 2.17, 2.17, 2, 2.1, 2.05, 2.2)),
                               row.names = c(NA, -18L),
                               class = "data.frame")


ui <- fluidPage(titlePanel("Hello Giuseppe!"),
                sidebarLayout(sidebarPanel(
                  {
                    customSlider <- sliderInput(
                    inputId = "rows",
                    label = "Amount of Classes",
                    min = 1,
                    max = nrow(GiuseppeData),
                    value = 7,
                    step = 1,
                    animate = animationOptions(
                      interval = 600,
                      loop = TRUE,
                      playButton = actionButton(
                        inputId = "play",
                        label = "Play",
                        style = "margin-top: 20px; margin-bottom: -15px; width: 160px;"
                      ),
                      pauseButton = actionButton(
                        inputId = "pause",
                        label = "Pause",
                        style = "margin-top: 20px; margin-bottom: -15px; width: 160px;"
                      )
                    ),
                    ticks = TRUE
                    )
                    tagQuery(customSlider)$find("input")$addAttrs("data-values" = paste0(seq_len(nrow(GiuseppeData)), collapse = ", "))$allTags()
                    }
                ),
                mainPanel(
                  plotlyOutput(outputId = "distPie", height = "60vh")
                )))

server <- function(input, output) {
  output$distPie <- renderPlotly({
    x <- GiuseppeData[1:input$rows, ]
    fig <- plot_ly(
        data = x,
        labels = ~ number_of_classes,
        values = ~ length_of_classes,
        type = 'pie',
        textposition = 'inside',
        textinfo = 'label+value+percent',
        direction ='clockwise', 
        sort = FALSE
      )
    fig <- fig %>% layout(
      title = "Pie of Classes",
      xaxis = list(
        title = "Pie of Length of Each Class (in hours)",
        showgrid = FALSE,
        zeroline = FALSE,
        showticklabels = FALSE
      ),
      yaxis = list(
        showgrid = FALSE,
        zeroline = FALSE,
        showticklabels = FALSE
      )
    )
  })
}

shinyApp(ui = ui, server = server)

ps:我一直在使用 plotly ciptly pifly 而不是基础R图。随意恢复这一步骤。

To show a custom button we can use animationOptions and pass an actionButton to its playButton argument.

Customizing the slider ticks is not possible out of the box - however, we can use htmltools::tagQuery. Here a related thread can be found.

library(shiny)
library(readr)
library(plotly)
library(htmltools)

# GiuseppeData <- read_csv("Classes_with_Giuseppe.csv")
GiuseppeData <- structure(list(number_of_classes = c("1_class", "2_class", "3_class",
                                                     "4_class", "5_class", "6_class", "7_class", "8_class", "9_class", "10_class",
                                                     "11_class", "12_class", "13_class", "14_class", "15_class", "16_class",
                                                     "17_class", "18_class"), 
                               length_of_classes = c(2.24, 2.37, 2.14, 2.18, 2.28,
                                                     2.3, 2.32, 2.24, 2.36, 2.38, 2.21, 2.25, 2.17, 2.17, 2, 2.1, 2.05, 2.2)),
                               row.names = c(NA, -18L),
                               class = "data.frame")


ui <- fluidPage(titlePanel("Hello Giuseppe!"),
                sidebarLayout(sidebarPanel(
                  {
                    customSlider <- sliderInput(
                    inputId = "rows",
                    label = "Amount of Classes",
                    min = 1,
                    max = nrow(GiuseppeData),
                    value = 7,
                    step = 1,
                    animate = animationOptions(
                      interval = 600,
                      loop = TRUE,
                      playButton = actionButton(
                        inputId = "play",
                        label = "Play",
                        style = "margin-top: 20px; margin-bottom: -15px; width: 160px;"
                      ),
                      pauseButton = actionButton(
                        inputId = "pause",
                        label = "Pause",
                        style = "margin-top: 20px; margin-bottom: -15px; width: 160px;"
                      )
                    ),
                    ticks = TRUE
                    )
                    tagQuery(customSlider)$find("input")$addAttrs("data-values" = paste0(seq_len(nrow(GiuseppeData)), collapse = ", "))$allTags()
                    }
                ),
                mainPanel(
                  plotlyOutput(outputId = "distPie", height = "60vh")
                )))

server <- function(input, output) {
  output$distPie <- renderPlotly({
    x <- GiuseppeData[1:input$rows, ]
    fig <- plot_ly(
        data = x,
        labels = ~ number_of_classes,
        values = ~ length_of_classes,
        type = 'pie',
        textposition = 'inside',
        textinfo = 'label+value+percent',
        direction ='clockwise', 
        sort = FALSE
      )
    fig <- fig %>% layout(
      title = "Pie of Classes",
      xaxis = list(
        title = "Pie of Length of Each Class (in hours)",
        showgrid = FALSE,
        zeroline = FALSE,
        showticklabels = FALSE
      ),
      yaxis = list(
        showgrid = FALSE,
        zeroline = FALSE,
        showticklabels = FALSE
      )
    )
  })
}

shinyApp(ui = ui, server = server)

result

PS: I've been using plotly pie charts instead of the base R plots. Feel free to revert this step.

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