R Shiny - 当我拆分 ui 和服务器文件时徽标和 CSS 文件的路径
R Shiny - Paths of logo and CSS file when I split ui and server files
我有一个 Shiny 仪表板,我将其分为三个文件:主文件(数据和对通用仪表板的调用)、用户界面(ui.R
文件)和服务器(server
文件)。
我在仪表板中有一个徽标 header 和一个 CSS 文件,因此我的代码目录中有一个 www
目录。
当我使用 ui
文件启动应用程序时,它可以工作,但当我使用主文件启动应用程序时,它不会(精度:当我评论 CSS 行时,应用程序工作代码但徽标仍未显示)。
下面是我的代码的简化示例。
主要
# Library and function
library(ggplot2)
library(shiny)
library(shinydashboard)
CountPlotFunction <- function(MyData)
{
MyPlot <- ggplot(data = MyData, aes(x = MyData)) +
geom_bar(stat = "count", aes(fill = MyData)) +
geom_text(stat = "count", aes(label = ..count..)) +
scale_x_discrete(drop = FALSE) +
scale_fill_discrete(drop = FALSE)
return(MyPlot)
}
# The data
var1 <- c("Russia","Canada","Australia","Australia","Russia","Australia","Canada","Germany","Australia","Canada","Canada")
var2 <- c("UnitedStates","France","SouthAfrica","SouthAfrica","UnitedStates","SouthAfrica","France","Norge","SouthAfrica","France","France")
var3 <- c("Brazil","Colombia","China","China","Brazil","China","Colombia","Belgium","China","Colombia","Colombia")
df <- data.frame(var1, var2, var3)
# Call the app
source(paste0(TheFileDirectory,"server.R"))
source(paste0(TheFileDirectory,"ui.R"))
shinyApp(ui = Interface, server = Serveur)
用户界面
# The Shiny app
Interface <- dashboardPage(skin = "black",
dashboardHeader(title = "Dashboard",
dropdownMenuOutput("messageMenu"),
tags$li(class = "dropdown",
tags$a(href = "http://google.com", tags$img(src = "Logo.png", height = "20px")))
),
dashboardSidebar(disable = TRUE),
dashboardBody(
tags$head(includeCSS("www/style.css")),
fluidPage(
selectInput(inputId = "Question",
label = "Choose the question",
choices = colnames(df),
selected = colnames(df)[1]),
mainPanel(plotOutput(outputId = "ThePlot"))
)
)
)
服务器
Serveur <- function(input, output)
{
output$ThePlot <- renderPlot({CountPlotFunction(MyData = df[input$Question])})
}
要修复 css 包含,请在调用 source
之前调用 setwd(TheFileDirectory)
或引用 css 的完整路径。但请注意,这不是好的做法,只要应用程序的路径发生变化,您就会有很多重命名要应用。
(相反,您可能想 运行 从其文件夹中引用该应用程序并引用数据。您所有的重命名都会在同一个地方)
要修复徽标,您可以为其来源添加前缀:
src = "/foo/logo.png"
然后在使用 shinyApp
启动应用程序之前进行此调用:
addResourcePath("foo", "www")
它将闪亮地开始为文件夹 www
提供参考 /foo
我有一个 Shiny 仪表板,我将其分为三个文件:主文件(数据和对通用仪表板的调用)、用户界面(ui.R
文件)和服务器(server
文件)。
我在仪表板中有一个徽标 header 和一个 CSS 文件,因此我的代码目录中有一个 www
目录。
当我使用 ui
文件启动应用程序时,它可以工作,但当我使用主文件启动应用程序时,它不会(精度:当我评论 CSS 行时,应用程序工作代码但徽标仍未显示)。
下面是我的代码的简化示例。
主要
# Library and function
library(ggplot2)
library(shiny)
library(shinydashboard)
CountPlotFunction <- function(MyData)
{
MyPlot <- ggplot(data = MyData, aes(x = MyData)) +
geom_bar(stat = "count", aes(fill = MyData)) +
geom_text(stat = "count", aes(label = ..count..)) +
scale_x_discrete(drop = FALSE) +
scale_fill_discrete(drop = FALSE)
return(MyPlot)
}
# The data
var1 <- c("Russia","Canada","Australia","Australia","Russia","Australia","Canada","Germany","Australia","Canada","Canada")
var2 <- c("UnitedStates","France","SouthAfrica","SouthAfrica","UnitedStates","SouthAfrica","France","Norge","SouthAfrica","France","France")
var3 <- c("Brazil","Colombia","China","China","Brazil","China","Colombia","Belgium","China","Colombia","Colombia")
df <- data.frame(var1, var2, var3)
# Call the app
source(paste0(TheFileDirectory,"server.R"))
source(paste0(TheFileDirectory,"ui.R"))
shinyApp(ui = Interface, server = Serveur)
用户界面
# The Shiny app
Interface <- dashboardPage(skin = "black",
dashboardHeader(title = "Dashboard",
dropdownMenuOutput("messageMenu"),
tags$li(class = "dropdown",
tags$a(href = "http://google.com", tags$img(src = "Logo.png", height = "20px")))
),
dashboardSidebar(disable = TRUE),
dashboardBody(
tags$head(includeCSS("www/style.css")),
fluidPage(
selectInput(inputId = "Question",
label = "Choose the question",
choices = colnames(df),
selected = colnames(df)[1]),
mainPanel(plotOutput(outputId = "ThePlot"))
)
)
)
服务器
Serveur <- function(input, output)
{
output$ThePlot <- renderPlot({CountPlotFunction(MyData = df[input$Question])})
}
要修复 css 包含,请在调用 source
之前调用 setwd(TheFileDirectory)
或引用 css 的完整路径。但请注意,这不是好的做法,只要应用程序的路径发生变化,您就会有很多重命名要应用。
(相反,您可能想 运行 从其文件夹中引用该应用程序并引用数据。您所有的重命名都会在同一个地方)
要修复徽标,您可以为其来源添加前缀:
src = "/foo/logo.png"
然后在使用 shinyApp
启动应用程序之前进行此调用:
addResourcePath("foo", "www")
它将闪亮地开始为文件夹 www
提供参考 /foo