R SHINY:运行App Local与部署时导出的PNG分辨率不同 [英] R Shiny: Exported PNG Resolution is Different when Running App Local vs. Deployed

查看:32
本文介绍了R SHINY:运行App Local与部署时导出的PNG分辨率不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在部署的闪亮应用程序中创建具有手动指定分辨率的PNG图像。此PNG图像应该保存在我的Dropbox中。由于某种原因,我的闪亮应用程序的部署版本没有考虑png函数中的res参数。

考虑以下示例:

##### Load R packages #####


library("rdrop2")
library("shiny")
library("shinythemes")


##### Define UI #####


ui <- fluidPage(theme = shinytheme("cerulean"),
                
                path_now <<- tempdir(),

                mainPanel(tags$h1("My Input"),
                      
                          textInput("some_text", "Insert Some Text", "Some Text"),
                          textOutput("some_text_txtout"),
            
                          actionButton("do", "Run"),
                ))


##### Define server function #####


server <- function(input, output) {

  observeEvent(input$do, {

    fun_some_text <- reactive({
      input$some_text
    })

    some_text <<- fun_some_text()

    outfile <- tempfile(fileext = "my_identifier.png")

    png(outfile, 
        width = 1500,
        height = 1000,
        res = 10)

    par(mar = c(0, 0, 0, 0))
    par(bg = "green")

    N <- 5000
    x <- runif(N)
    y <- runif(N)

    plot(x, y, type = "l", xlim = c(0.1, 0.9), ylim = c(0.1, 0.9))

    points(0.5, 0.5, col = "green", cex = 1700, pch = 16)

    text(0.5, 0.575, some_text, cex = 50)

    dev.off()

    token <- readRDS("droptoken.rds")

    file_path <- file.path(path_now, list.files(path_now, pattern = "my_identifier")[1])
    file_path <- gsub("\\", "/", file_path)

    drop_upload(file_path,
                path = "responses",
                dtoken = token)
  })
}


##### Create Shiny object #####


shinyApp(ui = ui, server = server)

如果我在本地运行此应用程序,则会创建以下PNG映像:

但是,当我将完全相同的应用程序部署到shinyapps.io并在线运行时,会创建以下PNG映像:

如您所见,第二个图像的分辨率要大得多,即我在png函数中指定的res = 10参数在应用程序的部署版本中没有考虑在内。

我还是个新手,所以我想我错过了一些非常基本的东西。然而,经过两天的研究,我仍然没有找到解决方案。

问题:如何在已部署的闪亮应用中指定PNG分辨率?

推荐答案

请使用以下命令检查您的系统和shinyapps.io上的RAG输出是否保持相同:

##### Load R packages #####
library("shiny")
library("shinythemes")
library("ragg")

createPNG <- function(text_input, res, type){
  outfile <- tempfile(fileext = paste0("_", gsub(" ","_", gsub(":",".", Sys.time())), "_", type, ".png"))
  
  if(type == "ragg"){
    agg_png(outfile, width = 1500, height = 1000, res = res)
  } else {
    png(outfile, 
        width = 1500,
        height = 1000,
        res = res, type = type)
  }
  
  par(mar = c(0, 0, 0, 0))
  par(bg = "green")
  
  N <- 5000
  x <- runif(N)
  y <- runif(N)
  
  plot(x, y, type = "l", xlim = c(0.1, 0.9), ylim = c(0.1, 0.9))
  points(0.5, 0.5, col = "green", cex = 1700, pch = 16)
  text(0.5, 0.575, text_input, cex = 50)
  invisible(dev.off())
  outfile
}

##### Define UI #####
ui <- fluidPage(theme = shinytheme("cerulean"),
                path_now,
                mainPanel(tags$h1("My Input"),
                          textInput("some_text", "Insert Some Text", "Some Text"),
                          verbatimTextOutput("pngPaths"),
                          numericInput("resolution", "resolution", value = 10, min = 1, max = 20),
                          actionButton("do", "Run")
                ))


##### Define server function #####
server <- function(input, output, session) {
  
  pngPaths <- reactiveVal(NULL)
  
  observeEvent(input$do, {
    cairoPath <- createPNG(input$some_text, input$resolution, "cairo")
    windowsPath <- createPNG(input$some_text, input$resolution, "windows")
    raggPath <- createPNG(input$some_text, input$resolution, "ragg")
    
    pngPaths(list(cairoPath, windowsPath, raggPath))
    
    if(Sys.info()["sysname"] == "Windows"){
      shell.exec(dirname(cairoPath))
    }
  })
  
  output$pngPaths <- renderPrint(req(pngPaths()))
}


##### Create Shiny object #####
shinyApp(ui = ui, server = server)

Here可以找到相关帖子。

这篇关于R SHINY:运行App Local与部署时导出的PNG分辨率不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