首页 > 解决方案 > onclick 功能无法正常运行

问题描述

显然我的代码看起来不错,我已经运行了一个这样的示例,但是这些onclick() 函数无论如何都没有执行它们的工作。有人可以帮我解决这个问题吗?

它只是一个带有 6 个按钮的简单代码。3 个按钮用于绘制散点图、直方图和条形图,而一个按钮隐藏输出 div,一个按钮显示输出 div,一个按钮更改输出 div 的背景。这些 onclick 功能不起作用:/

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

moduleTestUI <- function(id){
  ns=NS(id)

   # Application title
   titlePanel("My First Shiny Program")

   # Sidebar with a slider input for number of bins 
   sidebarLayout(
      sidebarPanel(

        tags$style(type='text/css'
        , ".btn {padding: 8px; font-size: 120%;background-color: rgb(0, 102, 204);color: white;}"
        ,"div.main{width: 750px;height: 550px;border-style: double;border-width: 10px;border-color:grey; }"
        ),



    tags$div(id=ns("div1"),
        hr(),
        actionButton("btn",id=ns("button1"),label = "Scatter Plot"),
        actionButton("btn",id=ns("button2"),label = "Histogram"),
        actionButton("btn",id=ns("button3"),label = "Bar Plot"),
        hr(),
        br(),
        actionButton("btn",id=ns("button4"),label = "Hide Output"),
        actionButton("btn",id=ns("button5"),label = "Show Output"),
        actionButton("btn",id=ns("button6"),label = "Change Color"))),


      # Show a plot of the generated distribution
      mainPanel(
      br(),br(),br(),
      tags$p(strong("  ***** MY OUTPUT PANEL ***** ")),

      tags$div(id=ns('div2'),class="main")
      )
   )
}


moduleTest <- function(input, output, session){
 #scatter plot
   onclick('button1', 
          plot(mtcars$disp, mtcars$mpg,xlab="Engine displacement",
               ylab="mpg", main="MPG vs engine displacement",
               las=1))
  #or onclick('button1',qplot(disp, mpg, data=mtcars,main="MPG vs engine displacement"))

#histogram 
  onclick('button2',hist(mtcars$disp,xlab="Engine displacement", breaks = 5))

#bar plot
  onclick('button3',barplot(mtcars$disp, main="Graph of displacement", names.arg = mtcars$mpg))

  onclick('button4',hide('div2'))
  onclick('button5',show('div2'))
  onclick('button6',setBackgroundColor("blue"))


#---------- other method 
  #scatter plot
 # observeEvent(input$button1,plot(mtcars$disp, mtcars$mpg,xlab="Engine displacement",
  # ylab="mpg", main="MPG vs engine displacement", las=1))
  #or onclick('button1',qplot(disp, mpg, data=mtcars,main="MPG vs engine displacement"))

  #histogram 
  #observeEvent(input$button2,hist(mtcars$disp,xlab="Engine displacement", breaks = 5))

  #bar plot
  #observeEvent(input$button3,barplot(mtcars$disp, main="Graph of displacement", names.arg = mtcars$mpg))

  #observeEvent(input$button4,hide("div2"))
  #observeEvent(input$button5,show("div2"))
  #observeEvent(input$button6,setBackgroundColor("blue"))


}



ui <- fluidPage(

  useShinyjs(),
  moduleTestUI('test')
)


# Define server logic required to draw a histogram
server <- function(input, output) {

}


# Run the application 
shinyApp(ui = ui, server = server)

标签: shinyonclickshiny-reactivity

解决方案


推荐阅读