R 根据数据库中的反应子集数据动态选择输入

R 根据数据库中的反应子集数据动态选择输入,r,shiny,R,Shiny,设置:我已经建立了一个闪亮的应用程序与两个情节。我使用flexdashboard-包在两个选项卡中创建了两个绘图。此外,我在R-markdown中编程了整个闪亮的应用程序 现在我想创建一个界面,用户可以在其中对数据进行子集。这一部分本身就是有效的。然而,在我绘制两个图之前,我还需要使用子集数据执行一些计算 是否有任何方法可以将某些子集对象(如mydata)转换为数据帧?我的问题是,我还需要在其他绘图的UI部分中使用此子集对象 编辑:我特别需要某种方法将我的选择从checkboxGroupInpu

设置:我已经建立了一个闪亮的应用程序与两个情节。我使用
flexdashboard
-包在两个选项卡中创建了两个绘图。此外,我在R-markdown中编程了整个闪亮的应用程序

现在我想创建一个界面,用户可以在其中对数据进行子集。这一部分本身就是有效的。然而,在我绘制两个图之前,我还需要使用子集数据执行一些计算

是否有任何方法可以将某些子集对象(如
mydata
)转换为数据帧?我的问题是,我还需要在其他绘图的UI部分中使用此子集对象

编辑:我特别需要某种方法将我的选择从
checkboxGroupInput
传输到
selectInput(“cat_1”,“category 1:”,choices=levels(mydata()$mycat)

###1.创建一些示例数据

myrows我花了一段时间才弄明白你的代码。所以:

1) 利用
renderUI
可以动态创建控件

2) 坚持使用一个
ui

3) 确保您非常了解
渲染
以及您要绘制的内容

library(shiny)
library(plotly)

### 1. Create some sample data
myrows<-sample(letters,12)
exdata<- data.frame(mycat=rep(myrows,2),yr=rep(1:2,each=12),KPI_1=rnorm(24),
                    KPI_2=round(runif(24,1,20)),KPI_3=rbinom(24,6,0.5))


ui <- fluidPage(

  sidebarPanel(
    uiOutput("c1"),uiOutput("c2")),
  mainPanel(
    column(6,
    checkboxGroupInput("comp", "Categories",myrows,myrows,inline=TRUE),
    actionButton("go", "Update"),
    textOutput("txt"),
    tableOutput("head")),
    column(6,
    plotlyOutput("plot3", height = 300, width = 700)))
)

server <- function(input, output) {
  ### 3. Server part
  mydata <- eventReactive(input$go,{
    res<-subset(exdata,mycat%in%input$comp)
    return(res)
  })

  output$txt <- renderText({
    paste("You chose", paste(input$comp, collapse = ", "))
  })
  output$head <- renderTable({
    mydata()
  })

  conrolsdata <- reactive({
    unique(as.character(mydata()$mycat))
  })
  output$c1 <- renderUI({
    selectInput("cat_1", "Variable:",conrolsdata())
  })

  output$c2 <- renderUI({
    selectInput("cat_2", "Variable:",conrolsdata())
  })


  output$plot3 <- renderPlotly({

    if(is.null(input$cat_1)){
      return()
    }

    y1<- mydata()$KPI_1[as.character(mydata()$mycat) %in% input$cat_1]
    y2<- mydata()$KPI_2[as.character(mydata()$mycat) %in% input$cat_2]
    x0<-c(1,2)
     #use the key aesthetic/argument to help uniquely identify selected observations
     plot_ly(x = x0,y = y1, type="scatter",mode='lines+markers',name="Firm1") %>%
       add_trace(y = y2, name = "Firm2", mode = 'lines+markers') %>%
       layout(dragmode = "select")
  })

}

shinyApp(ui, server)
库(闪亮)
图书馆(绘本)
### 1. 创建一些示例数据

