我想知道如何导入server.R部分的代码。我有一个带有observeEvent 的button.R 文件。当我使用 source() 函数将其导入 server.R 文件时,运行应用程序时出现错误。
app.R
ui <- dashboardPage(dashboardHeader(title = "Basic dashboard"), dashboardSidebar(sidebarMenu(menuItem("Accueil", tabName = "dashboard", icon = icon("dashboard")))),
dashboardBody(tabItems(tabItem(tabName = "dashboard",
actionButton("button", "button"),
))))
server <- function(input, output, session) {
source(file = "button.R")
}
shinyApp(ui = ui, server = server)
按钮.R
observeEvent(input$button, {
print("clicked")
})
错误
Listening on http://127.0.0.1:5919
Warning: Error in eval_tidy: object 'input' not found
1: runApp
您可以通过将
button.R
的内容包装在传递 input
的函数中并在服务器中调用它来实现此目的:
my_button <- function(input){
observeEvent(input$button, {
print("clicked")
})
}
server <- function(input, output, session) {
source(file = "button.R")
my_button(input)
}