R 如何在屏幕上绘制selectInput()函数中选定的输入?
我在代码中使用以下两个输入函数:R 如何在屏幕上绘制selectInput()函数中选定的输入?,r,input,shiny,data-analysis,R,Input,Shiny,Data Analysis,我在代码中使用以下两个输入函数: selectInput("categoryVisu", label="SELECT CATEGORY", choices = list("Full" = "full", "Fact" = "fact", "Fact Positive" = "factpos", selected = "full", multiple = TRUE) 及 我现在的任务是,如果用户选择例如Full和Notified,那么我的代码应该从我的数据集中获取InformedFull列来打印
selectInput("categoryVisu", label="SELECT CATEGORY", choices = list("Full" = "full", "Fact" = "fact", "Fact Positive" = "factpos", selected = "full", multiple = TRUE)
及
我现在的任务是,如果用户选择例如Full和Notified,那么我的代码应该从我的数据集中获取InformedFull列来打印代码。我该怎么办?我的数据集如下所示:
ggplot(data = OLS.Data, aes(x=OLS.Data$Date, y=...))+geom_line()
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)
),
我已经做了ggplot代码,看起来像这样:
ggplot(data = OLS.Data, aes(x=OLS.Data$Date, y=...))+geom_line()
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)
),
所以我把。。。它们应该基于my selectInput的booth选择,是我的数据集的正确列
我的UI框如下所示:
ggplot(data = OLS.Data, aes(x=OLS.Data$Date, y=...))+geom_line()
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)
})
}
将输入拉到两个,然后将它们粘贴在一起。一旦你有了它,就可以使用它从你的数据框中提取数据。将数据作为变量输入绘图函数
# 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指定为某个人的值,因此它与您的数据框列标题不匹配。解决此问题的简单方法是将选项的值设置为列标题中使用的名称,例如Informed=Informed
如果我们用按钮
>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 button
找到的解决方案:
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)
})
})