带条件的 R Shiny Reactive 输入文件
R Shiny Reactive input file with conditions
我正在使用 R shiny 构建一个仪表板,其中包含两个电子表格并比较它们以提供一些结果。当我为一种模式这样做时,它工作得很好。但是当我尝试根据输入参数添加两种不同类型的比较时,它没有通过。这是我最初在 server.R 中所做的(并且有效),
report1 <- eventReactive(input$Report1, {
read.csv(input$Report1$datapath)
report2 <- eventReactive(input$Report2, {
read.csv(input$Report2$datapath)
其中 Report1 和 Report2 是来自 ui.R 的输入文件。我想要的是添加一个参数来决定如何处理输入文件。假设这两种处理方法称为 "Method1" 和 "Method2"。我不能使用相同的方法来调用这两种类型,因为第二组文件是 .crm 格式,它是一个“;”分离的文件,R 不能直接识别的东西。
需要明确的是,以下代码适用于非服务器 运行.
中的 .crm 文件
File <- read.csv(file, sep = ";", header = TRUE)
所以我想做的是
handlingMethod <- reactive(input$Method)
if (handlingMethod() == "Method1"){
report1 <- eventReactive(input$Report1, {
read.csv(input$Report1$datapath)})
report2 <- eventReactive(input$Report2, {
read.csv(input$Report2$datapath)})
} else if (handlingMethod() == "Method2"){
report1 <- eventReactive(input$Report1, {
read.csv(input$Report1$datapath,sep = ";", header = TRUE)})
report2 <- eventReactive(input$Report2, {
read.csv(input$Report2$datapath, sep = ";", header = TRUE)})
} else {}
但这给了我错误 "You tried to do something that can only be done from inside a reactive expression or observer." 我以为我已经理解了那个问题并解决了它,但显然不是。然后我把整个东西放在一个反应式表达式中,如下所示,
reactive({if (handlingMethod() == "Method1"){
report1 <- eventReactive(input$Report1, {
read.csv(input$Report1$datapath)})
report2 <- eventReactive(input$Report2, {
read.csv(input$Report2$datapath)})
} else if (handlingMethod() == "Method2"){
report1 <- eventReactive(input$Report1, {
read.csv(input$Report1$datapath,sep = ";", header = TRUE)})
report2 <- eventReactive(input$Report2, {
read.csv(input$Report2$datapath, sep = ";", header = TRUE)})
} else {}})
这使得闪亮的应用程序 运行,但随后向我显示消息,"Error in report1: could not find function "report1""
有没有人见过这种错误?如果是这样,请告诉我它是什么,我将不胜感激。
尝试将 if...else...
放入 eventReactive
。此外,行 handlingMethod <- reactive(input$Method)
似乎是不必要的,因为您可以直接使用 input$Method
而不是将其复制到 handlingMethod()
。见下文:
report1 <- eventReactive(input$Report1, {
if (input$Method == "Method1"){
read.csv(input$Report1$datapath)
} else if (input$Method == "Method2"){
read.csv(input$Report1$datapath,sep = ";", header = TRUE)
} else{}
})
report2 <- eventReactive(input$Report2, {
if (input$Method == "Method1"){
read.csv(input$Report2$datapath)
} else if (input$Method == "Method2"){
read.csv(input$Report2$datapath, sep = ";", header = TRUE)
} else{}
})
我认为 else{}
不是必需的,但没什么大不了的。
我正在使用 R shiny 构建一个仪表板,其中包含两个电子表格并比较它们以提供一些结果。当我为一种模式这样做时,它工作得很好。但是当我尝试根据输入参数添加两种不同类型的比较时,它没有通过。这是我最初在 server.R 中所做的(并且有效),
report1 <- eventReactive(input$Report1, {
read.csv(input$Report1$datapath)
report2 <- eventReactive(input$Report2, {
read.csv(input$Report2$datapath)
其中 Report1 和 Report2 是来自 ui.R 的输入文件。我想要的是添加一个参数来决定如何处理输入文件。假设这两种处理方法称为 "Method1" 和 "Method2"。我不能使用相同的方法来调用这两种类型,因为第二组文件是 .crm 格式,它是一个“;”分离的文件,R 不能直接识别的东西。 需要明确的是,以下代码适用于非服务器 运行.
中的 .crm 文件File <- read.csv(file, sep = ";", header = TRUE)
所以我想做的是
handlingMethod <- reactive(input$Method)
if (handlingMethod() == "Method1"){
report1 <- eventReactive(input$Report1, {
read.csv(input$Report1$datapath)})
report2 <- eventReactive(input$Report2, {
read.csv(input$Report2$datapath)})
} else if (handlingMethod() == "Method2"){
report1 <- eventReactive(input$Report1, {
read.csv(input$Report1$datapath,sep = ";", header = TRUE)})
report2 <- eventReactive(input$Report2, {
read.csv(input$Report2$datapath, sep = ";", header = TRUE)})
} else {}
但这给了我错误 "You tried to do something that can only be done from inside a reactive expression or observer." 我以为我已经理解了那个问题并解决了它,但显然不是。然后我把整个东西放在一个反应式表达式中,如下所示,
reactive({if (handlingMethod() == "Method1"){
report1 <- eventReactive(input$Report1, {
read.csv(input$Report1$datapath)})
report2 <- eventReactive(input$Report2, {
read.csv(input$Report2$datapath)})
} else if (handlingMethod() == "Method2"){
report1 <- eventReactive(input$Report1, {
read.csv(input$Report1$datapath,sep = ";", header = TRUE)})
report2 <- eventReactive(input$Report2, {
read.csv(input$Report2$datapath, sep = ";", header = TRUE)})
} else {}})
这使得闪亮的应用程序 运行,但随后向我显示消息,"Error in report1: could not find function "report1""
有没有人见过这种错误?如果是这样,请告诉我它是什么,我将不胜感激。
尝试将 if...else...
放入 eventReactive
。此外,行 handlingMethod <- reactive(input$Method)
似乎是不必要的,因为您可以直接使用 input$Method
而不是将其复制到 handlingMethod()
。见下文:
report1 <- eventReactive(input$Report1, {
if (input$Method == "Method1"){
read.csv(input$Report1$datapath)
} else if (input$Method == "Method2"){
read.csv(input$Report1$datapath,sep = ";", header = TRUE)
} else{}
})
report2 <- eventReactive(input$Report2, {
if (input$Method == "Method1"){
read.csv(input$Report2$datapath)
} else if (input$Method == "Method2"){
read.csv(input$Report2$datapath, sep = ";", header = TRUE)
} else{}
})
我认为 else{}
不是必需的,但没什么大不了的。