【问题标题】:Shiny::renderPlot - cannot set resolution reactiveShiny::renderPlot - 无法设置分辨率反应
【发布时间】:2016-01-28 15:10:02
【问题描述】:

嗨,我的第二周闪亮。我正在编写一个应用程序,其中一个绘图窗口和一些绘图控制按钮需要在某个时候出现。我使用renderUI()renderPlot()。我可以让用户更新绘图参数renderPlot(expr={ [inside here] })

,但我不能让用户更改绘图分辨率renderPlot(res=[some reactive variable outside here],expr={ })

我已经在下面发布了测试示例,非常感谢。

library(shiny)

ui = fluidPage(
  uiOutput('thisPlot'),
  uiOutput('plotOptions')
)

server <- function(input, output) {
  #some plot options which will appear by some event
  output$plotOptions <- renderUI({
    if(TRUE) {list(
      radioButtons('plot.log', 'log axis',
                   c('none'= '' ,
                     'x'   = 'x',
                     'y'   = 'y',
                     'xy'  = 'xy'),sel=NULL),
    sliderInput('plotRes', "resolution",min=50,max=200,value=75)

    )}
  })


#render plot // plotRes does not respond
output$thisGraphicsDev <- renderPlot(res=input$plotRes,expr={
 plot(x=runif(1000),y=runif(1000),log=input$plot.log)
})

#write plot to ui
output$thisPlot <- renderUI({plotOutput('thisGraphicsDev')})

}
shinyApp(ui, server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    每个renderXX 函数的内部都是响应式的,但不是函数调用本身。您必须将其包装在另一个反应式环境中。

    代码修复:

    library(shiny)
    
    ui = fluidPage(
      uiOutput('thisPlot'),
      uiOutput('plotOptions')
    )
    
    server <- function(input, output) {
      #some plot options which will appear by some event
      output$plotOptions <- renderUI({
        if(TRUE) {list(
          radioButtons('plot.log', 'log axis',
                       c('none'= '' ,
                         'x'   = 'x',
                         'y'   = 'y',
                         'xy'  = 'xy'),sel=NULL),
        sliderInput('plotRes', "resolution",min=50,max=200,value=75)
    
        )}
      })
    
    
    # Invalidates and re-evaluates whenever plotRes changes. 
    observeEvent(input$plotRes, {
        output$thisGraphicsDev <- renderPlot(res=input$plotRes,expr={
         plot(x=runif(1000),y=runif(1000),log=input$plot.log)
        })
    })
    
    #write plot to ui
    output$thisPlot <- renderUI({plotOutput('thisGraphicsDev')})
    
    }
    shinyApp(ui, server)
    

    请注意,您只需观察 plotRes,因为 plot.log 已经在反应式表达式中。

    【讨论】:

      【解决方案2】:

      renderPlot 中的 res 参数不控制浏览器输出分辨率,来自 renderPlot 帮助文件:

      res 结果图的分辨率,以每英寸像素为单位。这个值是 传递给 png。注意这会影响PNG渲染的分辨率 在R中;它不会改变浏览器的实际 ppi

      使用此页面 http://shiny.rstudio.com/gallery/image-output.html 中的信息,您可以将绘图保存为您控制其分辨率的临时图像,然后显示该图像。

      library(shiny)
      
      ui = fluidPage(
        imageOutput('plot'),
        uiOutput('plotOptions')
      )
      
      server <- function(input, output, session) {
        #some plot options which will appear by some event
        output$plotOptions <- renderUI({
          if(TRUE) {list(
            radioButtons('plot.log', 'log axis',
                         c('none'= '' ,
                           'x'   = 'x',
                           'y'   = 'y',
                           'xy'  = 'xy'),sel=NULL),
            sliderInput('plotRes', "resolution",min=10,max=200,value=75)
      
          )}
        })
      
      
        #render plot // plotRes does not respond
      
        output$plot <- renderImage({
      
          width  <-  session$clientData$output_plot_width
          height  <-  session$clientData$output_plot_height
      
          outfile <- tempfile(fileext=".jpeg")
      
          jpeg(outfile, 
              res = input$plotRes, 
              units = "px",
              width  = width,
              height  = height,
              pointsize = 12)
          plot(x=runif(1000),y=runif(1000),log=input$plot.log, cex = 1)
          dev.off()
      
          list(src = outfile,
               height = height,
               width = width)
        }, deleteFile = TRUE)
      
      
      }
      shinyApp(ui, server)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-13
        • 2013-04-29
        • 1970-01-01
        • 2012-12-20
        • 1970-01-01
        相关资源
        最近更新 更多