【问题标题】:Material Switch not working as expected in shiny材料开关在闪亮时无法按预期工作
【发布时间】:2021-06-08 13:04:33
【问题描述】:

我有一个带材质开关的闪亮应用程序。我已经给出了条件,如果它打开,则显示表格或显示树状图。但看起来我的代码中有一些错误。谁能帮我? 我想我已经给出了正确的条件,但不确定为什么它不起作用

library(shiny)
library(plotly)
library(shinyWidgets)
library(DT)


ui <- fluidPage(
  plotlyOutput("sd"),
  dataTableOutput("iris"),
  
  # switchInput(inputId = "tp", value = TRUE, onLabel = "Table View", offLabel = "Tree Map",width = 1500, size = 'large')
  # radioButtons(inputId = "tp",choices = c("on","off"),selected = "on")
  materialSwitch(inputId = "tp", label = "Table View", status = "danger")
)

server <- function(input, output, session) {
  
  dtd7 <- structure(
    list(
      topic = structure(
        c(9L, 8L, 4L, 7L, 2L, 6L, 1L, 3L,
          5L, 10L, 13L, 11L, 12L),
        .Label = c("Apple", "Avocado", "Banana", "Carrot", "Mango","Mushroom", "Onion", "Orange", "Pineapple", "Strawberry", "Sweet-lemon", "Watermelon", "Wildberry"),
        class = "factor"
      ),
      n = structure(
        c(4L, 3L, 9L, 11L, 12L, 2L, 1L, 6L, 10L, 5L,
          7L, 8L, 1L),
        .Label = c("23", "24", "36", "42", "43", "46", "48", "52", "56", "61", "82", "94"),
        class = "factor"
      ),
      link = structure(c("<a href = 'https://www.google.co.in/'>google</a>","<a href = 'https://www.google.co.in/'>google</a>",
                         "<a href = 'https://www.google.co.in/'>google</a>","<a href = 'https://www.google.co.in/'>google</a>",
                         "<a href = 'https://www.google.co.in/'>google</a>","<a href = 'https://www.google.co.in/'>google</a>",
                         "<a href = 'https://www.google.co.in/'>google</a>","<a href = 'https://www.google.co.in/'>google</a>",
                         "<a href = 'https://www.google.co.in/'>google</a>","<a href = 'https://www.google.co.in/'>google</a>",
                         "<a href = 'https://www.google.co.in/'>google</a>","<a href = 'https://www.google.co.in/'>google</a>",
                         "<a href = 'https://www.google.co.in/'>google</a>"))
    ),
    class = "data.frame",
    row.names = c(NA,-13L)
  )


  observe({
    if(input$tp == TRUE){
  output$sd <- renderPlotly({
    plot_ly(
      dtd7,
      labels = ~ topic,
      parents = NA,
      values = ~ n,
      type = 'treemap', source = event_data("plotly_click"),
      hovertemplate = "Ingredient: %{label}<br>Count: %{value}<extra></extra>"
    )
  })
    }
    else {
      output$iris <- renderDataTable({
        datatable(iris)
      })
    }
})
  
}

shinyApp(ui, server)

有没有办法做到这一点............?

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    Perhps,这可能会有所帮助 -

    library(shiny)
    library(plotly)
    library(shinyWidgets)
    library(DT)
    
    
    ui <- fluidPage(
      plotlyOutput("sd"),
      dataTableOutput("iris"),
      materialSwitch(inputId = "tp", label = "Table View", status = "danger")
    )
    
    server <- function(input, output, session) {
      
      dtd7 <- structure(
        list(
          topic = structure(
            c(9L, 8L, 4L, 7L, 2L, 6L, 1L, 3L,
              5L, 10L, 13L, 11L, 12L),
            .Label = c("Apple", "Avocado", "Banana", "Carrot", "Mango","Mushroom", "Onion", "Orange", "Pineapple", "Strawberry", "Sweet-lemon", "Watermelon", "Wildberry"),
            class = "factor"
          ),
          n = structure(
            c(4L, 3L, 9L, 11L, 12L, 2L, 1L, 6L, 10L, 5L,
              7L, 8L, 1L),
            .Label = c("23", "24", "36", "42", "43", "46", "48", "52", "56", "61", "82", "94"),
            class = "factor"
          ),
          link = structure(c("<a href = 'https://www.google.co.in/'>google</a>","<a href = 'https://www.google.co.in/'>google</a>",
                             "<a href = 'https://www.google.co.in/'>google</a>","<a href = 'https://www.google.co.in/'>google</a>",
                             "<a href = 'https://www.google.co.in/'>google</a>","<a href = 'https://www.google.co.in/'>google</a>",
                             "<a href = 'https://www.google.co.in/'>google</a>","<a href = 'https://www.google.co.in/'>google</a>",
                             "<a href = 'https://www.google.co.in/'>google</a>","<a href = 'https://www.google.co.in/'>google</a>",
                             "<a href = 'https://www.google.co.in/'>google</a>","<a href = 'https://www.google.co.in/'>google</a>",
                             "<a href = 'https://www.google.co.in/'>google</a>"))
        ),
        class = "data.frame",
        row.names = c(NA,-13L)
      )
      
      
      output$sd <- renderPlotly({
            if(input$tp) {
            plot_ly(
              dtd7,
              labels = ~ topic,
              parents = NA,
              values = ~ n,
              type = 'treemap', source = event_data("plotly_click"),
              hovertemplate = "Ingredient: %{label}<br>Count: %{value}<extra></extra>"
            )
            }
          })
      
        output$iris <- renderDataTable({
          if(!input$tp) {
            datatable(iris)
          }
          })
      
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-03
      • 1970-01-01
      • 2017-03-12
      • 2020-03-08
      • 2020-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多