英文:
In R package workflow how to add image to shiny app
问题
我不理解这个行为的问题:
我有一个小的Shiny应用程序,按照这里描述的方式进行了组织:
- 将所有的R代码放在R/目录中。
- 编写一个启动你的应用程序的函数(即使用你的UI和服务器调用shinyApp()的函数)。
- 在应用程序的根目录中创建一个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()
2. 使用这个代码一次,我得到了这个:
runApp()
3. 如果我现在使用myApp()
(在应用程序中使用了一次runApp()
之后),我得到了这个:
# 在我之前使用了一次runApp()之后
myApp()
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:
- Put all R code in the R/ directory.
- Write a function that starts your app (i.e. calls shinyApp() with your UI and server).
- Create a DESCRIPTION file in the root directory of your app.
Here is a minimal reproducible example:
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()
I would like to add the logo.png
image to the app:
1. Using this code, I get this:
library(devtools)
load_all()
myApp()
2. Using this code once, I get this:
runApp()
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()
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("R/www", "logo.png", package = "test2"), width = 200, style = "margin-left: 20px;"))
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 "Create a package which contains an R Shiny app".
Let's call the package "rabbit". This is my suggested file & 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't need to have all the files/folders listed above.
Now let'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
#' Run the rabbit shiny app
#'
#' @export
run_rabbit <- function() {
# You can set options for your app here, eg.
# Increase file upload size to 1GB:
# options(shiny.maxRequestSize = 1e3 * 1024^2)
app_dir <- system.file("app", package = "rabbit")
if (app_dir == "") {
stop(
"Could not find the app directory. Try re-installing `rabbit`.",
call. = FALSE
)
}
setwd(app_dir)
shiny::shinyAppDir(appDir = ".")
}
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 = "stylesheet", href = "styles.css")
Say you decide to put all images in one folder inst/app/www/img/
, then you can:
tags$img(
src = file.path('img', 'rabbit-logo.svg')
)
where rabbit-logo.svg
is your app's logo.
Run the app
devtools::load_all()
run_rabbit()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论