Ser*_*asa 14 r shiny googlevis
我想进行反应显示,根据输入选择器的哪个值选择显示不同数量的绘图.在mtcars数据集的情况下,假设我想让用户选择在Nr之间切割.Gears或Nr.Carcarratos为要生产的地块.
看着 unique(mtcars$gear)我们看到它有4 3 53个可能的值,同时unique(mtcars$carb)有4 1 2 3 6 86个可能的值.因此,我想在Nr. of Carburators选择时生成6个单独的图,并且在选择时仅绘制3个图Nr. of Gears.我曾经玩过,conditionalPanel但是我在选择器之间切换一次或两次之后总是会爆炸.救命?
闪亮的用户界面:
library(shiny)
library(googleVis)
shinyUI(bootstrapPage(
selectInput(inputId = "choosevar",
label = "Choose Cut Variable:",
choices = c("Nr. of Gears"="gear",
"Nr. of Carburators"="carb")),
htmlOutput('mydisplay') ##Obviously I'll want more than one of these...
# conditionalPanel(...)
))
Run Code Online (Sandbox Code Playgroud)
闪亮的服务器:
shinyServer(function(input, output) {
#Toy output example for one out of 3 unique gear values:
output$mydisplay <- renderGvis({
gvisColumnChart(
mtcars[mtcars$gear==4,], xvar='hp', yvar='mpg'
)
})
})
Run Code Online (Sandbox Code Playgroud)
灵感来自于此,您可以:
ui.R
shinyUI(pageWithSidebar(
headerPanel("Dynamic number of plots"),
sidebarPanel(
selectInput(inputId = "choosevar",
label = "Choose Cut Variable:",
choices = c("Nr. of Gears"="gear", "Nr. of Carburators"="carb"))
),
mainPanel(
# This is the dynamic UI for the plots
uiOutput("plots")
)
))
Run Code Online (Sandbox Code Playgroud)
server.R
library(googleVis)
shinyServer(function(input, output) {
#dynamically create the right number of htmlOutput
output$plots <- renderUI({
plot_output_list <- lapply(unique(mtcars[,input$choosevar]), function(i) {
plotname <- paste0("plot", i)
htmlOutput(plotname)
})
tagList(plot_output_list)
})
# Call renderPlot for each one. Plots are only actually generated when they
# are visible on the web page.
for (i in 1:max(unique(mtcars[,"gear"]),unique(mtcars[,"carb"]))) {
local({
my_i <- i
plotname <- paste0("plot", my_i)
output[[plotname]] <- renderGvis({
data <- mtcars[mtcars[,input$choosevar]==my_i,]
if(dim(data)[1]>0){
gvisColumnChart(
data, xvar='hp', yvar='mpg'
)}
else NULL
})
})
}
})
Run Code Online (Sandbox Code Playgroud)
它基本上会htmlOutput动态创建绘图,并googleVis在子集中有数据时绑定绘图.
| 归档时间: |
|
| 查看次数: |
4761 次 |
| 最近记录: |