在R包工作流中,如何将图像添加到Shiny应用程序中?

huangapple go评论74阅读模式
英文:

In R package workflow how to add image to shiny app

问题

我不理解这个行为的问题:

我有一个小的Shiny应用程序,按照这里描述的方式进行了组织:

  1. 将所有的R代码放在R/目录中。
  2. 编写一个启动你的应用程序的函数(即使用你的UI和服务器调用shinyApp()的函数)。
  3. 在应用程序的根目录中创建一个DESCRIPTION文件。

这是一个最小的可重现示例:

library(shiny)

myApp <- function(...) {
  
  ui <- fluidPage(
    titlePanel("Minimal Shiny App"),
    
    mainPanel(
      div(
        style = "display: flex; align-items: center;",
        sliderInput("slider", "Slider", min = 1, max = 10, value = 5),
        tags$img(src = "logo.png", width = 200, style = "margin-left: 20px;")
      ),
      plotOutput("plot")
    )
  )
  
  server <- function(input, output) {
    output$plot <- renderPlot({
      plot(1:input$slider, type = "l", lwd = 2)
    })
  }
  
  shinyApp(ui = ui, server = server, ...)
}

myApp()

我想将logo.png图像添加到应用程序中:

1. 使用以下代码,我得到了这个:

library(devtools)
load_all()
myApp()

在R包工作流中,如何将图像添加到Shiny应用程序中?

2. 使用这个代码一次,我得到了这个:

runApp()

在R包工作流中,如何将图像添加到Shiny应用程序中?

3. 如果我现在使用myApp()(在应用程序中使用了一次runApp()之后),我得到了这个:

# 在我之前使用了一次runApp()之后
myApp()

在R包工作流中,如何将图像添加到Shiny应用程序中?

logo.png文件位于R文件夹中的www文件夹中。

如果我不使用包的工作流程,一切都正常。但是当我使用load_all()myApp()时,无法将图像放在那里。

我已经使用了类似于tags$img(src = system.file("R/www", "logo.png", package = "test2"), width = 200, style = "margin-left: 20px;"))system.file,但行为相同。

英文:

I have problems to understand this behavior:

I have a small shiny app organized in a package workflow like described here:

  1. Put all R code in the R/ directory.
  2. Write a function that starts your app (i.e. calls shinyApp() with your UI and server).
  3. Create a DESCRIPTION file in the root directory of your app.

Here is a minimal reproducible example:

library(shiny)

myApp &lt;- function(...) {
  
  ui &lt;- fluidPage(
    titlePanel(&quot;Minimal Shiny App&quot;),
    
    mainPanel(
      div(
        style = &quot;display: flex; align-items: center;&quot;,
        sliderInput(&quot;slider&quot;, &quot;Slider&quot;, min = 1, max = 10, value = 5),
        tags$img(src = &quot;logo.png&quot;, width = 200, style = &quot;margin-left: 20px;&quot;)
      ),
      plotOutput(&quot;plot&quot;)
    )
  )
  
  server &lt;- function(input, output) {
    output$plot &lt;- renderPlot({
      plot(1:input$slider, type = &quot;l&quot;, lwd = 2)
    })
  }
  
  shinyApp(ui = ui, server = server, ...)
}

myApp()

I would like to add the logo.png image to the app:

1. Using this code, I get this:

library(devtools)
load_all()
myApp()

在R包工作流中,如何将图像添加到Shiny应用程序中?

2. Using this code once, I get this:

runApp()

在R包工作流中,如何将图像添加到Shiny应用程序中?

3. If I use now myApp() (after I have used runApp() once in the app), I get this:

# After I used runApp() once before
myApp()

在R包工作流中,如何将图像添加到Shiny应用程序中?

The logo.png file is in www folder in the R folder.

All works fine if I do not use the package workflow. But I need this workflow and for me it is not possible to get the image there when using load_all() and myApp().

I have used system.file like tags$img(src = system.file(&quot;R/www&quot;, &quot;logo.png&quot;, package = &quot;test2&quot;), width = 200, style = &quot;margin-left: 20px;&quot;)) but same behaviour

答案1

得分: 2

一个好的方法是“创建一个包含一个R Shiny应用程序的包”。

