通过修改再现ebailey78 / shinyMenus示例

时间:2016-05-10 05:17:08

标签: javascript r shiny shinybs

我试图轻轻地重现ebailey78 / shinyMenus example

在此示例中,当用户右键单击绘图时,会出现三个单选按钮,并且根据用户选择的单选按钮,绘图将更改为Normal,Uniform或LogNormal。

除了radiobutton,我试图做同样的事情。我没有在右键单击时显示单选按钮,而是希望用户看到常规菜单选项,如Normal,Lognormal,Uniform(无单选按钮)。

现在这就是我所做的,当用户右键单击时,它不显示单选按钮,但显示Quick Dropdown。如何摆脱Quick Dropdown并使其成为常规Dropdown并对用户选择做出反应?任何指针或建议都非常感谢。提前谢谢。

#devtools::install_github("ebailey78/shinyMenus")

library(shiny)
library(shinyBS)
library(shinyMenus)

app <- shinyApp(
  ui = 
    fluidPage(
      smNavBar("testMenu", "shinyMB", full.width = TRUE, fixed = FALSE, 

               textInput("n", "Sample Size", value = 1000),
               smNavDropdown("Distribution",
                             smRadio("dist", "norm", "Normal", selected = TRUE),
                             smRadio("dist", "lnorm", "Lognormal"),
                             smRadio("dist", "unif", "Uniform")
               ),
               smQuickDropdown("qd2Test", parent = "navbar", "Quick Dropdown", c("Action 1", "Action 2", "Action 3"))
       ),

      smQuickDropdown("qdTest", "Quick Dropdown", c("Action 1", "Action 2", "Action 3")),
      plotOutput("testPlot"),
#       smContextMenu("context1", "testPlot", 
#                     smRadio("dist", "norm", "Normal", selected = TRUE),
#                     smRadio("dist", "lnorm", "Lognormal"),
#                     smRadio("dist", "unif", "Uniform")
#       )

        smContextMenu("context1","testPlot",

                      smQuickDropdown("dist", "Quick Dropdown", c("Normal", "Lognormal", "Uniform"))

                      )

    ),
  server = 
    function(input, output, session) {

      observeEvent(input$qdTest, ({print(input$qdTest)}))
      observeEvent(input[["Action 1"]], ({print(input[["Action 1"]])}))
      output$testPlot <- renderPlot({
        dist <- switch(input$dist,
                       norm = rnorm,
                       lnorm = rlnorm,
                       unif = runif
        )
        plot(dist(input$n))
      })
    }
)

runApp(app)

1 个答案:

答案 0 :(得分:1)

您应该使用smAction。见下面的例子

library(shiny)
library(shinyBS)
library(shinyMenus)

app <- shinyApp(
    ui = 
        fluidPage(
            smNavBar("testMenu", "shinyMB", full.width = TRUE, fixed = FALSE, 

                     textInput("n", "Sample Size", value = 1000),
                     smNavDropdown("Distribution",
                                   smRadio("dist", "norm", "Normal", selected = TRUE),
                                   smRadio("dist", "lnorm", "Lognormal"),
                                   smRadio("dist", "unif", "Uniform")
                     ),
                     smQuickDropdown("qd2Test", parent = "navbar", "Quick Dropdown", c("Action 1", "Action 2", "Action 3"))
            ),

            smQuickDropdown("qdTest", "Quick Dropdown", c("Action 1", "Action 2", "Action 3")),
            plotOutput("testPlot"),
            #       smContextMenu("context1", "testPlot", 
            #                     smRadio("dist", "norm", "Normal", selected = TRUE),
            #                     smRadio("dist", "lnorm", "Lognormal"),
            #                     smRadio("dist", "unif", "Uniform")
            #       )

            smContextMenu("context1","testPlot",

                          smAction("action1", "Normal"),
                          smAction("action2", "Lognormal"),
                          smAction("action3", "Uniform")

            )

        ),
    server = 
        function(input, output, session) {
            output$testPlot <- renderPlot({
                plot(rnorm(input$n))
            })
            observeEvent(input$action1, ({
                output$testPlot <- renderPlot({
                    plot(rnorm(input$n))
                })
            }))
            observeEvent(input$action2, ({
                output$testPlot <- renderPlot({
                    plot(rlnorm(input$n))
                })
            }))
            observeEvent(input$action3, ({
                output$testPlot <- renderPlot({
                    plot(runif(input$n))
                })
            }))

        })