我正在try 使用一个shiny 的小部件根据列名设置我的数据集的子集,但似乎不起作用.

## app.R ##
library(shiny)
library(shinydashboard)
library(shinyjs)
library(htmlwidgets)
library(shinyWidgets)
diamonds<-structure(list(`Approved Amount_UA Equivalent` = c(690221.630165446, 
                                                             75290000, 27500000, 2131605.31196044, 7580000, 5920000, 5947178.82036962, 
                                                             191844.478076439, 8e+07, 11842251.7331135, 11842251.7331135, 
                                                             11842251.7331135, 22250000, 27990000, 2273712.3327578, 89721563.7663194, 
                                                             14933054.9695902, 1200000, 38520632.9893852, 72490558.1048068
), `Approved Amount_USDEquivalent` = c(920065.43301054, 100361570, 
                                       36657500, 2841429.88084326, 10104140, 7891360, 7927589.3675527, 
                                       255728.689275894, 106640000, 15785721.5602403, 15785721.5602403, 
                                       15785721.5602403, 29659250, 37310670, 3030858.53956615, 119598844.500504, 
                                       19905762.2744638, 1599600, 51348003.7748504, 96629913.9537075
), `High Five Prority 1: Feed Africa` = c(NA, NA, NA, NA, NA, 
                                          NA, 178415.364611089, NA, NA, NA, NA, NA, 22250000, NA, NA, 89721563.7663194, 
                                          447991.649087706, 36000, NA, NA), `High Five Prority 2: Light Up And Power Africa` = c(NA, 
                                                                                                                                 75290000, 27500000, NA, NA, NA, 59471.7882036962, NA, NA, NA, 
                                                                                                                                 NA, NA, NA, NA, NA, NA, 149330.549695902, 12000, 38520632.9893852, 
                                                                                                                                 NA), `High Five Prority 3: Industrialize Africa` = c(NA, NA, 
                                                                                                                                                                                      NA, 2131605.31196044, NA, NA, NA, NA, NA, 11842251.7331135, 11842251.7331135, 
                                                                                                                                                                                      11842251.7331135, NA, NA, 2273712.3327578, NA, NA, NA, NA, 72490558.1048068
                                                                                                                                 ), `High Five Prority 4: Integrate Africa` = c(NA, NA, NA, NA, 
                                                                                                                                                                                NA, NA, NA, NA, 8e+07, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                                                                                                                                                                                NA), `High Five Prority 5: Improve Quality Of Life` = c(690221.630165446, 
                                                                                                                                                                                                                                        NA, NA, NA, 7580000, 5920000, 5709291.66755484, 191844.478076439, 
                                                                                                                                                                                                                                        NA, NA, NA, NA, NA, 27990000, NA, NA, 14335732.7708066, 1152000, 
                                                                                                                                                                                                                                        NA, NA), `NUMBER OF PROJECT` = c(1, 1, 1, 1, 0.5, 0.5, 1, 1, 
                                                                                                                                                                                                                                                                         1, 1, 1, 1, 1, 1, 1, 1, 0.5, 0.5, 1, 1), `Amount in UA Million` = c(0.690221630165446, 
                                                                                                                                                                                                                                                                                                                                             75.29, 27.5, 2.13160531196044, 7.58, 5.92, 5.94717882036962, 
                                                                                                                                                                                                                                                                                                                                             0.191844478076439, 80, 11.8422517331135, 11.8422517331135, 11.8422517331135, 
                                                                                                                                                                                                                                                                                                                                             22.25, 27.99, 2.2737123327578, 89.7215637663194, 14.9330549695902, 
                                                                                                                                                                                                                                                                                                                                             1.2, 38.5206329893852, 72.4905581048068)), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                                                                                      -20L), class = c("tbl_df", "tbl", "data.frame"))

ui <- dashboardPage(
  dashboardHeader(
    title="Task Managers' Workload Analysis",
    titleWidth = 400
    
  ),
  dashboardSidebar(
    selectInput("var","Select variable",choices = colnames(diamonds),selected = colnames(diamonds)[1])
  ),
  dashboardBody(
    plotOutput("hist")
  )
)

server <- function(input, output,session) {
  output$hist<-renderPlot({
    ggplot(data=diamonds, aes_string(x=input$var)) +
      geom_histogram(fill="steelblue", color="black") +
      ggtitle("Histogram of Price Values")
  })
}

shinyApp(ui, server)

推荐答案

建议将aes_string替换为.data代词:

## app.R ##
library(shiny)
library(shinydashboard)
library(shinyjs)
library(htmlwidgets)
library(shinyWidgets)
library(ggplot2)

# `stat_bin()` requires a continuous x aesthetic
choices <- setdiff(colnames(diamonds), c("cut", "color", "clarity"))

ui <- dashboardPage(
  dashboardHeader(title = "Task Managers' Workload Analysis",
                  titleWidth = 400),
  dashboardSidebar(
    selectInput(
      "var",
      "Select variable",
      choices = choices,
      selected = choices[1]
    )
  ),
  dashboardBody(plotOutput("hist"))
)

server <- function(input, output, session) {
  output$hist <- renderPlot({
    ggplot(data = diamonds, aes(x = .data[[input$var]])) +
      geom_histogram(fill = "steelblue", color = "black") +
      ggtitle("Histogram of Price Values")
  })
}

shinyApp(ui, server)

Edit: aes_string()在ggplot2 3.0.0中已弃用.-现在改用?sym:

## app.R ##
library(shiny)
library(shinydashboard)
library(shinyjs)
library(htmlwidgets)
library(shinyWidgets)
library(ggplot2)

# `stat_bin()` requires a continuous x aesthetic
choices <- setdiff(colnames(diamonds), c("cut", "color", "clarity"))

ui <- dashboardPage(
  dashboardHeader(title = "Task Managers' Workload Analysis",
                  titleWidth = 400),
  dashboardSidebar(
    selectInput(
      "var",
      "Select variable",
      choices = choices,
      selected = choices[1]
    )
  ),
  dashboardBody(plotOutput("hist"))
)

server <- function(input, output, session) {
  output$hist <- renderPlot({
    ggplot(data = diamonds, aes(x = !!sym(input$var))) +
      geom_histogram(fill = "steelblue", color = "black") +
      ggtitle("Histogram of Price Values")
  })
}

shinyApp(ui, server)

R相关问答推荐

如何在ggplot 2 geom_segment图表中将UTC转换为EET?

变量计算按R中的行更改

如何使用rmarkdown和kableExtra删除包含折叠行的表的第一列的名称

R Markdown中的交叉引用表

R for循环返回到先前值

如何在modelsummary中重命名统计数据?

如何编辑gMarginal背景以匹配绘图背景?

使用整齐的计算(curl -curl )和杂音

R中插入符号训练函数的中心因子和尺度因子预测

迭代到DataFrame列并获得成对的值列表(col1->;col2、col2->;col3、col3->;col4等)的正确方法.

使用for循环和粘贴创建多个变量

如何根据数据帧中的值从该数据帧中提取值?

TreeNode打印 twig 并为其上色

在多页PDF中以特定布局排列的绘图列表不起作用

派生程序包| ;无法检索';return()';的正文

如何从向量构造一系列双边公式

我们如何在R中透视数据并在之后添加计算

如何删除设置大小的曲线图并添加条形图顶部数字的百分比

R将函数参数传递给ggploy

如何捕获这个shiny 的、可扩展的react 性用户输入矩阵作为另一个react 性对象,以便进一步操作?