Я хотел бы заполнить много таблиц, но показать только те, которые запрашивает пользователь. Например, в приведенном ниже минимальном примере я разделил mtcars на три таблицы и поставил три флажка. Я хотел бы отображать каждую таблицу, только если установлен соответствующий флажок.
Я пытался использовать условная панель, но это не работает. Приложение не реагирует на мои действия. Может быть, conditionalPanel работает только для входных данных графического интерфейса.
## Only run examples in interactive R sessions
if (interactive()) {
ui <- fluidPage(
checkboxInput("cyl4", "Show 4 cylinders", value=FALSE),
textOutput("box4"),
checkboxInput("cyl6", "Show 6 cylinders", value=FALSE),
textOutput("box6"),
checkboxInput("cyl8", "Show 8 cylinders", value=FALSE),
textOutput("box8"),
conditionalPanel(
condition = "input.cyl4 == TRUE",
tableOutput("data4")
),
conditionalPanel(
condition = "input.cyl6 == TRUE",
tableOutput("data6")
),
conditionalPanel(
condition = "input.cyl8 == TRUE",
tableOutput("data8")
)
)
server <- function(input, output) {
output$box4 <- renderText(input$cyl4)
output$box6 <- renderText(input$cyl6)
output$box8 <- renderText(input$cyl8)
output$data4 <- renderTable({mtcars[mtcars$cyl == 4, ]}, rownames = TRUE)
output$data6 <- renderTable({mtcars[mtcars$cyl == 6, ]}, rownames = TRUE)
output$data8 <- renderTable({mtcars[mtcars$cyl == 8, ]}, rownames = TRUE)
}
shinyApp(ui, server)
}
В настоящее время блестящее приложение показывает все три таблицы и не отвечает на флажки. Если установлен только флажок cyl4, я бы хотел, чтобы приложение отображало только таблицу «data4».





Условный аргумент должен быть написан на JavaScript, где TRUE пишется true. Приведенный ниже код должен работать.
library("shiny")
shinyApp(
ui = fluidPage(
checkboxInput("cyl4", "Show 4 cylinders", value=FALSE),
textOutput("box4"),
checkboxInput("cyl6", "Show 6 cylinders", value=FALSE),
textOutput("box6"),
checkboxInput("cyl8", "Show 8 cylinders", value=FALSE),
textOutput("box8"),
conditionalPanel(
condition = "input.cyl4 == true",
tableOutput("data4")
),
conditionalPanel(
condition = "input.cyl6 == true",
tableOutput("data6")
),
conditionalPanel(
condition = "input.cyl8 == true",
tableOutput("data8")
)
),
server =function(input, output) {
output$box4 <- renderText(input$cyl4)
output$box6 <- renderText(input$cyl6)
output$box8 <- renderText(input$cyl8)
output$data4 <- renderTable({mtcars[mtcars$cyl == 4, ]}, rownames = TRUE)
output$data6 <- renderTable({mtcars[mtcars$cyl == 6, ]}, rownames = TRUE)
output$data8 <- renderTable({mtcars[mtcars$cyl == 8, ]}, rownames = TRUE)
}
)
Спасибо и Hellwalker, и olorcain. В программировании вы можете быть так близко и в то же время так далеко. Я решил использовать 1 вместо true, чтобы избавить себя от головной боли в будущем.
Просто отметим, что «true» выше также можно заменить на «1». Например. условие «input.cyl8 == true» также может читаться как «input.cyl8 == 1».