R Shiny - 警告:[.data.frame 中的错误:创建两个交互式绘图时选择了未定义的列

问题描述 投票:0回答:2
我正在 R Shiny 中创建两个交互式图,虽然我可以让一个图显示并工作,但第二个图一直给我“警告:[.data.frame 中的错误:选择了未定义的列”并且不会出现。

我在网上查看了很多解决方案,但到目前为止没有一个能够帮助我或解决我的问题。

我很难看到我的列是如何未定义的,但我对 R Shiny 也比较陌生,很容易忽略一些东西,所以我希望有人可以帮助我解决这个问题。

这是我的代码:

library(shiny) library(dplyr) library(readr) library(ggplot2) library(tidyverse) age <- c(1, 4, 7,10, 15) v_m_1 <- c(10, 14, 17, 20, 25) v_m_2 <- c(9, 13, 16, 19, 24) sex <- c("F", "M","U", "F", "M") P_v_rn <- c(0.11, 0.51, 0.61, 0.91, 1) C_v_rn <- c(11.1, 15.1, 16.1, 19.1, 20.1) P_v_rk <- c(0.11, 0.51, 0.61, 0.91, 1) B_v_rk <- c("Low", "Medium", "Medium", "High", "High") df_test <- data.frame(age, v_m_1, v_m_2, sex, P_v_rn, C_v_rn, P_v_rk, B_v_rk) # Define UI for application that draws a histogram ui <- fluidPage( # Application title titlePanel("Test"), # Sidebar with a slider input for number of bins verticalLayout( sidebarLayout( sidebarPanel( selectInput(inputId = "xvar", label = "Choose X variable", #All variables are numeric c("Age" = 1), selected = 1), selectInput(inputId = "yvar", label = "Choose bone variable", #All variables are numeric c("v_m_1" = 2, "v_m_2" = 3), selected = 2), checkboxInput(inputId = "regression", label = "Fit LOESS - By Sex", value = FALSE)), mainPanel( plotOutput('dataplot1') ) ), tags$hr(), sidebarLayout( sidebarPanel( selectInput(inputId = "xvar_name", label = "Choose X variable", #All variables are numeric c("Age" = 1), selected = 1), selectInput(inputId = "yvar_name", label = "Choose Y variable", #The first variable option is numeric, the rest are factors c("P_v_rk" = 7, "B_v_rk" = 8), selected = 7), selectInput(inputId = "zvar_name", label = "Choose Z variable", #All variables are numeric c("C_v_rn" = 6, "P_v_rn" = 5), selected = 6)), # Show a plot of the generated distribution mainPanel( plotOutput('dataplot2') ) ), tags$hr(), )) # Define server logic required to draw a scatterplot server <- function(input, output) { df <- df_test %>% select(age, v_m_1, v_m_2, sex, P_v_rn, C_v_rn, P_v_rk, B_v_rk) df$B_v_rk <- as.factor(df$B_v_rk) #Growth Curve output$dataplot1 <- renderPlot({ xvar <- as.numeric(input$xvar) yvar <- as.numeric(input$yvar) Sex <- as.factor(df$sex) p <- ggplot() + aes(x = df[ ,xvar], y = df[ ,yvar], col = sex) + geom_point(alpha = 0.5, aes(size = 1.5)) + # 50% transparent labs(x = names(df[xvar]), y = names(df[yvar])) + theme_classic() if(input$regression) { # add a line to the plot p <- p + geom_smooth() } p # The plot ('p') is the "return value" of the renderPlot function }) #Environmental metrics output$dataplot2 <- renderPlot({ xvar_name <- input$xvar_name yvar_name <- input$yvar_name zvar_name <- input$zvar_name #Color palette for ggplots as blue color range was difficult for me fun_color_range <- colorRampPalette(c("yellow", "red")) my_colors <- fun_color_range(20) p2 <- ggplot() + aes(x = df[ ,xvar_name], y = df[ ,yvar_name], col = df[ ,zvar_name]) + geom_point(alpha = 0.5, aes(size = 1.5)) + # 50% transparent scale_colour_gradientn(colors = my_colors) + labs(x = names(df[xvar_name]), y = names(df[yvar_name])) + theme_classic() p2 # The plot ('p2') is the "return value" of the renderPlot function }) } # Run the application shinyApp(ui = ui, server = server)
第一个图再次工作正常,第二个图产生错误代码。

我想我很困惑,因为第一个图的代码工作正常,但它不适用于第二个图。

作为参考,这是我想要的布局,但我想在错误代码位置绘制另一个图。

enter image description here

r ggplot2 shiny
2个回答
0
投票
我的猜测是该错误与

names(df[xvar_name])

一致。如果 df 是数据框,这将引发您引用的错误。要使用索引或列名称对数据框进行子集化,您可以使用双括号 (
df[[...]]
) 或逗号 (
df[ ..., ... ]
)。我想你的意思是
names(df[ , xvar_name ])
。此错误也在下面的行中重复出现。

一般来说,要确定问题发生的位置,请在代码中使用 browser() 。


0
投票
我不断收到相同的代码错误。

start_cord_df1 <- df1 %>% st_as_sf(coords = c("start_lng", "start_lat "))
    
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.