你好我的第二个星期闪亮。我正在编写一个应用程序,其中一个绘图窗口和一些绘图控制按钮需要在某个时刻出现。我使用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)
答案 0 :(得分:4)
每个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已经在反应式表达式中。
答案 1 :(得分:1)
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)