让我们将这个包命名为“rabbit”。这是我建议的文件和文件夹布局:

|- .Rbuildignore
|- .Renviron
|- .Rprofile
|- data/
|- data-raw/
|- DESCRIPTION
|- rabbit.Rproj
|- inst/
|- app/
|- global.R
|- R/
|- server.R
|- ui.R
|- www/
|- NAMESPACE
|- R/
|- README.md
|- README.Rmd
|- tests/


你不需要拥有上面列出的所有文件/文件夹。

现在让我们看看与你的问题相关的重要目录。

### `R/`

这是项目文件夹根目录中的一个目录(在上面的`NAMESPACE`后面列出)。

将所有业务逻辑放在这里。这些是可以从你的包中导出的函数。
这与应用程序无关。

它还将包含 `run_rabbit.R`,这是一个运行你的应用程序的函数。

```r
# 运行 rabbit Shiny 应用程序
# 
# @export
run_rabbit <- function() {
  # 你可以在这里为你的应用程序设置选项,例如:
  # 增加文件上传大小到 1GB:
  # options(shiny.maxRequestSize = 1e3 * 1024^2)
  
  app_dir <- system.file("app", package = "rabbit")
  if (app_dir == "") {
    stop(
      "无法找到应用程序目录。请尝试重新安装 `rabbit`。",
      call. = FALSE
    )
  }
  setwd(app_dir)
  shiny::shinyAppDir(appDir = ".")
}

inst/app/R/

这是你的闪亮模块和与应用程序相关的其他辅助函数所在的地方。

inst/app/www/

你可以在这里放置你的CSS,JS和图像文件。

要在应用程序中引用它们,你可以:

tags$link(rel = "stylesheet", href = "styles.css")

假设你决定将所有图像放在一个名为 inst/app/www/img/ 的文件夹中,那么你可以:

tags$img(
  src = file.path('img', 'rabbit-logo.svg')
)

其中 rabbit-logo.svg 是你应用程序的标志。

运行应用程序

devtools::load_all()
run_rabbit()

<details>
<summary>英文:</summary>

A good approach would be &quot;Create a package which contains an R Shiny app&quot;.

Let&#39;s call the package &quot;rabbit&quot;. This is my suggested file &amp; folder arrangement:

|- .Rbuildignore
|- .Renviron
|- .Rprofile
|- data/
|- data-raw/
|- DESCRIPTION
|- rabbit.Rproj
|- inst/
|- app/
|- global.R
|- R/
|- server.R
|- ui.R
|- www/
|- NAMESPACE
|- R/
|- README.md
|- README.Rmd
|- tests/


You don&#39;t need to have all the files/folders listed above.

Now let&#39;s look at the important directories in regards to your question.

### `R/`

This is the one at the root of the project folder (listed after `NAMESPACE` above).

Put all the business logic there. These are the functions which can be exported from your package.
These have nothing to do with the app.

It will also contain `run_rabbit.R` which is a function to run your app.

```r
#&#39; Run the rabbit shiny app
#&#39; 
#&#39; @export
run_rabbit &lt;- function() {
  # You can set options for your app here, eg.
  # Increase file upload size to 1GB:
  # options(shiny.maxRequestSize = 1e3 * 1024^2)
  
  app_dir &lt;- system.file(&quot;app&quot;, package = &quot;rabbit&quot;)
  if (app_dir == &quot;&quot;) {
    stop(
      &quot;Could not find the app directory. Try re-installing `rabbit`.&quot;,
      call. = FALSE
    )
  }
  setwd(app_dir)
  shiny::shinyAppDir(appDir = &quot;.&quot;)
}

inst/app/R/

This is where your shiny modules and other helper functions related to
the app will reside.

inst/app/www/

You can put your CSS, JS and image files here.

To reference these in the app, you can:

tags$link(rel = &quot;stylesheet&quot;, href = &quot;styles.css&quot;)

Say you decide to put all images in one folder inst/app/www/img/, then you can:

tags$img(
  src = file.path(&#39;img&#39;, &#39;rabbit-logo.svg&#39;)
)

where rabbit-logo.svg is your app's logo.

Run the app

devtools::load_all()
run_rabbit()

huangapple
  • 本文由 发表于 2023年7月7日 02:36:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631670.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定