Переключение между plotOutput и plotlyOutput на основе флажка - PullRequest
0 голосов
/ 01 марта 2019

Я пытаюсь создать приложение Shiny, которое загружает plotly, только если пользователь помечает флажок для интерактивных фигур.Тем не менее, то, что я пробовал до сих пор, заканчивает тем, что чертит обе цифры независимо от значения флажка:

require('plotly')
require('shiny')

ui <- fluidPage(

  tabsetPanel(
    id = 'mainTab',

    tabPanel(
      'conditionally interactive tab',

      checkboxInput(
        inputId = 'interactive', label = 'Interactive figure', value = FALSE
      ),

      conditionalPanel(
        condition = 'input.interactive == TRUE',
        plotlyOutput('interactivePlot')
      ),

      conditionalPanel(
        condition = 'input.interactive == FALSE',
        plotOutput('staticPlot')
      )
    ),
    tabPanel('unrelated tab')
  )
)

server <- function(input, output, session){

  output$interactivePlot <- renderPlotly({
    plot_ly(iris, x = ~Petal.Length, y = ~Sepal.Length)
  })

  output$staticPlot <- renderPlot({
    plot(Sepal.Length ~ Petal.Length, iris)
  })
}

shinyApp(ui = ui, server = server)

Причины этого - более длительное время загрузки при использовании плотной печати и неудобство плотной печати на портативном устройстве.устройства (пытаться прокрутить сюжет, реагирующий на прикосновения, сложно).Я бы предпочел не иметь для них отдельных вкладок, но я понимаю, что если ничего не работает, это может быть вариантом.

1 Ответ

0 голосов
/ 01 марта 2019

Ты очень близко.Выражение внутри condition в conditionalPanel не является ни выражением JavaScript, ни выражением R.В JavaScript они используют true / false вместо TRUE / FALSE.Так что просто измените это, и это сработает.

require('plotly')
require('shiny')

ui <- fluidPage(

        tabsetPanel(
                id = 'mainTab',

                tabPanel(
                        'conditionally interactive tab',

                        checkboxInput(
                                inputId = 'interactive', label = 'Interactive figure', value = FALSE
                        ),

                        conditionalPanel(
                                condition = 'input.interactive == true',
                                plotlyOutput('interactivePlot')
                        ),

                        conditionalPanel(
                                condition = 'input.interactive == false',
                                plotOutput('staticPlot')
                        )
                ),
                tabPanel('unrelated tab')
        )
)

server <- function(input, output, session){

        output$interactivePlot <- renderPlotly({
                plot_ly(iris, x = ~Petal.Length, y = ~Sepal.Length)
        })

        output$staticPlot <- renderPlot({
                plot(Sepal.Length ~ Petal.Length, iris)
        })
}

shinyApp(ui = ui, server = server)
...