gpt4 book ai didi

r - 使用 react 值调整给定屏幕尺寸的绘图

转载 作者:行者123 更新时间:2023-12-04 17:15:49 24 4
gpt4 key购买 nike

我正在尝试通过采用用户 session 提供的像素比率来调整 flexdashboard 中的绘图,当我使用 renderPlot 渲染绘图时这工作正常但我正在努力为使用 renderPlotly 渲染的绘图分配动态高度/宽度

我提取用户像素比例如下

pixelratio <- reactive({
session$clientData$pixelratio
})

尝试 1

output$myplot <- renderPlotly(myplot())
plotlyOutput("myplot", height = function() {900 / pixelratio()}, width = 825)

第一次尝试给出以下错误消息:

Error : CSS units must be single element numeric or character vector

尝试 2

output$myplot <- renderPlotly(myplot())
plotlyOutput("myplot", height = 900 / pixelratio(), width = 825)

第二次尝试提交以下错误消息:

Error : Operation not allowed without an active reactive context.
* You tried to do something that can only be done from inside a reactive consumer

有没有办法获取像素比以自动缩放 plotlyOutput?

最佳答案

可以在plotl_ly函数中设置高度,在plotlyOutput中设置height = "auto"

library(shiny)
library(plotly)
library(dplyr)
library(tibble)

state <- data.frame(state.x77, state.region, state.abb) %>%
rename(Region = state.region) %>%
rownames_to_column("State")


ui <- fluidPage(
br(),
plotlyOutput("myplotly", width = 825, height = "auto")
)

server <- function(input, output, session){

pixelratio <- reactive({
session$clientData$pixelratio
})

output[["myplotly"]] <- renderPlotly({
plot_ly(
data = state,
x = ~ Income,
y = ~ Murder,
type = "scatter",
mode = "markers",
text = ~ paste(State, "<br>Income: ", Income, '<br>Murder Rate:', Murder),
height = 900 / pixelratio()
)

})

observe({
print(pixelratio())
})
}

shinyApp(ui, server)

关于r - 使用 react 值调整给定屏幕尺寸的绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68758782/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com