“ Shiny”中的对象存在问题

时间:2019-03-01 12:24:40

标签: r shiny

我在尝试创建Shiny应用程序时遇到问题。使用此表:

data <- data.frame(
  variable1 = c('S','S','S','N','N','N'),
  variable2 = c('S','S','N','N','N','N'),
  TYPE = c('REAL','ESTUDIO','REAL','ESTUDIO','REAL','ESTUDIO')
  )

UI:

    ui <- fluidPage(
  selectInput(inputId = "variable", 
              label = "Variable:",
              choices = c("variable1","variable2"), 
              selected = "variable1"),

  selectInput(inputId = "medida", 
              label = "Medida:",
              choices = c("Mix","Freq"), 
              selected = "Mix"),

  plotOutput("plot"))

服务器:

server <- function(input, output) {

  dateRangeInput<-reactive({
    dataset = data %>%
      group_by(variable1,TYPE) %>%
      summarise(Freq=n()) %>%
      inner_join(data %>%
                   group_by(TYPE) %>%
                   summarise(Freq_Tot=n())
                 ,
                 by="TYPE") %>%
      mutate(Mix=Freq/Freq_Tot)
    dataset
  })

  output$plot <-renderPlot({
    ggplot(data=dateRangeInput(), 
           aes_string(x=input$variable,
                      y=input$medida,
                      fill="TYPE")) +
      geom_bar(stat="identity",
               position="dodge")
  })

}

它工作正常(我认为这不是历史上最好的R代码,我只是在学习),如果我运行最终代码,则:

shinyApp (ui = ui, server = server)

结果:

Results

如您所见,我做了一个简单的应用程序(我刚刚开始),所以我想让图形包含在UI中选择的变量。因此,当我进行这些更改时:

  • 在服务器代码中,当我定义dateRangeInput时,我在该组中使用了变量“ variable1”。
  • 现在我将input $ variable代替。

    dateRangeInput<-reactive({
      dataset = data %>%
        group_by(input$variable,TYPE) %>%
        summarise(Freq=n()) %>%
        inner_join(data %>%
                     group_by(TYPE) %>%
                     summarise(Freq_Tot=n())
                   ,
                   by="TYPE") %>%
        mutate(Mix=Freq/Freq_Tot)
      dataset
    })
    

它导致此错误:

error

我认为代码无法将输出理解为变量。我尝试使用开关功能对其进行修复,但此方法不起作用。

1 个答案:

答案 0 :(得分:0)

如我所见,问题似乎是变量的原始计数。那是因为上面的代码只考虑了变量1,而不是两个变量,这意味着如果要求输入变量2,闪亮的应用程序将出错。

在为非大数据构建闪亮的应用程序时,通常会创建一个脚本,以在没有反应性元素的情况下尽我所能。在这种情况下,这将为我提供以下信息:

library(shiny)
library(tidyverse)

data <- tibble(
    variable1 = c('S','S','S','N','N','N'),
    variable2 = c('S','S','N','N','N','N'),
    TYPE = c('REAL','ESTUDIO','REAL','ESTUDIO','REAL','ESTUDIO')
)

data1 <- data %>% 
    transmute_all(as_factor)

data_tidy <- data1 %>% 
    gather(
        key = "Variable", 
        value = "value", 
        -TYPE
        )

...生成:

# A tibble: 12 x 3
   TYPE    Variable  value
   <fct>   <chr>     <chr>
 1 REAL    variable1 S    
 2 ESTUDIO variable1 S    
 3 REAL    variable1 S    
 4 ESTUDIO variable1 N    
 5 REAL    variable1 N    
 6 ESTUDIO variable1 N    
 7 REAL    variable2 S    
 8 ESTUDIO variable2 S    
 9 REAL    variable2 N    
10 ESTUDIO variable2 N    
11 REAL    variable2 N    
12 ESTUDIO variable2 N  

然后我们可以总结这些数据:

data_summary_Freq <- data_tidy %>% 
    count(TYPE, Variable, value, name = "Freq")

data_summary_Total <- data_summary_Freq %>% 
    group_by(Variable, TYPE) %>% 
    summarise(total = sum(Freq))

data_summary <- data_summary_Freq %>% 
    left_join(
        data_summary_Total, 
        by = c("Variable", "TYPE")
        ) %>% 
    mutate(Mix = Freq / total) %>% 
    gather("Output", "Number", -TYPE, -Variable, -value, -total)

...并且为data_summary产生以下结果:

# A tibble: 16 x 6
   TYPE    Variable  value total Output Number
   <fct>   <chr>     <chr> <int> <chr>   <dbl>
 1 REAL    variable1 N         3 Freq    1    
 2 REAL    variable1 S         3 Freq    2    
 3 REAL    variable2 N         3 Freq    2    
 4 REAL    variable2 S         3 Freq    1    
 5 ESTUDIO variable1 N         3 Freq    2    
 6 ESTUDIO variable1 S         3 Freq    1    
 7 ESTUDIO variable2 N         3 Freq    2    
 8 ESTUDIO variable2 S         3 Freq    1    
 9 REAL    variable1 N         3 Mix     0.333
10 REAL    variable1 S         3 Mix     0.667
11 REAL    variable2 N         3 Mix     0.667
12 REAL    variable2 S         3 Mix     0.333
13 ESTUDIO variable1 N         3 Mix     0.667
14 ESTUDIO variable1 S         3 Mix     0.333
15 ESTUDIO variable2 N         3 Mix     0.667
16 ESTUDIO variable2 S         3 Mix     0.333

剩下的问题涉及围绕此数据框构建闪亮的代码。

总体而言,这意味着:

library(shiny)
library(tidyverse)

data <- tibble(
    variable1 = c('S','S','S','N','N','N'),
    variable2 = c('S','S','N','N','N','N'),
    TYPE = c('REAL','ESTUDIO','REAL','ESTUDIO','REAL','ESTUDIO')
)

data1 <- data %>% 
    transmute_all(as_factor)

data_tidy <- data1 %>% 
    gather(
        key = "Variable", 
        value = "value", 
        -TYPE
        )

data_summary_Freq <- data_tidy %>% 
    count(TYPE, Variable, value, name = "Freq")

data_summary_Total <- data_summary_Freq %>% 
    group_by(Variable, TYPE) %>% 
    summarise(total = sum(Freq))

data_summary <- data_summary_Freq %>% 
    left_join(
        data_summary_Total, 
        by = c("Variable", "TYPE")
        ) %>% 
    mutate(Mix = Freq / total) %>% 
    gather("Output", "Number", -TYPE, -Variable, -value, -total)

ui <- fluidPage(
    selectInput(inputId = "variable", 
                label = "Variable:",
                choices = c("variable1","variable2"), 
                selected = "variable1"),

    selectInput(inputId = "medida", 
                label = "Medida:",
                choices = c("Mix","Freq"), 
                selected = "Mix"),

    plotOutput("plot")
    )

server <- function(input, output) {

    dateRangeInput <- reactive({
        data_summary %>% 
            filter(
                Variable == input$variable,
                Output == input$medida
                )
    })

    output$plot <-renderPlot({
        dateRangeInput() %>% 
            ggplot() +
            geom_bar(
                aes(
                    x = value,
                    y = Number, 
                    group = TYPE,
                    fill = TYPE
                    ),
                stat = "identity",
                position = "dodge"
                ) +
            labs(
                title = paste(input$medida, "information about", input$variable)
            )
        })

}

shinyApp (ui = ui, server = server)

...生成this app,我已将其上传到 shinyapps.io

我希望我已正确理解您的问题,对您有帮助。

祝一切顺利...