如何在 Shiny R 包的 renderPlot() 中使“高度”参数动态化

Yos*_*Yos 4 r shiny

我试图弄清楚如何使 Shiny R 包中的 renderPlot 函数中的“height”变量从 renderPlot() 内部接收变量。这是我在 server.R 中的代码:

shinyServer(function(input, output) { 
output$contents <- renderPlot({
file<-input$file #a CSV file is uploaded

file<-with(file,
                     aggregate(file[, input$metricIn],
                     by=list(period=day,
                     dimension1=file[, input$dimension1In], 
                     dimension2=file[, input$dimension2In]),
                     FUN = input$funIn, na.rm=TRUE))
#input$dimension1In is column name from file
#input$dimension2In is column name from file

#count of facets in the plot to calculate height in renderPlot height argument
facetCount<<-as.numeric(length(unique(file[, input$dimension1In]))) * 100


plot<-reactive({      
  g<-ggplot(data = file, aes_string(x=input$dimension2In,
                                    y=input$metricIn, fill=period))
  plot<-g + geom_bar(stat="identity", position=position_dodge()) +
  facet_wrap(as.formula(paste("~", input$dimension1In)),
               scales = "free", ncol=1)
}) #end of reactive
  print(plot())
}, width=1000,
height = facetCount)

}) #end of shinyServer
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,即使我在 renderPlot 内使用 <<- 赋值,高度参数 renderPlot 也看不到facetCount 变量。

我想让高度动态化,因为如果有很多要绘制的面,我希望绘图高度能够相应调整。

wik*_*lev 5

当绘图的高度仅取决于某些输入值时,请遵循此问题的答案。主要的变化是使用uiOutput而不是plotOutput在 ui.R 文件中,因此也包含plotOutputrenderUIserver.R 文件中。

当在服务器端计算绘图的高度时(如上面的示例所示),您还需要向参数height中添加函数调用plotOutput添加函数调用。我认为这是必需的,因为 Shiny 在制作实际绘图之前使用了高度参数。下面的例子对我有用。

ui.R:

shinyUI(fluidPage(
    titlePanel("The plot height depends on a calculated value"),
    # Sidebar with a slider input for a number
    sidebarLayout(
        sidebarPanel(
            sliderInput("dimension1In",
                        "Choose a number:",
                        min = 20,
                        max = 50,
                        value = 30)
            ),
        # use uiOutput instead of plotOutput directly
        mainPanel(
            uiOutput('ui_plot')
        )
    )
))
Run Code Online (Sandbox Code Playgroud)

服务器.R:

shinyServer(function(input, output) {
    # instead of global assignment use reactive values
    values <- reactiveValues()
    # function where you do all the processing, e.g. read file, get data from it
    file_processing <- function() {
        # imagine this coefficient comes from your file
        coefficient <- 10
        return(coefficient)
    }
    # this function defines a height of your plot
    plot_height <- function() {
        # calculate values$facetCount
        values$facetCount <- as.numeric(input$dimension1In) * file_processing()
        return(values$facetCount)
    }
    # this function defines your plot, which depends on input$dimension1In
    output$contents <- renderPlot({
        plot(1:input$dimension1In)
    })
    # wrap plotOutput in renderUI
    output$ui_plot <- renderUI({
        plotOutput("contents", height = plot_height(), width = "100%")
    })

})
Run Code Online (Sandbox Code Playgroud)