【发布时间】: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")
)
)
)
))
最终版本将使用不同的数据集和更多图表包。提供的代码更像是一个玩具示例,大部分内容都被剥离了。
【问题讨论】: