首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不同绘图包间的rShiny选择

不同绘图包间的rShiny选择
EN

Stack Overflow用户
提问于 2015-04-16 21:23:27
回答 2查看 335关注 0票数 0

我有一个任务,我需要构建一个rShiny应用程序,允许用户选择使用哪种R绘图包来显示一个图形。

目前,我让它工作的唯一方法(半体面)是对服务器端的每个包使用包特定的功能,并在UI端使用一系列条件面板。

然而,问题是当用户第一次进入页面时,所有的情节都会被初始化。第二个问题是,当用户更改一些绘图输入值,然后选择另一个包时,就会显示旧的绘图,直到创建一个新的绘图。

问题:

  • 这是唯一可行的方法吗?
  • 我觉得必须有一种方法来使用反应性函数来选择包?
  • 我觉得应该可以在ui中使用单个rShiny的htmlOutput (或类似的东西),因此不需要switchPanel?

我创建了一个小应用程序来演示我当前的实现以及这两个问题:

server.R

代码语言:javascript
复制
library(shiny)
#library(devtools)
#install_github("ramnathv/rCharts")
library(rCharts)

shinyServer(function(input, output) {
  names(iris) = gsub("\\.", "", names(iris))

  #Render the Generic plot
  output$GenericPlot <- renderPlot({
    data = iris[0:input$variable,]
    plot(data$SepalLength ~ data$SepalWidth)
  })

  #Render the Polychart plot
  output$PolychartPlot <- renderChart({
    plotData <- rPlot(SepalLength ~ SepalWidth, data = iris[0:input$variable,], color = 'Species', type = 'point')
    plotData$addParams(dom = 'PolychartPlot')
    return(plotData)
  })

  #Render the NDV3 plot
  output$NDV3Plot <- renderChart({
    plotData <- nPlot(SepalLength ~ SepalWidth, data = iris[0:input$variable,], group = 'Species', type = 'scatterChart')
    plotData$addParams(dom = 'NDV3Plot')
    return(plotData)
  })
})

ui.R

代码语言:javascript
复制
library(shiny)
library(rCharts)

shinyUI(fluidPage(

  sidebarLayout(
    sidebarPanel(
      selectInput("lib", label = "Library:",
                  choices = list("Generic", "rCharts Polychart", "rCharts NDV3"),
                  selected = "Generic"
      ),

      numericInput("variable", "Observations:",
                   min = 5,
                   max = 150,
                   value = 10
      )

    ),

    mainPanel(
      conditionalPanel(
        condition = "input.lib == 'Generic'",
        h3("Generic plot"),
        plotOutput("GenericPlot")
      ),

      conditionalPanel(
        condition = "input.lib == 'rCharts Polychart'",
        h3("rCharts Polychart plot"),
        showOutput("PolychartPlot", "polycharts")
      ),

      conditionalPanel(
        condition = "input.lib == 'rCharts NDV3'",
        h3("rCharts NDV3 plot"),
        showOutput("NDV3Plot", "nvd3")
      )
    )
  )
))

最终版本将使用不同的数据集和更多的图表包。所提供的代码更像是一个玩具示例,大部分内容都被去掉了。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-04-17 09:10:25

找到了解决我问题的方法。解决方案基本上是在ui.R中使用ui.R并将plotOutput()showOutput()方法移动到server.R。

基于iacobus代码的解决方案:

ui.R

代码语言:javascript
复制
library(shiny)
library(rCharts)

shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("lib", "Library: ", choices = list("base", "ggplot", "Polychart"),
                  selected = "base")
    ),
    mainPanel(uiOutput("plot"))
  )
))

server.R

代码语言:javascript
复制
library(shiny)
library(ggplot2)
library(rCharts)
data(cars)
server <- function(input, output) {

  output$plot<- renderUI({
    if (input$lib == "base") {
      plotOutput("base") 
    } else if (input$lib == "ggplot") {
      plotOutput("ggplot")
    } else if (input$lib == "Polychart") {
      showOutput("polychart", "polycharts")
    }
  })

  output$base <- renderPlot({
    plot(cars$speed, cars$dist)
  })

  output$ggplot <- renderPlot({
    ggplot(cars, aes(x = speed, y = dist)) + geom_point()
  })

  output$polychart <- renderChart({
    p <- rPlot(speed ~ dist, data = cars, type = "point")
    p$addParams(dom = 'plot')
    p
  })

}

我遇到了困难,因为我认为plotOutput()showOutput()等方法只能在ui.R中使用,但事实并非如此。

编辑:--事实证明,这不足以使pollyCharts与其他rCharts包一起正常工作。

相反,我使用renderUI和rCharts $show内联显示图表。下面的链接对我很有帮助:https://github.com/ramnathv/rCharts/issues/373。在ui中,我使用htmlOutput

票数 1
EN

Stack Overflow用户

发布于 2015-04-16 22:55:21

使应用程序的输出部分中包含一些基于输入的逻辑的单个部分。例如,

代码语言:javascript
复制
library(shiny)
library(ggplot2)
data(cars)
server <- function(input, output) {output$plot<- renderPlot({
  if (input$lib == "base") {
    p <- plot(cars$speed, cars$dist)
  } else if (input$lib == "ggplot") {
    p <- ggplot(cars, aes(x = speed, y = dist)) + geom_point()
  }
  p
})
}



ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("lib", "Library: ", choices = list("base", "ggplot"),
                  selected = "base")
    ),
    mainPanel(plotOutput("plot"))
  )
)

shinyApp(ui = ui, server = server)

这提供了一个绘图,一旦我更改lib选项,它就会重新生成。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29686238

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档