我有一个任务,我需要构建一个rShiny应用程序,允许用户选择使用哪种R绘图包来显示一个图形。
目前,我让它工作的唯一方法(半体面)是对服务器端的每个包使用包特定的功能,并在UI端使用一系列条件面板。
然而,问题是当用户第一次进入页面时,所有的情节都会被初始化。第二个问题是,当用户更改一些绘图输入值,然后选择另一个包时,就会显示旧的绘图,直到创建一个新的绘图。
问题:
我创建了一个小应用程序来演示我当前的实现以及这两个问题:
server.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")
)
)
)
))最终版本将使用不同的数据集和更多的图表包。所提供的代码更像是一个玩具示例,大部分内容都被去掉了。
发布于 2015-04-17 09:10:25
找到了解决我问题的方法。解决方案基本上是在ui.R中使用ui.R并将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"))
)
))server.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。在ui中,我使用htmlOutput
发布于 2015-04-16 22:55:21
使应用程序的输出部分中包含一些基于输入的逻辑的单个部分。例如,
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选项,它就会重新生成。
https://stackoverflow.com/questions/29686238
复制相似问题