【问题标题】:rShiny Selecting between different plotting packagesr Shiny 在不同的绘图包之间进行选择
【发布时间】:2023-04-06 03:45:01
【问题描述】:

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

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

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

问题:

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

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

服务器.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)
  })
})

ui.R

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

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

【问题讨论】:

    标签: r plot shiny rcharts


    【解决方案1】:

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

    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 选项,它就会重新生成。

    【讨论】:

    • rCharts 图(例如 Polychart 和 NDV3)似乎不适用于 plotOutput()。因此,我对htmlOutput() 提出了问题。这也是我选择switchPanel 的原因。
    • 该示例有效。但是,当我尝试从 rCharts 包中添加 rPlot 时,该图未显示。相反,它在我的 RStudio 中呈现。我觉得我可能在这里遗漏了一些明显的东西。
    【解决方案2】:

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

    基于iacobus代码的解决方案:

    ui.R

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

    服务器.R

    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。在用户界面中我使用htmlOutput

    【讨论】:

      猜你喜欢
      • 2021-05-01
      • 1970-01-01
      • 2017-02-17
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-20
      相关资源
      最近更新 更多