我正在努力讓 r2d3 Shiny 只渲染一個 .png 檔案。如果我在 .js 檔案中使用 URL,我能夠做到這一點,但如果我使用指向完全相同檔案的相對路徑,但在本地存盤,則不會呈現任何內容。我當然檢查了本地檔案是否存在,路徑中沒有拼寫錯誤等。
我已經構建了一個 toy R Shiny 應用程式來重現該問題。它只呈現一個 d3 圖表,它本身只是在 .png 檔案中顯示 RStudio 徽標(注釋/取消注釋 .js 檔案的最后兩行以查看問題)。為什么會這樣?如何使用r2d3獲取要顯示的本地影像?幾個小時以來,我一直在尋找解決方案,但徒勞無功。
應用程式
library(r2d3)
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Reprex"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
),
# Show a plot of the generated distribution
mainPanel(
d3Output("d3Chart", width = "100%", height = "800px") # d3output is the kind of output needed for D3; it requires renderD3 on the server side
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$d3Chart <- renderD3({
# generate bins based on input$bins from ui.R
r2d3(script = "./reprex.js", d3_version ="6")
})
}
# Run the application
shinyApp(ui = ui, server = server)
reprex.js
svg
.append("image")
.attr("class","expand")
.attr("x", 0)
.attr("y", 0)
.attr('width', 50)
.attr('height', 50)
.style("opacity",1)
.attr("xlink:href", "https://www.rstudio.com/wp-content/uploads/2018/10/RStudio-Logo.png"); // It works with that image but NOT with a local image. Why?
//.attr("xlink:href", "./RStudio-Logo.png");
我正在使用 R-4.1.2 RStudio 1.4.1717 來運行 Shiny 應用程式。
uj5u.com熱心網友回復:
多虧了 Geovany,這才解決了。我在 ui 函式之前添加了 app.R ,addResourcePath("images", ".")以便我可以使用標簽訪問我的圖片存盤在的作業目錄(這里是 app.R 所在的位置)images。通過將reprex.js 的最后一行替換為.attr("xlink:href", "images/RStudio-Logo.png");(并讓前一行注釋),圖片就顯示出來了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/386173.html
標籤:javascript r d3.js 闪亮的 r2d3
上一篇:帶軸的D3JS樹
