R 在应用程序中为动态用户定义数据使用plotOutput中的过滤器
我在为ggplot2可视化过滤数据时发现问题。我想使用一个过滤器并输入f值,就像使用R 在应用程序中为动态用户定义数据使用plotOutput中的过滤器,r,graphics,shiny,R,Graphics,Shiny,我在为ggplot2可视化过滤数据时发现问题。我想使用一个过滤器并输入f值,就像使用 data %>% filter(Col == Number) ggplot() 根据数据集定制绘图 我使用这段代码生成了一个闪亮的应用程序,用户可以上传数据并以交互方式绘制数据,但过滤器不起作用。 : 库(闪亮) 图书馆(tidyverse) ui我个人不会在ggplot()调用中进行任何子集设置。您也不需要(实际上也不应该)在ggplot()调用中指定data()。我可能会这样做(尽管如果您不提供
data %>%
filter(Col == Number)
ggplot()
根据数据集定制绘图
我使用这段代码生成了一个闪亮的应用程序,用户可以上传数据并以交互方式绘制数据,但过滤器不起作用。:
库(闪亮)
图书馆(tidyverse)
ui我个人不会在ggplot()
调用中进行任何子集设置。您也不需要(实际上也不应该)在ggplot()
调用中指定data()
。我可能会这样做(尽管如果您不提供可复制的数据,很难进行测试):
后者不起作用,因为它试图绘制mtcars$carb
,而不是过滤的(mtcars==2
)向量。感谢您的回答,但在这种情况下,它只需要数据点而不是整个列来进行绘制。如果我们使用x_-var作为时间序列,y_-var作为连续变量;它会一次显示一个点,而不是所有的数据。嗨@Mutaz,请看我的编辑——这行吗?我认为我们需要aes\u string
以编程方式传递变量。谢谢,但没有任何变化,事实上,在尝试绘图时,它会给出一个清晰的图形,即使在尝试不同的数据集时也是如此。在未来,尝试提供一些数据,以便试图提供帮助的人能够使用与您相同的数据。非常感谢,@heds1!但我尝试将其用作数据可视化工具,而不是用于特定的数据集。
library(shiny)
library(tidyverse)
ui <- shinyUI(
fluidPage(
h1(' output'),
h2('Graphics'),
sidebarLayout(
sidebarPanel(
fileInput(inputId = 'file1', label = 'Upload your Data:', accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
tags$hr(),
selectInput('xcol', "X variable:", "", selected = ""),
selectInput('ycol', 'Y variable:', "", selected = ""),
selectInput('filter1', 'Filter:', "", selected = "", multiple = TRUE),
selectInput('filter2', 'Filter Value',"",selected = "")
),
mainPanel(
plotOutput("plot")
)
)
)
)
server <- shinyServer(function(session, input, output){
data <- reactive({
req(input$file1)
inFile <- input$file1
df <- read_csv(inFile$datapath)#, header = T, sep=",")
updateSelectInput(session, inputId = 'xcol', label = 'X variable:',
choices = names(df), selected = names(df))
updateSelectInput(session, inputId = 'ycol', label = 'Y variable:',
choices = names(df), selected = names(df))
updateSelectInput(session, inputId = 'filter1', label = 'Filter:',
choices = names(df), selected ="")
observe({
updateSelectInput(session, inputId = 'filter2', label = 'Filter value', choices =c(0:10))
})
return(df)
})
output$plot <- renderPlot({
F1 <- input$filter1
F2 <- input$filter2
data() %>% filter(F1==F2)%>%
ggplot(aes(x =data()[input$xcol],y= data()[input$ycol]))+
geom_point()
})
})
shinyApp(ui = ui, server = server)
x_var <- reactive(input$xcol)
y_var <- reactive(input$ycol)
data() %>%
filter(F1 == F2) %>%
ggplot(aes_string(x = x_var(), y = y_var())) +
geom_point()
mtcars %>% filter(carb == 2) %>% ggplot(aes(x = carb, y = wt)) + geom_point()
# this works!
mtcars %>% filter(carb == 2) %>% ggplot(aes(x = mtcars$carb, y = mtcars$wt)) + geom_point()
# this doesn't