我正在尝试使用2个可视化(数据表和条形图)创建一个闪亮的仪表板,允许用户同时对两个可视化应用相同的过滤器。 我尝试使用此处显示的解决方案,但我想我错过了一些东西。 “Data5”是我用来填充的数据框的名称。 任何帮助,将不胜感激。
server.R
library(shiny) library(ggplot2) library(dplyr) dt <- data5 shinyServer(function(input, output) { data <- reactive({data5 if (input$year != "All"){ data <- data[data5$year == input$year,] } if (input$month != "All"){ data <- data[data5$month == input$month,] } if (input$partner_name != "All"){ data <- data[data5$partner_name == input$partner_name,] } if (input$cube_title != "All"){ data <- data[data5$cube_title == input$cube_title,] } }) #plots output$table1 <- renderDataTable({ data }) output$plot1 <- renderPlot({ ggplot(data=subset(data5,cube_title=="Leads"), aes(x=month,y=f0_)) + geom_bar(fill="blue", stat = "identity") + ylab("Leads") + ggtitle("Leads") }) output$plot2 <- renderPlot({ ggplot(data=subset(data5,cube_title==c("CommunityProfileViews","HomeProfileViews")), aes(x=month,y=f0_)) + geom_bar(fill="blue", stat = "identity") + ylab("Profile Views") + ggtitle("Profile Views") }) })ui.R
dashboardPage( skin = "blue", dashboardHeader(title = "Basic dashboard"), dashboardSidebar( ( selectInput("year", "Year:", c("All", unique(as.character(data5$year)))) ), ( selectInput("month", "Month:", c("All", unique(as.character(data5$month)))) ), ( selectInput("partner_name", "Partner:", c("All", unique(as.character(data5$partner_name)))) ), ( selectInput("cube_title", "Metric:", c("All", unique(as.character(data5$cube_title)))) ) ), dashboardBody( tabsetPanel(id = "tabSelected", tabPanel("Charts", plotOutput("plot1"), box(plotOutput("plot2"))), tabPanel("DataTable", dataTableOutput("table1")) ) ) )I am trying to create a dashboard in shiny with 2 visualizations (datatable and bargraph) that will allow the user to apply the same filters to both visualizations simultaneously. I tried using the solution shown here but I guess I am missing something. "Data5" is the name of the data frame I am using to populate. Any help would be appreciated.
server.R
library(shiny) library(ggplot2) library(dplyr) dt <- data5 shinyServer(function(input, output) { data <- reactive({data5 if (input$year != "All"){ data <- data[data5$year == input$year,] } if (input$month != "All"){ data <- data[data5$month == input$month,] } if (input$partner_name != "All"){ data <- data[data5$partner_name == input$partner_name,] } if (input$cube_title != "All"){ data <- data[data5$cube_title == input$cube_title,] } }) #plots output$table1 <- renderDataTable({ data }) output$plot1 <- renderPlot({ ggplot(data=subset(data5,cube_title=="Leads"), aes(x=month,y=f0_)) + geom_bar(fill="blue", stat = "identity") + ylab("Leads") + ggtitle("Leads") }) output$plot2 <- renderPlot({ ggplot(data=subset(data5,cube_title==c("CommunityProfileViews","HomeProfileViews")), aes(x=month,y=f0_)) + geom_bar(fill="blue", stat = "identity") + ylab("Profile Views") + ggtitle("Profile Views") }) })ui.R
dashboardPage( skin = "blue", dashboardHeader(title = "Basic dashboard"), dashboardSidebar( ( selectInput("year", "Year:", c("All", unique(as.character(data5$year)))) ), ( selectInput("month", "Month:", c("All", unique(as.character(data5$month)))) ), ( selectInput("partner_name", "Partner:", c("All", unique(as.character(data5$partner_name)))) ), ( selectInput("cube_title", "Metric:", c("All", unique(as.character(data5$cube_title)))) ) ), dashboardBody( tabsetPanel(id = "tabSelected", tabPanel("Charts", plotOutput("plot1"), box(plotOutput("plot2"))), tabPanel("DataTable", dataTableOutput("table1")) ) ) )最满意答案
问题1:被动返回一个函数,而不是一个对象。 所以我们需要在renderDataTable和renderPlot函数中调用data()而不是data
问题2:您需要将data()放在渲染函数中。 目前,它正在调用data5 ,它不是被动的:
output$plot1 <- renderPlot({ Temp <- data() ggplot(data=subset(Temp,cube_title=="Leads"), aes(x=month,y=f0_)) + geom_bar(fill="blue", stat = "identity") + ylab("Leads") + ggtitle("Leads") })问题3:您的被动函数返回一个赋值调用。 我会这样做更像:
data <- reactive({ #The data5 that was here is not necessary temp <- data5 if (input$year != "All"){ temp <- temp[temp$year == input$year,] } if (input$month != "All"){ temp <- temp[temp$month == input$month,] } if (input$partner_name != "All"){ temp <- temp[temp$partner_name == input$partner_name,] } if (input$cube_title != "All"){ temp <- temp[temp$cube_title == input$cube_title,] } return(temp) })所以现在当我们调用data() ,我们返回过滤后的data.frame
问题4 :(虽然不是问题)我会避免使用data作为对象名称。 这意味着别的东西,在阅读代码时经常会让人感到困惑。 使用更安全的DT或其他东西。 您始终可以通过在控制台中键入exists("NameToCheck")来检查名称是否正在使用中。
建议阅读
Issue 1: reactive returns a function, not an object. So we need to call data(), not data, in your renderDataTable and renderPlot functions
Issue 2: you need to put data() in your render functions. Currently, it is calling data5, which is not reactive:
output$plot1 <- renderPlot({ Temp <- data() ggplot(data=subset(Temp,cube_title=="Leads"), aes(x=month,y=f0_)) + geom_bar(fill="blue", stat = "identity") + ylab("Leads") + ggtitle("Leads") })Issue 3: Your reactive function returns an assignment call. I would do this more like:
data <- reactive({ #The data5 that was here is not necessary temp <- data5 if (input$year != "All"){ temp <- temp[temp$year == input$year,] } if (input$month != "All"){ temp <- temp[temp$month == input$month,] } if (input$partner_name != "All"){ temp <- temp[temp$partner_name == input$partner_name,] } if (input$cube_title != "All"){ temp <- temp[temp$cube_title == input$cube_title,] } return(temp) })so that now when we call data(), we return our filtered data.frame
Issue 4: (albeit less of an issue) I would avoid using data as an object name. It means something else and can often get confusing when reading code. Use the safer DT or something else. You can always check if a name is in use by typing exists("NameToCheck") in the console.
Suggested Reading
更多推荐
发布评论