gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:18:16 25 4
gpt4 key购买 nike

我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com