Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 闪亮:使用不同的变量创建反应式过滤器。_R_Filter_Shiny_Dplyr - Fatal编程技术网

R 闪亮:使用不同的变量创建反应式过滤器。

R 闪亮:使用不同的变量创建反应式过滤器。,r,filter,shiny,dplyr,R,Filter,Shiny,Dplyr,我有一个数据框架,它将社会人口统计数据与多个网站的意识测量结合起来。每个网站都有一个单独的栏目,说明该人是否知道该网站是/否。此外,每个受访者都应根据其提供的可变权重人数进行加权 我想创建一个闪亮的应用程序,为知道某个选定网站的人显示情节。该网站应通过selectInput按钮进行选择 我发现了几篇关于stackoverflow的文章,其中介绍了使用dplyr+Shining的数据集过滤器。但它们都改变了变量值,而不是变量本身 我试着使用下面的代码,但是没有成功,请参见下面的代码示例 [ 示例数

我有一个数据框架,它将社会人口统计数据与多个网站的意识测量结合起来。每个网站都有一个单独的栏目,说明该人是否知道该网站是/否。此外,每个受访者都应根据其提供的可变权重人数进行加权

我想创建一个闪亮的应用程序,为知道某个选定网站的人显示情节。该网站应通过selectInput按钮进行选择

我发现了几篇关于stackoverflow的文章,其中介绍了使用dplyr+Shining的数据集过滤器。但它们都改变了变量值,而不是变量本身

我试着使用下面的代码,但是没有成功,请参见下面的代码示例

[

示例数据帧:

我想以互动的方式做什么:

我试过的


有没有一种方法可以改变过滤器变量而不是值?我还可以选择其他解决方案。

我认为您需要添加第二个UI,它取决于第一个UI上选择的变量。在这里,我使用renderUI在服务器中创建了它。然后,我使用所选列将数据子集为与所选变量相等的数据。希望这有帮助

library(shiny)
library(ggplot2)
library(dplyr)

ui <- fluidPage(
  selectInput(inputId = "WebsiteName", label = "Choose a Website", choices = names(df) [c(3:7)]),
  htmlOutput("variableUI"),
  plotOutput("Gender")
)


server <- function(input, output) {

  output$variableUI <- renderUI({
    selectInput(inputId = "variable", label = "Choices", choices = df[,input$WebsiteName])
  })

  dfInput <- reactive({
   ##subsetting is a bit tricky here to id the column on which to subset        
    df[ df[ , input$WebsiteName ] == input$variable, ]
  })

  output$Gender <- renderPlot({
    df1 <- dfInput()
    ggplot(df1)+
      aes(x=gender, y=popWeight/sum(popWeight))+
      stat_summary(fun.y = sum, geom = "bar")+
      scale_y_continuous("Population (%)", labels = scales::percent)
  })
}


shinyApp(ui = ui, server = server)
您可以整理数据集,以更有用的方式对其进行转换,并为自己省去一些麻烦:

整洁的数据集 服务器 更新了反应式表达式

server <- function(input, output) {

  dfInput <- reactive({
    df %>% filter(web == input$websiteName & value == "Yes")
  })

  output$Gender <- renderPlot({
    df1 <- dfInput()
    ggplot(df1) +
      aes(x = gender, y = popWeight / sum(popWeight)) +
      stat_summary(fun.y = sum, geom = "bar") +
      scale_y_continuous("Population (%)", labels = scales::percent)
  })
}
library(shiny)
library(ggplot2)
library(dplyr)

ui <- fluidPage(
  selectInput(inputId = "WebsiteName", label = "Choose a Website", choices = names(df) [c(3:7)]),
  plotOutput("Gender")
)


server <- function(input, output) {

  dfInput <- reactive({
    df %>% filter (input$WebsiteName == "Yes")
  })

  output$Gender <- renderPlot({
    df1 <- dfInput()
    ggplot(df1)+
      aes(x=gender, y=popWeight/sum(popWeight))+
      stat_summary(fun.y = sum, geom = "bar")+
      scale_y_continuous("Population (%)", labels = scales::percent)
  })
}


shinyApp(ui = ui, server = server)
library(shiny)
library(ggplot2)
library(dplyr)

ui <- fluidPage(
  selectInput(inputId = "WebsiteName", label = "Choose a Website", choices = names(df) [c(3:7)]),
  htmlOutput("variableUI"),
  plotOutput("Gender")
)


server <- function(input, output) {

  output$variableUI <- renderUI({
    selectInput(inputId = "variable", label = "Choices", choices = df[,input$WebsiteName])
  })

  dfInput <- reactive({
   ##subsetting is a bit tricky here to id the column on which to subset        
    df[ df[ , input$WebsiteName ] == input$variable, ]
  })

  output$Gender <- renderPlot({
    df1 <- dfInput()
    ggplot(df1)+
      aes(x=gender, y=popWeight/sum(popWeight))+
      stat_summary(fun.y = sum, geom = "bar")+
      scale_y_continuous("Population (%)", labels = scales::percent)
  })
}


shinyApp(ui = ui, server = server)
df<- df %>% 
    gather(web, value, -age, -gender, -popWeight)
ui <- fluidPage(
  selectInput(inputId = "websiteName", 
              label = "Choose a Website", 
              choices = unique(df$web)),
  plotOutput("Gender")
)
server <- function(input, output) {

  dfInput <- reactive({
    df %>% filter(web == input$websiteName & value == "Yes")
  })

  output$Gender <- renderPlot({
    df1 <- dfInput()
    ggplot(df1) +
      aes(x = gender, y = popWeight / sum(popWeight)) +
      stat_summary(fun.y = sum, geom = "bar") +
      scale_y_continuous("Population (%)", labels = scales::percent)
  })
}