r在不同绘图包之间进行快速选择

r在不同绘图包之间进行快速选择,r,plot,shiny,rcharts,R,Plot,Shiny,Rcharts,我有一个任务,我需要构建一个rShiny应用程序,允许用户选择使用哪种R绘图软件包来显示绘图 目前,我让它工作的唯一方法(半体面地)是在服务器端为每个包使用包特定的函数,并在UI端使用一系列条件面板 但是问题是,当用户第一次进入页面时,所有绘图都被初始化。第二个问题是,当用户更改一些绘图输入值,然后选择另一个包时,将显示旧绘图,直到创建新绘图 问题: 这是唯一可用的方法吗 我觉得必须有一种方法来使用被动功能来选择软件包 我觉得应该可以在ui中使用单个rShiny的htmlOutput(或类似的

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

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

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

问题:

  • 这是唯一可用的方法吗
  • 我觉得必须有一种方法来使用被动功能来选择软件包
  • 我觉得应该可以在ui中使用单个rShiny的htmlOutput(或类似的东西),因此不需要开关面板
我创建了一个小应用程序来演示我当前的实现以及这两个问题:

服务器.R

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)
  })
})
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
  })

}

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

在应用程序的输出部分创建一个单独的部分,其中包含一些基于输入的逻辑。比如说,

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)
库(闪亮)
图书馆(GG2)
数据(汽车)

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

基于iacobus代码的解决方案:

用户界面

服务器.R

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)
  })
})
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
  })

}
库(闪亮)
图书馆(GG2)
图书馆(艺术)
数据(汽车)

服务器Rchart绘图(如Polychart和NDV3)似乎无法与
plotOutput()配合使用。因此,我的问题是关于
htmlOutput()
。这也是我使用
开关面板
的原因。但是,当我试图从rCharts包添加
rPlot
时,没有显示绘图。相反,它是在我的RStudio中呈现的。我觉得我可能错过了一些明显的东西。