我正在尝试将静态图像放入闪亮的应用程序中。www我在 Shiny 应用程序的工作目录中创建了一个名为的文件夹,并在其中放置了一个 PNG 文件。使用以下代码应该显示图像:
library(shiny)\n\nui <- fluidPage(\n tags$img(src=\'photo.png\')\n)\n \nserver <- function(input, output) {}\n\nshinyApp(ui=ui, server=server)\nRun Code Online (Sandbox Code Playgroud)\n但我有这个:
\n\n查询图片URL( http://127.0.0.1:7122/photo.png)直接显示404状态码。
无论我是通过手动运行代码、单击 RStudio 中的“运行应用程序”按钮还是通过执行文件来启动 Shiny 应用程序,结果都是相同的Rscript app.R命令行执行文件来启动 Shiny 应用程序,结果都是相同的。
这是文件夹结构:
\nlibrary(shiny)\n\nui <- fluidPage(\n tags$img(src=\'photo.png\')\n)\n \nserver <- function(input, output) {}\n\nshinyApp(ui=ui, server=server)\nRun Code Online (Sandbox Code Playgroud)\n我错过了什么吗?
\n看来这是Shiny的一个bug。
\n在解决此问题之前,有两种粗略的解决策略:
\n说服 Shinywww应该映射到一个shinyApp对象中。为此,我们需要修改shinyApp对象:
library(shiny)\n\nui <- fluidPage(\n tags$img(src = \'photo.png\')\n)\n\nserver <- function(input, output) {}\n\napp <- shinyApp(ui = ui, server = server)\napp$staticPaths <- list(\n `/` = httpuv::staticPath(\n file.path(getwd(), "www"), indexhtml = FALSE, fallthrough = TRUE\n )\n)\n\napp\nRun Code Online (Sandbox Code Playgroud)\n\n启动 Shiny 而不使用shinyApp直接使用该对象:
runApp(\'.\')或runApp(\'app.R\').其他启动应用程序的方式不起作用!值得注意的是,这还包括将脚本中的最后一行替换为runApp(shinyApp(ui, server)).