首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据selectInput选择列

根据selectInput选择列
EN

Stack Overflow用户
提问于 2021-02-24 06:55:29
回答 1查看 35关注 0票数 0

我正在构建一个闪亮的,最终结果将是一个图形,在x轴上有“深度”,在y轴上有“值”。用户将上传他们自己的数据帧,所以我包含了两个输入,这两个输入将根据数据帧进行更改。有一个checkboxGroupInput("elements", label = "Select Elements", choices = NULL)然后就是selectInput("depth_col", "Depth Column:", choices = NULL)这两种选择都是由相同的observeEvent在我的server.R中读取上传的dataframe的列名。当我试图绘制这个的时候,我的问题就来了。在我的测试数据集中,我知道" Depth“列实际上被称为Depth,所以:

代码语言:javascript
复制
output$Gamma <- renderPlot({
    input$slice
    
    if(input$slice == 0){
      return()
    }
    
    inFile <- inFile()
    Element <- input$elements
    
    inFile %>%
      select(c(Depth, all_of(Element))) %>%
      pivot_longer(-Depth, names_to = "Elements", values_to = "Values") %>%
      ggplot(aes(x = Depth, y = Values, colour = Elements)) +
      geom_path()
  })

生成所需的绘图(输入$slice是单击“选择”按钮的结果。

请注意,它只为两个选定的元素生成行。但是,当我尝试调整代码以使用input$depth_col(例如,在不同的数据集中,"Depth“列可能称为Deep ):

代码语言:javascript
复制
output$Gamma <- renderPlot({
    
    input$slice
    
    if(input$slice == 0){
      return()
    }
    
    inFile <- inFile()
    Deep <- input$depth_col
    Element <- input$elements
    
    inFile %>%
      select(c(Deep, all_of(Element))) %>%
      pivot_longer(-Deep, names_to = "Elements", values_to = "Values") %>%
      ggplot(aes(x = Deep, y = Values, colour = Elements)) +
      geom_path()
  })

它将Deep视为一个字符,而不是读取列中的值并绘制一条直线。

本质上,在“深度列”中选择的值应该分配给x轴和y轴上的"Select Elements“。另外两个输入将在稍后使用,但在这一点上应该不会有任何影响。

谢谢所有能告诉我我哪里错了的人,杰里米

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-24 16:27:10

您需要告诉R在input$depth_col作为对象名(仅在必要时,dplyr::select例如接受字符串)。您可以使用以下命令来完成此操作!! sym(input$depth_col)..。

代码语言:javascript
复制
output$Gamma <- renderPlot({
    input$slice
    
    if(input$slice == 0){
      return()
    }
    
    inFile <- inFile()
    Element <- input$elements
    
    inFile %>%
      select(all_of(c(input$depth, Element))) %>%
      pivot_longer(!! sym(input$depth_col), names_to = "Elements", values_to = "Values") %>%
      ggplot(aes(x = !! sym(input$depth_col), y = Values, colour = Elements)) +
      geom_path()
  })
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66342276

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档