我一直在尝试利用闪亮来为几种不同类别的数据创建多个可视化。昨晚我能够对数据进行子集,但我不知道如何用这些数据切换绘图类型。 我有以下数据框:
Hours<-c(2,3,4,2,1,1,3)
Project<-c("a","b","b","a","a","b","a")
cd=data.frame(Project,Hours)
library(shiny)
library(ggplot2)
library(lattice)
# Define shiny server
shinyServer(function(input, output) {
#Simple test plot
pdata=subset(cd, Project==input$proj)
plotType <- function(x, type) {
switch(type,
A = hist(x),
B = barplot(x),
C = pie(x))
}
output$testPlot <- renderPlot({
plotType(pdata, input$pType)
})
})
library(shiny)
ulist=levels(cd$Project)
names(ulist) = ulist
# Sidebar with a slider input for the number of bins
shinyUI(pageWithSidebar(
# Application title
headerPanel("Project Data"),
sidebarPanel(
#select project
selectInput("proj", "Project:",ulist)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("testPlot")
)))
我想将以下代码从jdharrison合并到ui文件中:
ui = bootstrapPage(
radioButtons("pType", "Choose plot type:",
list("A", "B", "C")),
plotOutput('plot')
以下是我试图合并的帖子的链接: create plots based on radio button selection R Shiny
答案 0 :(得分:2)
您需要使用reactive
个功能。
ui.R:
library(shiny)
ulist=levels(cd$Project)
names(ulist) = ulist
shinyUI(pageWithSidebar(
headerPanel("Project Data"),
sidebarPanel(
radioButtons("pType", "Choose plot type:",
list("A", "B", "C")),
selectInput("proj", "Project:",ulist)
),
mainPanel(
plotOutput("testPlot")
)
)
)
server.R
library(shiny)
library(ggplot2)
library(lattice)
# Define shiny server
shinyServer(function(input, output) {
pdata <- reactive({
subset(cd, Project==input$proj)
}
)
plotType <- reactive({
switch(input$pType,
A = hist,
B = barplot,
C = pie)
})
output$testPlot <- renderPlot({
plotType()(pdata()[["Hours"]])
})
})