myrowsI无法重现您的问题,因为一切都对我有利。也许你没有显示整个代码哦,是的。很抱歉将在一秒钟内添加它。请更新您的代码,因为很难知道它的确切位置。我认为您可以在服务器中创建一个对象,存储输出并在触发操作时更新它,然后在selectInput@akrun很抱歉给你带来了困惑。我已经做到了。问题在于
selectInput(“cat_1”,“category 1:”,choices=levels(mydata()$mycat),
。显然,shiny不喜欢其输入中的动态元素。如果您想构建一个绘图,这是有意义的,但如果您构建一个完整的仪表板,则不太可能。我指的是
choices=levels(mydata()$mycat)
应该更改谢谢。奇怪代码的主要原因是使用了
flexdashboard
和R-markdown。根据我目前的经验,我建议所有在使用
flexdashboard
时遇到问题的人尝试(重新)在
shinny
中创建应用程序。这可能会解决一些问题。很高兴了解如何应用
renderUI
,不知何故,我在
shinny
上的复习课没有提到这一点。
library(plotly)
library(shiny)

### 4. UI part of my plot
fluidRow(sidebarLayout(sidebarPanel(
           selectInput("cat_1",
                       "  category 1:",
                       choices = levels(mydata()$mycat),
                       selected = levels(mydata()$mycat)[1]),
           selectInput("cat_2",
                       "  category 2:",
                       choices = levels(mydata()$mycat),
                       selected = levels(mydata()$mycat)[2])),
           mainPanel(plotlyOutput("plot3", height = 300, width = 700))))

  ### 5. Server part of my plot
  output$plot3 <- renderPlotly({
  ## 5.1 Create plot data      
      cat1<-input$cat_1
      cat2<-input$cat_2
      y1<-as.numeric(mydata()[mydata()$mycat==cat1])
      y2<-as.numeric(mydata()[mydata()$mycat==cat2])
      x0<-c(1,2)

  ## 5.2 Do plot
  plot_ly(x = x0,y = y1, type="scatter",mode='lines+markers',name=Firm1) %>%
  add_trace(y = y2, name = Firm2, mode = 'lines+markers') %>%
            layout(dragmode = "select")
library(shiny)
library(plotly)

### 1. Create some sample data
myrows<-sample(letters,12)
exdata<- data.frame(mycat=rep(myrows,2),yr=rep(1:2,each=12),KPI_1=rnorm(24),
                    KPI_2=round(runif(24,1,20)),KPI_3=rbinom(24,6,0.5))


ui <- fluidPage(

  sidebarPanel(
    uiOutput("c1"),uiOutput("c2")),
  mainPanel(
    column(6,
    checkboxGroupInput("comp", "Categories",myrows,myrows,inline=TRUE),
    actionButton("go", "Update"),
    textOutput("txt"),
    tableOutput("head")),
    column(6,
    plotlyOutput("plot3", height = 300, width = 700)))
)

server <- function(input, output) {
  ### 3. Server part
  mydata <- eventReactive(input$go,{
    res<-subset(exdata,mycat%in%input$comp)
    return(res)
  })

  output$txt <- renderText({
    paste("You chose", paste(input$comp, collapse = ", "))
  })
  output$head <- renderTable({
    mydata()
  })

  conrolsdata <- reactive({
    unique(as.character(mydata()$mycat))
  })
  output$c1 <- renderUI({
    selectInput("cat_1", "Variable:",conrolsdata())
  })

  output$c2 <- renderUI({
    selectInput("cat_2", "Variable:",conrolsdata())
  })


  output$plot3 <- renderPlotly({

    if(is.null(input$cat_1)){
      return()
    }

    y1<- mydata()$KPI_1[as.character(mydata()$mycat) %in% input$cat_1]
    y2<- mydata()$KPI_2[as.character(mydata()$mycat) %in% input$cat_2]
    x0<-c(1,2)
     #use the key aesthetic/argument to help uniquely identify selected observations
     plot_ly(x = x0,y = y1, type="scatter",mode='lines+markers',name="Firm1") %>%
       add_trace(y = y2, name = "Firm2", mode = 'lines+markers') %>%
       layout(dragmode = "select")
  })

}

shinyApp(ui, server)