我对Shiny(和R一般)相当新,并且一直在努力创建一个可以通过保留,类别,使用类型和年份对数据进行子集化的应用程序,以便创建一段时间使用的条形图和饼图,平均在三个类别之间使用。以下是我的数据的示例:
Reserve Category Users UserDays Year
S Research 31 9735 2002
S Education 71 77 2002
S Public 0 0 2002
S Research 58 21596 2003
S Education 387 509 2003
S Public 188 219 2003
V Research 43 642 2012
V Education 12 60 2012
V Public 874 2882 2012
V Research 51 458 2013
V Education 27 108 2013
V Public 1003 1932 2013
以下是我目前的代码:
library(shiny)
library(tidyverse)
library(ggplot2)
library(dplyr)
ReserveData <- read_csv("DataTotals.csv")
View(ReserveData)
ui <- fluidPage(
titlePanel("Data Analysis"),
sidebarLayout(
sidebarPanel(
selectInput("Reserve", "Reserve:", choices = c("S", "V")),
selectInput("UseStatistic", "Use Statistic:", choices = c("Users", "UserDays")),
checkboxGroupInput("Category", label = h3("Category"),
choices = list("Research" = 1, "Education" = 2, "Public" = 3),selected = 1),
hr(),
fluidRow(column(3, verbatimTextOutput("Category"))),
fluidRow(
column(4,
sliderInput("Years", label = h3("Years"), min = 2002,
max = 2017, value = c(2002, 2017))
)
),
hr(),
fluidRow(
column(4, verbatimTextOutput("value")),
column(4, verbatimTextOutput("Years")))
),
mainPanel(
tableOutput("Categories"),
plotOutput(outputId = "distPlot"),
plotOutput("distPie")
)
)
)
server <- function(input, output) {
output$Categories <- renderTable({
ReserveDATA <- filter(ReserveData, Reserve == input$Reserve)
ReserveDATA <- filter(ReserveDATA, Year >= input$Years[1] & Year <= input$Years[2])
if(is.null(input$Category))
return()
Reserve_Data <- ReserveDATA[input$Category, ]
Reserve_Data
})
output$distPlot <- renderPlot({
ReserveDATA <- filter(ReserveData, Reserve == input$Reserve)
ReserveDATA <- filter(ReserveDATA, Year >= input$Years[1] & Year <= input$Years[2])
if(is.null(input$Category))
return()
Reserve_Data <- ReserveDATA[input$Category, ]
ggplot(data = Reserve_Data, aes(x=Year, y = input$UseStatistic, fill = Category)) +
geom_bar(stat = "identity", position=position_dodge())
})
output$distPie <- renderPlot({
ReserveDATA <- filter(ReserveData, Reserve == input$Reserve)
ReserveDATA <- filter(ReserveDATA, Year >= input$Years[1] & Year <= input$Years[2])
pie(ReserveDATA$Users, labels = ReseveDATA$Category)
})
}
shinyApp(ui, server)
我无法弄清楚为什么当我尝试按类别进行分组时,我只会显示第一年而不是我所看到的所有年份。此外,当我尝试显示用户和用户日期的条形图时,我只会显示一个包含用户或用户天字样的图表(而不是实际数据),而不是显示该列中的数字。我目前没有任何东西出现在我的饼图上。
无论如何,任何帮助将不胜感激!
答案 0 :(得分:0)
在使用名称ReserveDATA
时,从过滤器到拼写错误的代码中存在很多问题。我已尝试将数据过滤部分减少为单独的reactive
。在Shiny中使用它之前检查RStudio(控制台会话)中的过滤器总是更好的做法,这使得调试更容易,并且还将确保过滤器首先工作。并尝试使用%>%
运算符进行过滤,因为tidyverse
提供了使数据管道可读且更漂亮的功能。最后,我已经取代了你
aes
内ggplot
aes_string
library(shiny)
library(tidyverse)
library(ggplot2)
library(dplyr)
ReserveData <- read_csv("DataTotals.csv")
##View(ReserveData)
ui <- fluidPage(
titlePanel("Data Analysis"),
sidebarLayout(
sidebarPanel(
selectInput("Reserve", "Reserve:", choices = c("S", "V")),
selectInput("UseStatistic", "Use Statistic:", choices = c("Users", "UserDays")),
checkboxGroupInput("Category", label = h3("Category"),
choices = list("Research" , "Education" , "Public" ),selected = 'Research'),
hr(),
fluidRow(column(3, verbatimTextOutput("Category"))),
fluidRow(
column(4,
sliderInput("Years", label = h3("Years"), min = 2002,
max = 2017, value = c(2002, 2017))
)
),
hr(),
fluidRow(
column(4, verbatimTextOutput("value")),
column(4, verbatimTextOutput("Years")))
),
mainPanel(
tableOutput("Categories"),
plotOutput(outputId = "distPlot"),
plotOutput("distPie")
)
)
)
server <- function(input, output) {
ReserveDATA <- reactive({
ReserveDATA <- ReserveData %>% filter(Reserve %in% input$Reserve) %>%
filter(Category %in% input$Category) %>%
filter(Year >= input$Years[1] & Year <= input$Years[2])
#cat(input$Years)
#ReserveDATA <- filter(ReserveData, Reserve == input$Reserve)
#ReserveDATA <- filter(ReserveDATA, Year >= input$Years[1] & Year <= input$Years[2])
if(is.null(input$Category))
return()
#Reserve_Data <- ReserveDATA[input$Category, ]
ReserveDATA
})
output$Categories <- renderTable({
ReserveDATA()
})
output$distPlot <- renderPlot({
ggplot(data = ReserveDATA(), aes_string(x='Year', y = input$UseStatistic, fill = 'Category')) +
geom_bar(stat = "identity", position=position_dodge())
})
output$distPie <- renderPlot({
pie(ReserveDATA()$Users, labels = ReserveDATA()$Category)
})
}
shinyApp(ui, server)
来获取字符串输入(直接来自你闪亮的ui)
p.MineralName?.Equals("Sand")
答案 1 :(得分:0)
第一期:
checkboxGroupInput
返回已检查选项的值,而不是其标签。因此,当您检查&#34;研究&#34;你得到的价值&#34; 1&#34;在input$Category
而不是&#34;研究&#34;正如您在尝试这样的子集时所期望的那样:Reserve_Data <- ReserveDATA[input$Category, ]
。此外,像这样的子集将无效。因此,要解决第一个问题,您可以替换
Reserve_Data <- ReserveDATA[input$Category, ]
与
Category_selected <- c( "1"="Research", "2"="Education" , "3"= "Public")[input$Category]
Reserve_Data <- filter(ReserveDATA,Category==Category_selected)
第二期:
input$UseStatistic
返回一个字符串,aes
不理解字符串作为列名,因此,ggplot只显示您的字符串。要解决这个问题,您可以替换:
ggplot(data = Reserve_Data, aes(x=Year, y = input$UseStatistic, fill = Category)) +
geom_bar(stat = "identity", position=position_dodge())
与
to.plot <- Reserve_Data %>% mutate(Year=factor(Year)) %>% rename_(UseStatistic=input$UseStatistic)
ggplot(data = to.plot , aes(x=Year, y = UseStatistic, fill = Category)) +
geom_bar(stat = "identity", position=position_dodge())
总而言之,如果您更换,您的问题就会得到解决:
output$distPlot <- renderPlot({
ReserveDATA <- filter(ReserveData, Reserve == input$Reserve)
ReserveDATA <- filter(ReserveDATA, Year >= input$Years[1] & Year <= input$Years[2])
if(is.null(input$Category))
return()
Reserve_Data <- ReserveDATA[input$Category, ]
ggplot(data = Reserve_Data, aes(x=Year, y = input$UseStatistic, fill = Category)) +
geom_bar(stat = "identity", position=position_dodge())
})
与
output$distPlot <- renderPlot({
ReserveDATA <- filter(ReserveData, Reserve == input$Reserve)
ReserveDATA <- filter(ReserveDATA, Year >= input$Years[1] & Year <= input$Years[2])
if(is.null(input$Category))
return()
Category_selected <- c( "1"="Research", "2"="Education" , "3"= "Public")[input$Category]
Reserve_Data <- filter(ReserveDATA,Category==Category_selected)
to.plot <- Reserve_Data %>% mutate(Year=factor(Year)) %>% rename_(UseStatistic=input$UseStatistic)
ggplot(data = to.plot , aes(x=Year, y = UseStatistic, fill = Category)) +
geom_bar(stat = "identity", position=position_dodge())
})
最后,有一个错字。这一行:
pie(ReserveDATA$Users, labels = ReseveDATA$Category)
应该阅读
pie(ReserveDATA$Users, labels = ReserveDATA$Category)