应用conditionalPanel逻辑,其中一个switchInput设置为TRUE,并且滑块具有不同的输入

时间:2018-10-30 22:38:32

标签: javascript r shiny shinyjs shiny-reactivity

我正在实施R Shiny应用程序,但是我的Javascript技能不强。

我正在努力提出conditionalPanel()中的条件逻辑。仅在id3设置为TRUE且滑块范围id1有不同选择的情况下,如何显示id2

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
  materialSwitch(inputId = "id1", label = "Do I want this on?", value = FALSE, status = "primary"),
  sliderTextInput(inputId = "id2", label = "Show below if the range is greater than 0", choices = 2000:2018, selected = rep(2018, 2)),
  br(), br(),
  conditionalPanel("input.id1 && input.id2[1] != input.id2[2]",
               materialSwitch(inputId = "id3", label = "To show based on conditions above.", value = FALSE, status = "primary")
  )
)

server <- function(input, output) {}

shinyApp(ui = ui, server = server)

我也玩过shinyjs软件包,但也没有成功(请尝试以下内容):

library(shiny)
library(shinyWidgets)
library(shinyjs)

ui <- fluidPage(
  useShinyjs(),
  materialSwitch(inputId = "id1", label = "Do I want this on?", value = FALSE, status = "primary"),
  sliderTextInput(inputId = "id2", label = "Show below if the range is greater than 0", choices = 2000:2018, selected = rep(2018, 2)),
  br(), br(),
  hidden(
    p(id = "element", materialSwitch(inputId = "id3", label = "To show based on conditions above.", value = FALSE, status = "primary"))
  )
)

server <- function(input, output) {
  observe({
    if (input$id1 == TRUE && (input$id2[1] != input$id2[2])) {
      show("element")
    }
  })  
}

shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:1)

我忘了Java语言与R不同,使用的是零基索引。

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
  materialSwitch(inputId = "id1", label = "Do I want this on?", value = FALSE, status = "primary"),
  sliderTextInput(inputId = "id2", label = "Show below if the range is greater than 0", choices = 2000:2018, selected = rep(2018, 2)),
  br(), br(),
  conditionalPanel("input.id1 && input.id2[0] != input.id2[1]",
           materialSwitch(inputId = "id3", label = "To show based on conditions above.", value = FALSE, status = "primary")
  )
)

server <- function(input, output) {}

shinyApp(ui = ui, server = server)