我在R脚本中使用了shinyjs::toggle
,其中我在同一文件中具有ui
和server
函数。这样,它的工作效果非常好,但是一旦我在2个单独的文件(一个ui.R
和一个server.R
)中使用了完全相同的代码作为包的一部分,它就不再起作用。没有错误消息,该函数不会显示和隐藏我使用它的元素。
我用下面的最小示例进行了尝试,结果相同。作为独立的R脚本,它可以工作,一旦我在包中使用了相同的功能,它就不会起作用。
library(shiny)
library(shinydashboard)
library(shinyjs)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
useShinyjs(),
actionButton("button", "Click me"),
div(id = "hello", "Hello!")
)
)
server <- function(input, output) {
observeEvent(input$button, {
toggle("hello")
})
}
shinyApp(ui, server)
在包装中:
#' Shiny App UI
#'
#' @import shiny
#' @import shinydashboard
#' @import shinyjs
#'
#'
uitest <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
useShinyjs(),
actionButton("button", "Click me"),
div(id = "hello", "Hello!")
)
)
#' Shiny app server function
#'
#' @param input provided by shiny
#' @param output provided by shiny
#'
servertest <- function(input, output) {
observeEvent(input$button, {
toggle("hello")
})
}
有人可以在这里帮助我吗?可能出什么问题了]
加载程序包时,UI已经预先构建,并且useShinyjs()
脚本中包含的函数ui.R
不再被触发。在我的程序包中,我创建了一个launchApp函数,可以启动我的应用程序,并在启动该应用程序之前调用useShinyjs()
。因此,我有3个单独的R文件:ui.R
,server.R
和launchApp.R
。
launchApp <- function() {
shinyjs::useShinyjs()
shinyApp(ui, server)
}