我正在构建一个闪亮的,最终结果将是一个图形,在x轴上有“深度”,在y轴上有“值”。用户将上传他们自己的数据帧,所以我包含了两个输入,这两个输入将根据数据帧进行更改。有一个checkboxGroupInput("elements", label = "Select Elements", choices = NULL)然后就是selectInput("depth_col", "Depth Column:", choices = NULL)这两种选择都是由相同的observeEvent在我的server.R中读取上传的dataframe的列名。当我试图绘制这个的时候,我的问题就来了。在我的测试数据集中,我知道" Depth“列实际上被称为Depth,所以:
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 ):
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“。另外两个输入将在稍后使用,但在这一点上应该不会有任何影响。
谢谢所有能告诉我我哪里错了的人,杰里米
发布于 2021-02-24 16:27:10
您需要告诉R在input$depth_col作为对象名(仅在必要时,dplyr::select例如接受字符串)。您可以使用以下命令来完成此操作!! sym(input$depth_col)..。
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()
})https://stackoverflow.com/questions/66342276
复制相似问题