css - R Shiny - 拆分 ui 和服务器文件时 Logo 和 CSS 文件的路径

标签 css r path shiny shinydashboard

我有一个 Shiny 仪表板,我将其分为三个文件:main(数据和对通用仪表板的调用)、用户界面(ui.R 文件)和服务器(server 文件)。

我在仪表板标题和 CSS 文件中有一个 Logo ,因此我的代码目录中有一个 www 目录。

当我使用 ui 文件启动应用程序时,它可以工作,但是当我使用主文件启动应用程序时,它不会(精确:当我注释 CSS 代码行时,应用程序可以工作,但是 Logo 仍未显示)。

下面是我的代码的一个简化示例。

主要

# 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 的完整路径。但请注意,这不是好的做法,只要应用程序的路径发生变化,您就会有很多重命名要应用。 (相反,您可能希望从其文件夹运行该应用程序并引用数据。您所有的重命名都将在同一位置)

要修复 Logo ,您可以为其来源添加前缀:

src = "/foo/logo.png"

然后在使用 shinyApp 启动应用程序之前进行此调用:

addResourcePath("foo", "www")

它将 Shiny 地开始将文件夹 www 提供给引用 /foo

关于css - R Shiny - 拆分 ui 和服务器文件时 Logo 和 CSS 文件的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41642735/

相关文章:

R:使用连接和栅格包从 gz 文件读取 GeoTiff

r - 如何将函数仅应用于 R 中数据框中的数字字段(不包括标题)

java - 从 HTTP 请求中获取完整路径

wpf - 路径数据的高度和宽度

css - Primefaces 日历不会重新调整其在窗口旋转时的位置

javascript - 在 Js Accordion 菜单中选择子菜单时,保持所选父菜单打开

javascript - 元素尺寸正在 DOM 中更新,但更改未显示 onresize

r - 计算R中两个矩阵的所有行组合的函数

html - 文本对齐 : center and align-items: center not working horizontally

java - 路径是抽象的 : cannot be instatiated(IntelliJ Ide)