【发布时间】:2017-09-05 07:31:49
【问题描述】:
设置: 我已经构建了一个带有两个绘图的闪亮应用程序。我使用flexdashboard-package 在两个选项卡中创建了两个图。此外,我在 R-markdown 中编写了整个闪亮应用程序。
现在我想创建一个界面,用户可以在其中对数据进行子集化。该部分本身有效。但是,在我绘制两个图之前,我还需要对子集数据执行一些计算。
有什么方法可以将mydata 之类的子集对象转换为数据框?我的问题是我还需要在其他绘图的 UI 部分中使用这个子集对象。
编辑:我特别需要一些方法将我的选择从checkboxGroupInput 传输到selectInput("cat_1"," category 1:",choices = levels(mydata()$mycat)。
### 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))
### 2. UI part
fluidPage(fluidRow(
checkboxGroupInput("comp", "Categories",myrows,myrows,inline=TRUE),
actionButton("go", "Update"),
textOutput("txt"),
tableOutput("head"))
)
### 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()
})
在下一个块中,我这样做:
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")
【问题讨论】:
-
我无法重现您的问题,因为一切都对我有用。也许您没有显示整个代码
-
哦,是的。对不起。将在一秒钟内添加它
-
能否请您更新您的代码,因为很难知道它到底适合哪里。我认为您可以在服务器中创建一个对象,存储输出并在触发操作时对其进行更新,然后在 selectInput 中调用该对象
-
@akrun 很抱歉造成混乱。我已经做了。问题在
selectInput("cat_1"," category 1:",choices = levels(mydata()$mycat),。显然闪亮不喜欢其输入中的动态元素。如果您想构建一个情节,这是有道理的,但如果您构建一个完整的仪表板,则不是真的 -
我的意思是应该更改
choices = levels(mydata()$mycat)