我正在使用以下两个输入函数代码:
selectInput("categoryVisu", label="SELECT CATEGORY", choices = list("Full" = "full", "Fact" = "fact", "Fact Positive" = "factpos", selected = "full", multiple = TRUE)和
selectInput("investerVisu", label="SELECT INVESTOR", choices = list("Informed" = "inf", "Noise" = "noise"), selected = "inf", multiple = TRUE)我现在的任务是,如果用户选择“完全”和“知情”,那么我的代码应该从我的数据集中取出列"InformedFull“来打印代码。我该怎么处理呢?我的DataSet看起来是这样的:
我已经做了ggplot代码,看起来是这样的:
ggplot(data = OLS.Data, aes(x=OLS.Data$Date, y=...))+geom_line()所以我把..。它们应该基于我的selectInput的展位选择,是我数据集的正确列。
我的UI框看起来如下:
tabItem(tabName = "visu",
fluidRow(
box(
title = "Controls-1",
status = "primary",
solidHeader = TRUE,
width = 3,
selectInput("categoryVisu", label="SELECT CATEGORY", choices = list("Full" = "Full", "Fact" = "Fact", "Fact Positive" = "Fact.Pos", "Fact Negative" = "Fact.Neg", "Emotions" = "Emotions", "Emotions Fact" = "EmotionsFact"), selected = "Full", multiple = TRUE)
),
box(
title = "Controls-2",
status = "primary",
solidHeader = TRUE,
width = 3,
selectInput("investerVisu", label="SELECT INVESTOR", choices = list("Informed" = "Informed", "Noise" = "Noise"), selected = "Informed", multiple = TRUE)
),我的服务器文件:
server <- function(input, output) {
observeEvent(input$categoryVisu,{
partA<-input$catagoryVisu
partA<-as.character(partA)
})
observeEvent(input$investerVisu,{
partB<-input$investerVisu
partB<-as.character(partB)
})
partC<-paste(partB,partA)
DataFus<-OLS.Data$partC
output$myPlot <- renderPlot({
ggplot(data = OLS.Data, aes(x=OLS.Data$Date, y=OLS.Data$NYSE))+geom_line()+geom_line(data = OLS.Data,aes(x=OLS.Data$Date, y=DataFus),color="red")+geom_line(alpha = input$alphaVisu)
})
}发布于 2018-06-07 12:07:52
找到解决办法:
server <- function(input, output) {
partA = NULL
partB = NULL
makeReactiveBinding("partA")
makeReactiveBinding("partB")
observeEvent(input$categoryVisu, {
partA<<-input$categoryVisu
partA<<-as.character(partA)
})
observeEvent(input$investorVisu, {
partB<<-input$investorVisu
partB<<-as.character(partB)
})
observe({
PartC<-as.character(paste(partB,partA,sep=""))
print(PartC)
output$myPlot <- renderPlot({
ggplot(data = OLS.Data, aes(x=OLS.Data$Date, y=OLS.Data$NYSE))+geom_line()+geom_line(data = OLS.Data,aes(x=OLS.Data$Date, y=OLS.Data[PartC]),color="red")+geom_line(alpha = input$alphaVisu)
})
})发布于 2018-06-06 17:42:28
把输入拉到两个,然后粘贴在一起。一旦您有了它,使用它从您的数据帧中提取数据。将数据作为变量输入到绘图函数中。
# If you have a button simply observeEvent(input$button, { and put this all
# under that environment.
partA<-reactive({input$categoryVisu
})
partB<-reactive({input$investerVisu
})
#paste together to get full column name
PartC<-reactive(paste(partA(),partB(),sep=""))
# Pull column from data frame OR put it directly into ggplot argument
Data<-OLS.Data[PartC]
output$myplot<-renderPlot({
#use column as Y-Axis series
ggplot(data = OLS.Data, aes(x=OLS.Data$Date, y=OLS.Data[PartC]))+geom_line()
})请注意,您已将"inf“指定为选择”知情“的人的值,因此它将与数据帧列标题不匹配。解决这一问题的简单方法是将选项的值设置为将在列标题中使用的名称,例如“知情的”=“知情的”
如果我们用一个按钮
>ui
#Add button to ui.R
actionButton("go","Reload")
>server
observeEvent(input$go, {
#Grab input to categoryVisu
partA<-input$categoryVisu
partA<-as.character(partA)
#Grab input to investerVisu
partB<-input$investerVisu
partB<-as.character(partB)
#paste together to get full column name
PartC<-paste(partA,partB,sep="")
# Pull column from data frame OR put it directly into ggplot argument
Data<-OLS.Data[PartC]
output$myplot<-renderPlot({
#use column as Y-Axis series
ggplot(data = OLS.Data, aes(x=OLS.Data$Date, y=OLS.Data[PartC]))+geom_line()
}) #Closes plot
}) #Closes observing of action buttonhttps://stackoverflow.com/questions/50726365
复制相似问题