【发布时间】: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)
有没有办法做到这一点............?
【问题讨论】: