如果满足条件,则显示图表

时间:2016-02-21 22:18:53

标签: r shiny

是否可以仅在满足条件时显示绘图并在条件满足时隐藏其他绘图?

我尝试使用conditionalPanel()。如果条件满足,它可以工作并显示情节,但它也显示其他两个图。我希望仅在所选输入为

时才显示coolPlotcoolPlot_2
"X1_P"   "X2_S"   "X3_W"   "X4_S"   "X5_P"   "X6_P"   "X7_P"  
"X8_S"   "X9_P"   "X10_P"  "X11_P"  "X12_I"
只有在选择coolPlot_3并且应隐藏其他两个面板时,才必须显示

"X13_K"。 我使用了以下代码。

    ui = fluidPage(
  titlePanel("Data Visualization"),
  sidebarLayout(
    sidebarPanel(
                 uiOutput("variableOutput"),
                 uiOutput("text1Output")
    ),
  mainPanel(
    conditionalPanel(
      condition = "input.variableOutput != 'X13_K'",
      plotOutput("coolPlot")),
    br(),
    br(),
    conditionalPanel(
      condition = "input.variableOutput != 'X13_K'",
      plotOutput("coolPlot_2")),
    conditionalPanel(
    condition = "input.variableOutput == 'X13_K'",
  plotOutput("coolPlot_3")),
  br(),
  br(),
  dataTableOutput(
    "coolTable"
      )
    )
  )
)

正如尼斯所建议的那样,我也在发布服务器代码。

server = function(input,output){
  output$variableOutput = renderUI({
    selectInput("VariableInput",
                "Variable auswählen",
                choices = colnames(training)[-1] ,
                selected = 2)


  })

  plot_data = reactive({frame = as.data.frame(cbind(training[,"flag"],
                                             training[,input$VariableInput]))

                        frame[,1] = as.logical(frame[,1])

                        return(frame)

  })

  mean_data = reactive({data.frame(flag = c("2","1"),
                                   data = c(weighted.mean(training[,input$VariableInput][training$flag==1],
                                             training$weight[training$flag==1]),
                                            weighted.mean(training[,input$VariableInput][training$flag==0],
                                             training$weight[training$flag==0])))
  })
    output$coolPlot_3 = renderPlot({

      if (is.null(input$VariableInput)) {
        return(NULL)
      } 

      numb_classes = length(levels(training[,input$VariableInput]))

      row_names = levels(training[,input$VariableInput])

      plot_data_2 = data.frame(klassen   = character(numb_classes),
                               index = numeric(numb_classes),
                               stringsAsFactors = FALSE)

      for (j in 1:numb_classes){

        count_class_non_goal = count(subset(training[,input$VariableInput],
                                            training[,input$VariableInput] == row_names[j] & training[,"flag"] == FALSE))

        count_all_non_goal = count(training[training$flag == FALSE,input$VariableInput])

        percent_non_goal = count_class_non_goal[,2]/(sum(count_all_non_goal[,2])/100)

        count_class_goal = count(subset(training[,input$VariableInput],
                                        training[,input$VariableInput] == row_names[j] & training[,"flag"] == TRUE))

        count_all_goal = count(training[training$flag == TRUE,input$VariableInput])

        percent_goal = count_class_goal[,2]/(sum(count_all_goal[,2])/100)

        plot_data_2[,1][j] = row_names[j]

        plot_data_2[,2][j] = round((percent_goal/percent_non_goal*100)-100,
                                   digits =2)
      }

      ggplot(data = plot_data_2) + 
        geom_bar(aes(y = index, 
                     x = klassen),
                 stat= "identity")+ 
        coord_flip()+
        theme(legend.position = "none",
              axis.title.x = element_text(size=15,
                                          face = "bold"),
              axis.text.y  = element_text(size=12),
              axis.text.x  = element_text(size=12),
              axis.title.y = element_text(size=15,
                                          face = "bold"))+
        labs(x = paste(input$VariableInput),
             y = "Index")
    })

    output$coolPlot = renderPlot({
    if (is.null(input$VariableInput)) {
      return(NULL)
    } 

    ggplot(data = plot_data()) + 
      geom_boxplot(aes(x=V1,
                       y=V2,
                       fill=V1),
                   outlier.shape = NA) + 
      guides(fill=FALSE)+
      scale_x_discrete(labels=c("Nicht-Ziel", "Ziel")) +
      coord_cartesian(ylim = c(min(plot_data()[,"V2"]),
                               quantile(plot_data()[,"V2"])[4] + IQR(plot_data()[,"V2"], 
                                                  na.rm = TRUE, type = 7)*1.9)) +
      stat_boxplot(aes(x=V1,
                       y=V2,
                       fill=V1),
                   geom ='errorbar') + 
      theme(axis.title.x = element_text(size=15,
                                        face = "bold"),
            axis.text.y  = element_text(size=12),
            axis.text.x  = element_text(size=12),
            axis.title.y = element_text(size=15,
                                        face = "bold"))+
      labs(x = paste(input$VariableInput),
           y = "Ausprägung der Variable")
  })

  output$coolPlot_2 = renderPlot({
    if (is.null(input$VariableInput)) {
      return(NULL)
    } 

    ggplot(data = plot_data(),
           aes(x=V2,
               fill=V1)) + geom_density(alpha=.3)+
      geom_vline(data=mean_data(), 
                 aes(colour=flag,
                     xintercept=data),
                linetype="dashed", size=1)+
      scale_fill_discrete(name = "Gruppen",
                          labels=c("Nicht-Ziel",
                                   "Ziel"))+
      theme(axis.title.x = element_text(size=15,
                                        face = "bold"),
            axis.text.y  = element_text(size=12),
            axis.text.x  = element_text(size=12),
            axis.title.y = element_text(size=15,
                                        face = "bold"))+
      labs(x = paste(input$VariableInput),
           y = "Density")


  })



  output$coolTable = renderDataTable({ 
    training
  })
}

