图像的闪亮意外行为

问题描述 投票:0回答:1

我是

Shiny
应用程序的新手,但我设法构建了一个界面,允许我根据可以在 url 中找到的图像从图像 url 列表中进行选择。当我在
Run App
上执行
RStudio
时,该应用程序工作正常,但一旦我尝试从外部函数调用它,图像就不再显示(仅缩略图)。由于脚本相当长,经过一些调试后,我设法在基于闪亮项目示例的 MRE 上重新创建效果。

library(shiny)

app_ui <- fluidPage(
    
    # Application title
    titlePanel("Old Faithful Geyser Data"),
    
    # Sidebar with a slider input for number of bins and an image
    sidebarLayout(
        sidebarPanel(
            sliderInput("bins", "Number of bins:", min = 1, max = 50, value = 30),
            # Inserting the image
            img(src = "/prova/1.jpg", height = "300px", width = "100%")
        ),
        
        # Show a plot of the generated distribution
        mainPanel(
            plotOutput("distPlot")
        )
    )
)

# Define server logic required to draw a histogram
app_server <- function(input, output) {
    
    output$distPlot <- renderPlot({
        # generate bins based on input$bins from ui.R
        x    <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = input$bins + 1)
        
        # draw the histogram with the specified number of bins
        hist(x, breaks = bins, col = 'darkgray', border = 'white',
             xlab = 'Waiting time to next eruption (in mins)',
             main = 'Histogram of waiting times')
    })
}

# Run the application 
app <- shinyApp(ui = app_ui, server = app_server)

如果我有一个不同的 R 脚本 (

external.R
),我像这样调用文件
MainApp.R
中包含的应用程序,则图像会崩溃。

source("path_to_app/MainApp.R")
app

enter image description here

事实上,我注意到了一种更奇怪的行为。如果我首先

Run App
该应用程序,从外部文件获取图像时也会正确显示。如果我清理环境并尝试直接获取来源,则不会显示图像。看起来几乎就像图像被缓存在某个地方。

我的文件夹结构是

Shinyapp
   |- MainApp.R
   |- external.R
   |- www
    | |- prova
      | |- 1.jpg

r shiny rstudio shinyapps
1个回答
0
投票

这是奇怪的行为 - 尽管我认为奇怪的是图像完全显示出来。我怀疑这可能是 RStudio 的某种魔力,如果您在浏览器中打开 URL,您将看不到该图像。

无论如何,问题是您正在使用

shiny::shinyApp()
,它从
server
ui
R 对象创建应用程序。但是,您想使用
shiny::runApp()
,它指的是整个文件夹。

您需要将

mainApp.R
重命名为
app.R
,然后
external.R
的内容可以简单地是:

shiny::runApp()
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.