[英]Error when trying to dynamically change a plotly plot using facets when shiny window changes/resizes
I'm trying to make the rendPlotly height more dynamic when I plot a chart with the argument facet_wrap.当我使用参数 facet_wrap 绘制图表时,我试图使 rendPlotly 高度更加动态。 I want to ensure that the facet_wrap plot doesn't go over the window in the shiny application and that the plots are equally as large as each other.
我想确保 facet_wrap 图不会越过闪亮的应用程序中的窗口,并且图彼此一样大。 I want to do this dynamically.
我想动态地做到这一点。 Is there a better way to do this?
有一个更好的方法吗?
Here is the code I'm trying to run:这是我试图运行的代码:
library(ggplot2)
library(shiny)
library(tidyverse)
library(plotly)
mtcars$cyl = sample(letters[1:5], 32, TRUE)
library(magrittr)
gg_facet_nrow <- function(p){
num_panels <- length(unique(ggplot_build(p)$data[[1]]$PANEL)) # get number of panels
num_cols <- ggplot_build(p)$layout$facet$params$ncol # get number of columns set by user
num_rows <- wrap_dims(num_panels, ncol=num_cols)[1] # determine number of rows
}
ui <- fluidPage(
navbarPage(title="title",
tabPanel("One",
column(3,
wellPanel( selectInput('name', 'NAME', c("A", "B")))),
column(9, plotOutput('plot1')))
))
server <- function(input, output) {
X <- reactive({input$name == "A"})
p1 <- reactive({
if(X()){
p1 <- ggplotly(ggplot(mtcars, aes(mpg, wt)) + facet_wrap( . ~ gear, ncol = 1 ), height = function(){he()*300})
}else{
p1 <- ggplotly(ggplot(mtcars, aes(mpg, wt)) + facet_wrap( cyl ~ gear, ncol = 1 ), height = function(){he()*300})
}
return(p1)
})
he <- reactive(gg_facet_nrow(p1()))
output$plot1 <- renderPlotly({p1()})
}
shinyApp(ui,server)
Here is the error I get:这是我得到的错误:
Error in *: non-numeric argument to binary operator
* 中的错误:二元运算符的非数字参数
I think below should work.我认为下面应该工作。 The first issue is that the gg_facet_nrow function for me doesn't work when sending it a ggplotly, so I moved the ggplotly function to output$plot1.
第一个问题是我的 gg_facet_nrow 函数在发送 ggplotly 时不起作用,所以我将 ggplotly 函数移动到 output$plot1。 Second, in the UI it had plotOutput, which I switched for plotlyOutput.
其次,在 UI 中它有 plotOutput,我将其切换为 plotlyOutput。 And lastly, the height function didn't seem to work as
height = function(){he()*300}
, so I removed the function terms to make it height = he()*300
最后,高度函数似乎不能作为
height = function(){he()*300}
,所以我删除了函数项,使其成为height = he()*300
library(ggplot2)
library(shiny)
library(tidyverse)
library(plotly)
mtcars$cyl = sample(letters[1:5], 32, TRUE)
library(magrittr)
gg_facet_nrow <- function(p){
num_panels <- length(unique(ggplot_build(p)$data[[1]]$PANEL)) # get number of panels
num_cols <- ggplot_build(p)$layout$facet$params$ncol # get number of columns set by user
num_rows <- wrap_dims(num_panels, ncol=num_cols)[1] # determine number of rows
}
ui <- fluidPage(
navbarPage(title="title",
tabPanel("One",
column(3,
wellPanel( selectInput('name', 'NAME', c("A", "B")))),
column(9, plotlyOutput('plot1'))) #Changed to plotlyOutput
))
server <- function(input, output) {
X <- reactive({input$name == "A"})
p1 <- reactive({
if(X()){
p1 <- ggplot(mtcars, aes(mpg, wt)) + facet_wrap( . ~ gear, ncol = 1 ) #Removed ggplotly wrapper and height function
}else{
p1 <- ggplot(mtcars, aes(mpg, wt)) + facet_wrap( cyl ~ gear, ncol = 1 ) #Removed ggplotly wrapper and height function
}
return(p1)
})
he <- reactive(gg_facet_nrow(p1()))
output$plot1 <- renderPlotly({ggplotly(p1(), height = he()*300)}) #removed the function term around the he*300
}
shinyApp(ui,server)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.