shiny - 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)
解决方案
推荐阅读
- count - 如何计算 Cognos 中交叉表中的行数
- javascript - 如何使用 JS 匹配检查字符串中所有可能的大小写?
- angular - 如何使用 pc 网络摄像头读取 pdf417 解码
- c++ - “如何在 C/C++ 中的 URL 之间传递字符串值?”
- python - 计算多索引熊猫数据框中值出现的最快方法
- kubernetes - 尝试将文件复制/挂载到 AWX kubernetes pod 但出现错误
- c++ - 链接升压文件系统时程序不启动
- ios - 在 Xcode 中导入 SQLite 时未找到此类模块
- c# - Blazor 编译器如何生成序列号?
- sql - SQL查询将列值显示为表中的列名