R Shining-显示使用checkboxGroupInput选择的多个绘图
下面我有一个简化的闪亮应用程序,它允许渲染一个独特的情节。我想通过允许用户检查多个变量并显示相应的图来改进这个应用程序(在一个简单的演示中,只是堆叠在彼此的顶部) 我在UI中的R Shining-显示使用checkboxGroupInput选择的多个绘图,r,plot,shiny,R,Plot,Shiny,下面我有一个简化的闪亮应用程序,它允许渲染一个独特的情节。我想通过允许用户检查多个变量并显示相应的图来改进这个应用程序(在一个简单的演示中,只是堆叠在彼此的顶部) 我在UI中的outputPlot函数中尝试了for循环,在服务器调用中尝试了renderPlot函数之前,但没有成功。有解决这个问题的想法或线索吗 # Library and function library(ggplot2) library(shiny) CountPlotFunction <- function(MyDat
outputPlot
函数中尝试了for
循环,在服务器调用中尝试了renderPlot
函数之前,但没有成功。有解决这个问题的想法或线索吗
# Library and function
library(ggplot2)
library(shiny)
CountPlotFunction <- function(MyData)
{
MyPlot <- ggplot(data = MyData, aes(x = MyData)) +
geom_bar(stat = "count", aes(fill = MyData)) +
geom_text(stat = "count", aes(label = ..count..)) +
scale_x_discrete(drop = FALSE) +
scale_fill_discrete(drop = FALSE)
return(MyPlot)
}
# The data
var1 <- c("Russia","Canada","Australia","Australia","Russia","Australia","Canada","Germany","Australia","Canada","Canada")
var2 <- c("UnitedStates","France","SouthAfrica","SouthAfrica","UnitedStates","SouthAfrica","France","Norge","SouthAfrica","France","France")
var3 <- c("Brazil","Colombia","China","China","Brazil","China","Colombia","Belgium","China","Colombia","Colombia")
df <- data.frame(var1, var2, var3)
# The Shiny app
Interface <-
{
fluidPage(
sidebarPanel(
checkboxGroupInput(inputId = "Question",
label = "Choose the question",
choices = colnames(df),
selected = colnames(df)[1])),
mainPanel(plotOutput(outputId = "ThePlot")))
}
Serveur <- function(input, output)
{
output$ThePlot <- renderPlot({CountPlotFunction(MyData = df[input$Question])})
}
shinyApp(ui = Interface, server = Serveur)
#库和函数
图书馆(GG2)
图书馆(闪亮)
CountPlotFunction有几种方法可以实现您想要的功能。在shiny中,您可以使用renderUI。请参阅下面的代码
library(ggplot2)
library(shiny)
CountPlotFunction <- function(MyData)
{
MyPlot <- ggplot(data = MyData, aes(x = MyData)) +
geom_bar(stat = "count", aes(fill = MyData)) +
geom_text(stat = "count", aes(label = ..count..)) +
scale_x_discrete(drop = FALSE) +
scale_fill_discrete(drop = FALSE)
return(MyPlot)
}
# The data
var1 <- c("Russia","Canada","Australia","Australia","Russia","Australia","Canada","Germany","Australia","Canada","Canada")
var2 <- c("UnitedStates","France","SouthAfrica","SouthAfrica","UnitedStates","SouthAfrica","France","Norge","SouthAfrica","France","France")
var3 <- c("Brazil","Colombia","China","China","Brazil","China","Colombia","Belgium","China","Colombia","Colombia")
df <- data.frame(var1, var2, var3)
# The Shiny app
Interface <-
{
fluidPage(
sidebarPanel(
checkboxGroupInput(inputId = "Question",
label = "Choose the question",
choices = colnames(df),
selected = colnames(df)[1])),
mainPanel(
uiOutput('ui_plot')
)
)
}
Serveur <- function(input, output)
{
# gen plot containers
output$ui_plot <- renderUI({
out <- list()
if (length(input$Question)==0){return(NULL)}
for (i in 1:length(input$Question)){
out[[i]] <- plotOutput(outputId = paste0("plot",i))
}
return(out)
})
# render plots
observe({
for (i in 1:3){
local({ #because expressions are evaluated at app init
ii <- i
output[[paste0('plot',ii)]] <- renderPlot({
if ( length(input$Question) > ii-1 ){
return(CountPlotFunction(MyData = df[input$Question[[ii]]]))
}
NULL
})
})
}
})
}
shinyApp(ui = Interface, server = Serveur)
库(ggplot2)
图书馆(闪亮)
计数函数