1 个答案:

答案 0 :(得分:5)

server.rui.r

之间的名称中,prolbem是一些不匹配

server.r中声明

output$variableOutput = renderUI({
    selectInput("VariableInput",
                "Variable auswählen",
                choices = colnames(training)[-1] ,
                selected = 2)

导致以下代码

<select id="VariableInput" ...>
  <option value="X13_K"></option>
</select>

在UI代码中,您将引用

uiOutput("variableOutput"),

用于输出控件。这是正确的,因为您将控件定义为output$variableOutput

然而,在您编写condition = "input.VariableOutput == 'X13_k'"时出现问题这应该引用控件的ID,该控件在

中定义为VariableInput
 selectInput("VariableInput",
                    "Variable auswählen",
                    choices = colnames(training)[-1] ,
                    selected = 2)

所以一个修复就是用作条件condition = "input.VariableInput == 'X13_k'"

但是,我的建议是替换VariableInputVariableOutput by VariableSelection`或类似的东西。

具有最小修复参考的代码:

library(shiny)
library(ggplot2)
training=iris
names(training)[5] <- c("X13")

server = function(input,output){
  output$variableOutput = renderUI({
    selectInput("VariableInput",
                "Variable auswählen",
                choices = colnames(training)[-1] ,
                selected = 2)


  })

  plot_data = reactive({frame = as.data.frame(cbind(training[,1],
                                                    training[,input$VariableInput]))

  frame[,1] = as.logical(frame[,1])

  return(frame)

  })

  mean_data = reactive({data.frame(flag = c("2","1"),
                                   data = c(weighted.mean(training[,input$VariableInput][training$flag==1],
                                                          training$weight[training$flag==1]),
                                            weighted.mean(training[,input$VariableInput][training$flag==0],
                                                          training$weight[training$flag==0])))
  })
  output$coolPlot_3 = renderPlot({

    if (is.null(input$VariableInput)) {
      return(NULL)
    } 

    numb_classes = length(levels(training[,input$VariableInput]))

    row_names = levels(training[,input$VariableInput])

    plot_data_2 = data.frame(klassen   = character(numb_classes),
                             index = numeric(numb_classes),
                             stringsAsFactors = FALSE)

    for (j in 1:numb_classes){

      count_class_non_goal = count(subset(training[,input$VariableInput],
                                          training[,input$VariableInput] == row_names[j] & training[,"flag"] == FALSE))

      count_all_non_goal = count(training[training$flag == FALSE,input$VariableInput])

      percent_non_goal = count_class_non_goal[,2]/(sum(count_all_non_goal[,2])/100)

      count_class_goal = count(subset(training[,input$VariableInput],
                                      training[,input$VariableInput] == row_names[j] & training[,"flag"] == TRUE))

      count_all_goal = count(training[training$flag == TRUE,input$VariableInput])

      percent_goal = count_class_goal[,2]/(sum(count_all_goal[,2])/100)

      plot_data_2[,1][j] = row_names[j]

      plot_data_2[,2][j] = round((percent_goal/percent_non_goal*100)-100,
                                 digits =2)
    }

    ggplot(data = plot_data_2) + 
      geom_bar(aes(y = index, 
                   x = klassen),
               stat= "identity")+ 
      coord_flip()+
      theme(legend.position = "none",
            axis.title.x = element_text(size=15,
                                        face = "bold"),
            axis.text.y  = element_text(size=12),
            axis.text.x  = element_text(size=12),
            axis.title.y = element_text(size=15,
                                        face = "bold"))+
      labs(x = paste(input$VariableInput),
           y = "Index")
  })

  output$coolPlot = renderPlot({
    if (is.null(input$VariableInput)) {
      return(NULL)
    } 

    ggplot(data = plot_data()) + 
      geom_boxplot(aes(x=V1,
                       y=V2,
                       fill=V1),
                   outlier.shape = NA) + 
      guides(fill=FALSE)+
      scale_x_discrete(labels=c("Nicht-Ziel", "Ziel")) +
      coord_cartesian(ylim = c(min(plot_data()[,"V2"]),
                               quantile(plot_data()[,"V2"])[4] + IQR(plot_data()[,"V2"], 
                                                                     na.rm = TRUE, type = 7)*1.9)) +
      stat_boxplot(aes(x=V1,
                       y=V2,
                       fill=V1),
                   geom ='errorbar') + 
      theme(axis.title.x = element_text(size=15,
                                        face = "bold"),
            axis.text.y  = element_text(size=12),
            axis.text.x  = element_text(size=12),
            axis.title.y = element_text(size=15,
                                        face = "bold"))+
      labs(x = paste(input$VariableInput),
           y = "Ausprägung der Variable")
  })

  output$coolPlot_2 = renderPlot({
    if (is.null(input$VariableInput)) {
      return(NULL)
    } 

    ggplot(data = plot_data(),
           aes(x=V2,
               fill=V1)) + geom_density(alpha=.3)+
      geom_vline(data=mean_data(), 
                 aes(colour=flag,
                     xintercept=data),
                 linetype="dashed", size=1)+
      scale_fill_discrete(name = "Gruppen",
                          labels=c("Nicht-Ziel",
                                   "Ziel"))+
      theme(axis.title.x = element_text(size=15,
                                        face = "bold"),
            axis.text.y  = element_text(size=12),
            axis.text.x  = element_text(size=12),
            axis.title.y = element_text(size=15,
                                        face = "bold"))+
      labs(x = paste(input$VariableInput),
           y = "Density")


  })



  output$coolTable = renderDataTable({ 
    training
  })
}

library(shiny)
ui = fluidPage(
  titlePanel("Data Visualization"),
  sidebarLayout(
    sidebarPanel(
      uiOutput("variableOutput"),
      uiOutput("text1Output")
    ),
    mainPanel(
      conditionalPanel(
        "input.VariableInput != 'X13'",
        plotOutput("coolPlot")),
     # textInput("bla","blub",input.variableOutput),
      br(),
      br(),
      conditionalPanel(
        condition = "input.VariableInput != 'X13'",
        plotOutput("coolPlot_2")),
      conditionalPanel(
        condition = "input.VariableInput == 'X13'",
        plotOutput("coolPlot_3")),
      br(),
      br(),
      dataTableOutput(
        "coolTable"
      )
    )
  )
)
相关问题