rcharts - 对 Rcharts 使用百分比宽度

标签 rcharts

我想知道如何使用 % 而不是 px 设置 rChart 的宽度。我在源代码中注意到它默认为像素。我试图在一个 Shiny 的应用程序中使用它,并且用图表修复似乎是一个问题,因为它们不随用户界面的其余部分扩展。有办法解决这个问题吗?

最佳答案

这应该被视为一种黑客行为,因为 rCharts 或大多数库都没有内置响应行为。但是,一项更改是使用此行为定义您自己的 renderChart 函数。如果您在服务器中定义了 Shiny 的部分,这样的东西可能会起作用。

renderChart_pct <- function(expr, env = parent.frame(), quoted = FALSE) {
  func <- shiny::exprToFunction(expr, env, quoted)
  function() {
    rChart_ <- func()
    cht_style <- sprintf("<style>.rChart {width: %s; height: %s} </style>",
                         #### change these here to desired %
                         "100%", "100%")
    cht <- paste(capture.output(rChart_$print()), collapse = '\n')
    HTML(paste(c(cht_style, cht), collapse = '\n'))
  }
}

然后使用renderChart_pct而不是renderChart2。您可能会注意到结果不会真正响应。总而言之,这里是一个 dPlot 的示例。

  library(shiny)
  library(rCharts)

  renderChart_pct <- function(expr, env = parent.frame(), quoted = FALSE) {
    func <- shiny::exprToFunction(expr, env, quoted)
    function() {
      rChart_ <- func()
      cht_style <- sprintf("<style>.rChart {width: %s; height: %s} </style>",
                           "100%", "100%")
      cht <- paste(capture.output(rChart_$print()), collapse = '\n')
      HTML(paste(c(cht_style, cht), collapse = '\n'))
    }
  }

  ui = shinyUI(fluidPage(
    fluidRow(
      column(4,
        h1("Filler Header") 
      )
      ,column(8,
        div (
          showOutput("myChart", "dimple")
        )
      )
    )
  ))

  server = function(input,output){
    output$myChart <- renderChart_pct({
      d = dPlot(
        x~x
        , data = data.frame(x=1:10)
        , type = "line"
        , height = 500
        , width = NULL
      )
    })
  }

  runApp( list(ui=ui,server=server))

关于rcharts - 对 Rcharts 使用百分比宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28134392/

相关文章:

r - RStudio Shiny 中的可缩放图像映射

rCharts 轴标签与刻度线标签重叠

r - 如何使用 rCharts、dPlot 和 dimple 更改轴标题

javascript - 通过 selectInput 操作图例 - rCharts 中的 multibarChart

r - rCharts 中的自定义图例(highcharts)

r - 通过 rCharts 格式化 Highcharts 图

r - Shiny 中的 Highcharts 事件量表

R在R 3.4.2 x64上安装rCharts

javascript - 使用 highcharter 或 Rcharts 将图例添加到饼图