Rshiny 无法输出 ggstatsplot

Jor*_*tro 0 r shiny

我尝试在 Rshiny 中使用 ggstats 绘制箱提琴图。我收到错误无法转换为符号。尽管我已经将输入作为符号传递,但我仍然面临这个错误。数据表没有问题,因为我尝试从 Rshiny 中绘制它并且它有效。有人知道为什么吗?

用户界面

library (ggstatsplot)
library (shiny)

total_data <- read_rds('data/total_data.rds')


ui <- fluidPage(

    # Application title
    titlePanel("Trial"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
          selectInput(inputId = "Weekday",
                      label = "Choose a Weekday Type",
                      choices = c( "Weekday Earnings" = "weekday_earn",
                                   "Weekend Earnings" = "weekend_earn",
                                   "Total Earnings" = "total_earn"
                      ),
                      selected = "total_earn")
        ),

        # Show a plot of the generated distribution
        mainPanel(
           plotOutput("distPlot")
        )
    )
)

Run Code Online (Sandbox Code Playgroud)

输出

server <- function(input, output) {

    output$distPlot <- renderPlot({
      ggbetweenstats(
        data = total_data,
        x = type,
        y = .data[[input$Weekday]],
        type = "p",
        xlab = "Business Type",
        ylab = "Revenue",
        palette = "Set3",
        ggtheme = ggplot2::theme_gray()
      )
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

Run Code Online (Sandbox Code Playgroud)

Total_data 的输出

structure(list(Id = c(445, 446, 447, 448, 449, 895, 896, 897, 
898, 899, 1345, 1346, 1347, 1348, 1349, 1801, 1802, 1803, 1804, 
1805, 442, 443, 444, 892, 893, 894, 1342, 1343, 1344, 1798, 1799, 
1800), weekday_earn = c(31317.15, 55244.16, 138308.94, 103455.33, 
143642.1, 82046.76, 95939.92, 98409.6, 48108, 125085.35, 66392.2, 
28863.9, 37467.36, 22592.43, 20925.99, 211606.04, 63408.56, 33355.8, 
40972.32, 157498.25, 109567.27, 86654.63, 124132.1, 162232.24, 
202706.02, 133736.67, 363413.08, 186544.61, 309771.65, 229891.03, 
143759.45, 201677.06), weekend_earn = c(11726.55, 19344.63, 62996.84, 
31286.09, 69409.13, 52711.68, 25854.32, 81682.56, 5912.22, 38165.75, 
57311.8, 8137.35, 4282.11, 18112.86, 3286.53, 54284.52, 18079.72, 
19357.5, 51800, 61501.18, 128365.38, 94894.62, 127113.99, 189662.93, 
251234.36, 176850.53, 417060.38, 240337.75, 369086.74, 230502.94, 
159370.02, 228480.24), total_earn = c(43043.7, 74588.79, 201305.78, 
134741.42, 213051.23, 134758.44, 121794.24, 180092.16, 54020.22, 
163251.1, 123704, 37001.25, 41749.47, 40705.29, 24212.52, 265890.56, 
81488.28, 52713.3, 92772.32, 218999.43, 237932.65, 181549.25, 
251246.09, 351895.17, 453940.38, 310587.2, 780473.46, 426882.36, 
678858.39, 460393.97, 303129.47, 430157.3), type = c("Restaurant", 
"Restaurant", "Restaurant", "Restaurant", "Restaurant", "Restaurant", 
"Restaurant", "Restaurant", "Restaurant", "Restaurant", "Restaurant", 
"Restaurant", "Restaurant", "Restaurant", "Restaurant", "Restaurant", 
"Restaurant", "Restaurant", "Restaurant", "Restaurant", "Pub", 
"Pub", "Pub", "Pub", "Pub", "Pub", "Pub", "Pub", "Pub", "Pub", 
"Pub", "Pub")), row.names = c(NA, -32L), class = c("tbl_df", 
"tbl", "data.frame"))

Run Code Online (Sandbox Code Playgroud)

ste*_*fan 5

代词.data仅在动词内部使用时才有效,dplyr例如 inside ggplot2::aes()。但是,在您的情况下ggbetweenstats需要一个不带引号的列名称和错误消息

ensym 错误:无法转换为符号。

建议,通过 进行底层处理rlang::ensym

因此,要修复您的代码,您必须传递一个,symbol例如ggbetweenstats 可以通过以下方式实现!!rlang::sym(input$Weekday)

library(ggstatsplot)
library(shiny)

ui <- fluidPage(

  # Application title
  titlePanel("Trial"),

  # Sidebar with a slider input for number of bins
  sidebarLayout(
    sidebarPanel(
      selectInput(
        inputId = "Weekday",
        label = "Choose a Weekday Type",
        choices = c(
          "Weekday Earnings" = "weekday_earn",
          "Weekend Earnings" = "weekend_earn",
          "Total Earnings" = "total_earn"
        ),
        selected = "total_earn"
      )
    ),

    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("distPlot")
    )
  )
)

server <- function(input, output) {
  output$distPlot <- renderPlot({
    ggbetweenstats(
      data = total_data,
      x = type,
      y = !!rlang::sym(input$Weekday),
      type = "p",
      xlab = "Business Type",
      ylab = "Revenue",
      palette = "Set3",
      ggtheme = ggplot2::theme_gray()
    )
  })
}

# Run the application
shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述