使用闪亮的 r 应用程序绘制分类变量和数值变量

Plotting categorical and numerical varibles with a shiny r app

我正在开发 Shiny R 应用程序,但我不明白为什么我的功能 draw_plot_1 无法正常工作。因为只是在绘制最后else中的数据。我愿意

plot a boxplot when one variable is numeric and the other one is categorical.

在闪亮的应用程序之外,我的功能运行良好。任何建议都会很棒!

这是我的代码:

not_sel <- "No selected"

about_page <- tabPanel(
  title = "Info",
  titlePanel("Info"),
  "Creado con R Shiny",
  br(),
  "Mayo 2022"
)

main_page <- tabPanel(
  title = "Analysis",
  titlePanel("Analysis"),
  sidebarLayout(
    sidebarPanel(
      title = "Inputs",
      fileInput("csv_input", "Select CSV File to Import", accept = ".csv"),
      selectInput("num_var_1", "Variable 1", choices = c(not_sel)),
      selectInput("num_var_2", "Variable 2", choices = c(not_sel)),
      br(),
      actionButton("run_button", "Run Analysis", icon = icon("play"))
    ),
    mainPanel(
      tabsetPanel(
        tabPanel(
          title = "Plot",
          plotOutput("plot_1")
        
        
          )
        
        )
      
    )
  )
)

draw_plot_1 <- function(data_input, num_var_1, num_var_2){
  
  if(num_var_1 != not_sel & num_var_2 != not_sel)
  {
    if(is.character(num_var_2) & is.numeric(num_var_1))
    {
      ggplot(data = data_input,
             aes_string(x = num_var_1, y = num_var_2)) +
        geom_boxplot()  
    }
    else if (is.character(num_var_1) & is.numeric(num_var_2))
    {
      ggplot(data = data_input,
             aes_string(x = num_var_2, y = num_var_1)) +
        geom_boxplot()  
    }
    else
    {
      ggplot(data = data_input,
             aes_string(x = num_var_2, y = num_var_1)) +
        geom_point()  
    }
    } 
}

ui <- navbarPage(
  title = "Data Analyser",
  theme = shinytheme('united'),
  main_page,
  about_page
)

server <- function(input, output){
  
  options(shiny.maxRequestSize=10*1024^2) 
  
  data_input <- reactive({
    req(input$csv_input)
    fread(input$csv_input$datapath)
  })
  
  observeEvent(data_input(),{
    choices <- c(not_sel,names(data_input()))
    updateSelectInput(inputId = "num_var_1", choices = choices)
    updateSelectInput(inputId = "num_var_2", choices = choices)
  })
  
  num_var_1 <- eventReactive(input$run_button,input$num_var_1)
  num_var_2 <- eventReactive(input$run_button,input$num_var_2)

  
  # plot
  
  plot_1 <- eventReactive(input$run_button,{
    draw_plot_1(data_input(), num_var_1(), num_var_2())
  })
  
  output$plot_1 <- renderPlot(plot_1())
  
  output$num_var_1_title <- renderText(paste("Num Var 1:",num_var_1()))
  
}

shinyApp(ui = ui, server = server)

因为 select 列表输入控件中的 return 值是一个字符。我附上了一张截图,显示了 num_var_1()num_var_2().[=12= 的值]

我建议您插入 browser() 函数来调试您的代码,您可以 运行 下面的代码以更好地理解您的代码。

not_sel <- "No selected"

about_page <- tabPanel(
  title = "Info",
  titlePanel("Info"),
  "Creado con R Shiny",
  br(),
  "Mayo 2022"
)

main_page <- tabPanel(
  title = "Analysis",
  titlePanel("Analysis"),
  sidebarLayout(
    sidebarPanel(
      title = "Inputs",
      fileInput("csv_input", "Select CSV File to Import", accept = ".csv"),
      selectInput("num_var_1", "Variable 1", choices = c(not_sel)),
      selectInput("num_var_2", "Variable 2", choices = c(not_sel)),
      br(),
      actionButton("run_button", "Run Analysis", icon = icon("play"))
    ),
    mainPanel(
      tabsetPanel(
        tabPanel(
          title = "Plot",
          plotOutput("plot_1")
        
        
          )
        
        )
      
    )
  )
)

draw_plot_1 <- function(data_input, num_var_1, num_var_2){
  
  if(num_var_1 != not_sel & num_var_2 != not_sel)
  {
    if(is.character(num_var_2) & is.numeric(num_var_1))
    {
      ggplot(data = data_input,
             aes_string(x = num_var_1, y = num_var_2)) +
        geom_boxplot()  
    }
    else if (is.character(num_var_1) & is.numeric(num_var_2))
    {
      ggplot(data = data_input,
             aes_string(x = num_var_2, y = num_var_1)) +
        geom_boxplot()  
    }
    else
    {
      ggplot(data = data_input,
             aes_string(x = num_var_2, y = num_var_1)) +
        geom_point()  
    }
    } 
}

ui <- navbarPage(
  title = "Data Analyser",
  theme = shinytheme('united'),
  main_page,
  about_page
)

server <- function(input, output){
  
  options(shiny.maxRequestSize=10*1024^2) 
  
  data_input <- reactive({
    req(input$csv_input)
    fread(input$csv_input$datapath)
  })
  
  observeEvent(data_input(),{
    choices <- c(not_sel,names(data_input()))
    updateSelectInput(inputId = "num_var_1", choices = choices)
    updateSelectInput(inputId = "num_var_2", choices = choices)
  })
  
  num_var_1 <- eventReactive(input$run_button,input$num_var_1)
  num_var_2 <- eventReactive(input$run_button,input$num_var_2)

  
  # plot
  
  plot_1 <- eventReactive(input$run_button,{
    draw_plot_1(data_input(), num_var_1(), num_var_2())
  })
  
  output$plot_1 <- renderPlot(plot_1())
  
  output$num_var_1_title <- renderText(paste("Num Var 1:",num_var_1()))
  
}

shinyApp(ui = ui, server = server)