如何通过合并 csv 文件创建数据框,然后基于它创建闪亮的应用程序?
How do I create a dataframe from merging csv files and then create a shiny app based on it?
我有一个可能很愚蠢的问题,但我只是想创建我的第一个 R shiny 应用程序。我正在考虑编写一个代码,其中将读取 2 个单独的 csv 文件,然后将其组合成一个数据帧。在组合它们之前,我需要在每个数据框中添加几列,如代码所示。所以我想我可以在 ui 和服务器之外操作数据,然后创建允许用户 select 他们想要在每个轴上绘制的应用程序。闪亮部分之外的代码的第一部分工作正常,我得到了所需的组合数据框。但是,当我 运行 代码时,似乎没有创建数据框,因此我最终得到一个空界面。
如有任何帮助,我们将不胜感激!附上我有一张 csv 文件中的列的图片,第二个 csv 文件类似。
shop1.csv
library(tidyverse)
library(shiny)
twofiles<-c("shop1.csv","shop2.csv")
shop_list<-lapply(twofiles, read.csv, header=TRUE, sep=",")
shop_list<- lapply(seq_along(shop_list), function(i){
df <- shop_list[[i]]
df<-transform(df,ratio1=price_apples/price_pears)
df<-transform(df,ratio2=price_apples/price_cherries)
df<-transform(df,datano=i)
})
finaldata <- do.call(rbind, shop_list)
finaldata$datano <- factor(finaldata$datano)
ui<-fluidPage(
titlePanel("Shops plots"),
sidebarMenu(
selectInput(inputId = "x", label = "Select x-axis:",
choices = c("Year"="year","Hour"="hour"), selected="year"),
selectInput(inputId = "y", label = "Select y-axis:",
choices = c("Ratio 1"="ratio1","Ratio 2"="ratio2"),selected="ratio1"),
mainPanel(
plotOutput("plot")
)
)
)
server<-function(input, output) {
output$plot <-renderPlot({
ggplot(finaldata, aes(x=input$x, y=input$y, group=datano, color = datano)) +
geom_line()
})
}
shinyApp(ui=ui, server=server)
评论太长:
下面是 mtcars
数据集的示例代码:
您必须在 ggplot
中使用 aes_string
而不是 aes
。
数据帧操作,包括加载等。应该在服务器端完成。
另一件事是 group=datano
和 color=datano
也可能容易出现问题。在此处尝试 yourdataframe$datano
library(shiny)
library(dplyr)
library(ggplot2)
ui<-fluidPage(
titlePanel("XXX"),
sidebarMenu(
selectInput(inputId = "x", label = "Select x-axis:",
choices = c("mpg"="mpg","disp"="disp"), selected="mpg"),
selectInput(inputId = "y", label = "Select y-axis:",
choices = c("hp"="hp","drat"="drat"),selected="hp"),
mainPanel(
plotOutput("plot")
)
)
)
server<-function(input, output,session) {
output$plot <-renderPlot({
ggplot(finaldata, aes_string(x=input$x, y=input$y, group=mtcars$cyl, color = mtcars$cyl)) +
geom_line()
})
}
shinyApp(ui=ui, server=server)
我认为您遇到的问题与框架无关,而是与 ggplot2
的编程使用有关。一般来说,查找 ggplot2
和 quasiquotation
,你会发现自己陷入了 NSE(非标准评估)机制的泥潭,你认为你永远不需要(或者永远不知道你会需要,说实话)。
我可以用这个简单的非闪亮示例复制一些非工作性。
library(ggplot2)
input <- list(x = "mpg", y = "disp")
这会生成错误的图表,只有一个点:
ggplot(mtcars, aes(x=input$x, y=input$y)) + geom_point()
但是如果你切换到这个,它应该可以工作:
ggplot(mtcars, aes(x=!!sym(input$x), y=!!sym(input$y))) + geom_point()
也就是说,使用 !!sym(input$x)
而不仅仅是 input$x
(对于任何其他内容,您将放置在 aes(...)
.
中
如果你很好奇,旧的(软弃用的)解决方案是使用 aes_string
,它仍然有效(但我不知道多长时间):
ggplot(mtcars, aes_string(x=input$x, y=input$y)) + geom_point()
旁注:
我认为读入您的数据会更直接(并且代码高尔夫更简单):
twofiles <- setNames(nm = c("shop1.csv","shop2.csv"))
shop_list <- lapply(twofiles, read.csv, header=TRUE, sep=",")
finaldata <- transform(
dplyr::bind_rows(shop_list, .id = "datano"),
ratio1 = price_apples/price_pears,
ratio2 = price_apples/price_cherries,
datano = factor(datano)
)
虽然您已经使用了所有 tidyverse
,但您也可以切换到 dplyr
方法……并不是说它在这里给您带来了很多好处。
我有一个可能很愚蠢的问题,但我只是想创建我的第一个 R shiny 应用程序。我正在考虑编写一个代码,其中将读取 2 个单独的 csv 文件,然后将其组合成一个数据帧。在组合它们之前,我需要在每个数据框中添加几列,如代码所示。所以我想我可以在 ui 和服务器之外操作数据,然后创建允许用户 select 他们想要在每个轴上绘制的应用程序。闪亮部分之外的代码的第一部分工作正常,我得到了所需的组合数据框。但是,当我 运行 代码时,似乎没有创建数据框,因此我最终得到一个空界面。
如有任何帮助,我们将不胜感激!附上我有一张 csv 文件中的列的图片,第二个 csv 文件类似。
shop1.csv
library(tidyverse)
library(shiny)
twofiles<-c("shop1.csv","shop2.csv")
shop_list<-lapply(twofiles, read.csv, header=TRUE, sep=",")
shop_list<- lapply(seq_along(shop_list), function(i){
df <- shop_list[[i]]
df<-transform(df,ratio1=price_apples/price_pears)
df<-transform(df,ratio2=price_apples/price_cherries)
df<-transform(df,datano=i)
})
finaldata <- do.call(rbind, shop_list)
finaldata$datano <- factor(finaldata$datano)
ui<-fluidPage(
titlePanel("Shops plots"),
sidebarMenu(
selectInput(inputId = "x", label = "Select x-axis:",
choices = c("Year"="year","Hour"="hour"), selected="year"),
selectInput(inputId = "y", label = "Select y-axis:",
choices = c("Ratio 1"="ratio1","Ratio 2"="ratio2"),selected="ratio1"),
mainPanel(
plotOutput("plot")
)
)
)
server<-function(input, output) {
output$plot <-renderPlot({
ggplot(finaldata, aes(x=input$x, y=input$y, group=datano, color = datano)) +
geom_line()
})
}
shinyApp(ui=ui, server=server)
评论太长:
下面是 mtcars
数据集的示例代码:
您必须在 ggplot
中使用 aes_string
而不是 aes
。
数据帧操作,包括加载等。应该在服务器端完成。
另一件事是 group=datano
和 color=datano
也可能容易出现问题。在此处尝试 yourdataframe$datano
library(shiny)
library(dplyr)
library(ggplot2)
ui<-fluidPage(
titlePanel("XXX"),
sidebarMenu(
selectInput(inputId = "x", label = "Select x-axis:",
choices = c("mpg"="mpg","disp"="disp"), selected="mpg"),
selectInput(inputId = "y", label = "Select y-axis:",
choices = c("hp"="hp","drat"="drat"),selected="hp"),
mainPanel(
plotOutput("plot")
)
)
)
server<-function(input, output,session) {
output$plot <-renderPlot({
ggplot(finaldata, aes_string(x=input$x, y=input$y, group=mtcars$cyl, color = mtcars$cyl)) +
geom_line()
})
}
shinyApp(ui=ui, server=server)
我认为您遇到的问题与框架无关,而是与 ggplot2
的编程使用有关。一般来说,查找 ggplot2
和 quasiquotation
,你会发现自己陷入了 NSE(非标准评估)机制的泥潭,你认为你永远不需要(或者永远不知道你会需要,说实话)。
我可以用这个简单的非闪亮示例复制一些非工作性。
library(ggplot2)
input <- list(x = "mpg", y = "disp")
这会生成错误的图表,只有一个点:
ggplot(mtcars, aes(x=input$x, y=input$y)) + geom_point()
但是如果你切换到这个,它应该可以工作:
ggplot(mtcars, aes(x=!!sym(input$x), y=!!sym(input$y))) + geom_point()
也就是说,使用 !!sym(input$x)
而不仅仅是 input$x
(对于任何其他内容,您将放置在 aes(...)
.
如果你很好奇,旧的(软弃用的)解决方案是使用 aes_string
,它仍然有效(但我不知道多长时间):
ggplot(mtcars, aes_string(x=input$x, y=input$y)) + geom_point()
旁注:
我认为读入您的数据会更直接(并且代码高尔夫更简单):
twofiles <- setNames(nm = c("shop1.csv","shop2.csv")) shop_list <- lapply(twofiles, read.csv, header=TRUE, sep=",") finaldata <- transform( dplyr::bind_rows(shop_list, .id = "datano"), ratio1 = price_apples/price_pears, ratio2 = price_apples/price_cherries, datano = factor(datano) )
虽然您已经使用了所有
tidyverse
,但您也可以切换到dplyr
方法……并不是说它在这里给您带来了很多好处。